c# 使用 HttpWebRequest模拟登陆
在C#中,可以使用HttpWebRequest进行相关的模拟登陆,登陆后进行相关的操作,比如抓取数据,页面分析,制作相关登陆助手等等。
先说下流程
1.使用httpwebrequest先进入你要登录的网站,获取cookie
2.使用第一步获取的cookie到验证码的网页将验证码下载下来。
3.使用Post数据 发送至网站。如果有cookie则继续保存。
4.使用第三步的cookie登陆相关网页操作。
获取相关数据可以使用抓包工具进行抓取,如httpwatch。(网上下载的好多都有病毒,下载的时候注意点)
1。
- /// <summary>
- /// 通过get方式请求页面,传递一个实例化的cookieContainer
- /// </summary>
- /// <param name="postUrl"></param>
- /// <param name="cookie"></param>
- /// <returns></returns>
- public static ArrayList GetHtmlData(string postUrl, CookieContainer cookie)
- {
- HttpWebRequest request;
- HttpWebResponse response;
- ArrayList list = new ArrayList();
- request = WebRequest.Create(postUrl) as HttpWebRequest;
- request.Method = "GET";
- request.UserAgent = "Mozilla/4.0";
- request.CookieContainer = cookie;
- request.KeepAlive = true;
- request.CookieContainer = cookie;
- try
- {
- //获取服务器返回的资源
- using (response = (HttpWebResponse)request.GetResponse())
- {
- using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default))
- {
- cookie.Add(response.Cookies);
- //保存Cookies
- list.Add(cookie);
- list.Add(reader.ReadToEnd());
- list.Add(Guid.NewGuid().ToString());//图片名
- }
- }
- }
- catch (WebException ex)
- {
- list.Clear();
- list.Add("发生异常/n/r");
- WebResponse wr = ex.Response;
- using (Stream st = wr.GetResponseStream())
- {
- using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default))
- {
- list.Add(sr.ReadToEnd());
- }
- }
- }
- catch (Exception ex)
- {
- list.Clear();
- list.Add("5");
- list.Add("发生异常:" + ex.Message);
- }
- return list;
- }
2.下载验证码,保存在本地。
- /// <summary>
- /// 下载验证码图片并保存到本地
- /// </summary>
- /// <param name="Url">验证码URL</param>
- /// <param name="cookCon">Cookies值</param>
- /// <param name="savePath">保存位置/文件名</param>
- public static bool DowloadCheckImg(string Url, CookieContainer cookCon, string savePath)
- {
- bool bol = true;
- HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
- //属性配置
- webRequest.AllowWriteStreamBuffering = true;
- webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
- webRequest.MaximumResponseHeadersLength = -1;
- webRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
- webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
- webRequest.ContentType = "application/x-www-form-urlencoded";
- webRequest.Method = "GET";
- webRequest.Headers.Add("Accept-Language", "zh-cn");
- webRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
- webRequest.KeepAlive = true;
- webRequest.CookieContainer = cookCon;
- try
- {
- //获取服务器返回的资源
- using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
- {
- using (Stream sream = webResponse.GetResponseStream())
- {
- List<byte> list = new List<byte>();
- while (true)
- {
- int data = sream.ReadByte();
- if (data == -1)
- break;
- list.Add((byte)data);
- }
- File.WriteAllBytes(savePath, list.ToArray());
- }
- }
- }
- catch (WebException ex)
- {
- bol = false;
- }
- catch (Exception ex)
- {
- bol = false;
- }
- return bol;
- }
3。发送post数据
- /// <summary>
- /// 发送相关数据至页面
- /// 进行登录操作
- /// 并保存cookie
- /// </summary>
- /// <param name="postData"></param>
- /// <param name="postUrl"></param>
- /// <param name="cookie"></param>
- /// <returns></returns>
- public static ArrayList PostData(string postData, string postUrl, CookieContainer cookie)
- {
- ArrayList list = new ArrayList();
- HttpWebRequest request;
- HttpWebResponse response;
- ASCIIEncoding encoding = new ASCIIEncoding();
- request = WebRequest.Create(postUrl) as HttpWebRequest;
- byte[] b = encoding.GetBytes(postData);
- request.UserAgent = "Mozilla/4.0";
- request.Method = "POST";
- request.CookieContainer = cookie;
- request.ContentLength = b.Length;
- using (Stream stream = request.GetRequestStream())
- {
- stream.Write(b, 0, b.Length);
- }
- try
- {
- //获取服务器返回的资源
- using (response = request.GetResponse() as HttpWebResponse)
- {
- using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
- {
- if (response.Cookies.Count > 0)
- cookie.Add(response.Cookies);
- list.Add(cookie);
- list.Add(reader.ReadToEnd());
- }
- }
- }
- catch (WebException wex)
- {
- WebResponse wr = wex.Response;
- using (Stream st = wr.GetResponseStream())
- {
- using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default))
- {
- list.Add(sr.ReadToEnd());
- }
- }
- }
- catch (Exception ex)
- {
- list.Add("发生异常/n/r"+ex.Message);
- }
- return list;
- }
4。就是第三步请求的链接地址换一个就行了
好了
以上核心代码已经贴出了
具体实现需要靠你们按照你们自己的逻辑
还有一些header能不写就不写,因为我2天前一直在获取返回response这地方报500错误。
找了N多代码,看了N多资料都不可以。最后将一些header注释掉就可以了,真郁闷。
c# 使用 HttpWebRequest模拟登陆的更多相关文章
- c# 使用 HttpWebRequest模拟登陆(附带验证码)
在C#中,可以使用HttpWebRequest进行相关的模拟登陆,登陆后进行相关的操作,比如抓取数据,页面分析,制作相关登陆助手等等. 先说下流程 1.使用httpwebrequest先进入你要登录的 ...
- 使用C#的HttpWebRequest模拟登陆访问人人网
使用任何语言做模拟登陆或者抓取访问页面,无外乎以下思路: 第一 启用一个web访问会话方法或者实例化一个web访问类,如.net中的HttpWebRequest:第二 模拟POST或者GET方式提交的 ...
- 使用C#的HttpWebRequest模拟登陆访问人人网(转)
无论使用任何语言做模拟登陆或者抓取访问页面,无外乎以下思路:第一 启用一个web访问会话方法或者实例化一个web访问类,如.net中的HttpWebRequest:第二 模拟POST或者GET方式提交 ...
- 使用HttpWebRequest模拟登陆阿里巴巴(alibaba、httpwebrequest、login)
前言 其实老喜欢取经,偶尔也得分享下.关于阿里巴巴国际站的登陆,过程有点复杂但是算不上难.一不小心少个东西倒也挺麻烦的. 主要是看下请求类HttpClient基本请求封装使用,AliClient模拟浏 ...
- 转:使用C#的HttpWebRequest模拟登陆网站
这篇文章是有关模拟登录网站方面的. 实现步骤: 启用一个web会话 发送模拟数据请求(POST或者GET) 获取会话的CooKie 并根据该CooKie继续访问登录后的页面,获取后续访问的页面数据. ...
- 使用C#的HttpWebRequest模拟登陆网站
很久没有写新的东西了,今天在工作中遇到的一个问题,感觉很有用,有种想记下来的冲动. 这篇文章是有关模拟登录网站方面的. 实现步骤: 启用一个web会话 发送模拟数据请求(POST或者GET) 获取会话 ...
- HttpWebRequest模拟登陆,存储Cookie以便登录请求后使用
[一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/3284481.html] PostLogin :登录,并保存Cookie 1 public st ...
- C#如何HttpWebRequest模拟登陆,获取服务端返回Cookie以便登录请求后使用
public static string GetCookie(string requestUrlString, Encoding encoding, ref CookieContainer cooki ...
- 通过HttpWebRequest实现模拟登陆
1>通过HttpWebRequest模拟登陆 using System; using System.Collections.Generic; using System.Linq; using S ...
随机推荐
- Java核心技术 卷Ⅰ 基础知识(2)
第四章 对象与类 基于类的访问权限 静态域 类的设计技巧
- smoke.js是一款基于HTML5 Canvas的逼真烟雾特效js插件。通过该js插件,可以非常轻松的在页面中制作出各种烟雾效果。
Smoke.js 是一个浏览器默认警告系统的JavaScript替代品,如果你想要跨浏览器与平台的标准化JavaScript警告窗口,Smoke.js就是你想要的. Smoke.js是一个轻量级且灵活 ...
- 字典(Tire树)
4189 字典 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description 最经,skyzhong得到了一本好厉害的字典,这个 ...
- PSR
目前包括以下几个规范: PSR-0(弃用) PSR-1 PSR-2 PSR-3 PSR-4 1.PSR-0 自动加载规范,此规范已被启用-本规范已于2014年10月21日被标记为弃用,目前新的替代规范 ...
- Imagine Cup 微软“创新杯”全球学生科技大赛
一. 介绍 微软创新杯微博:http://blog.sina.com.cn/u/1733906825 官方站点:https://www.microsoft.com/china/msdn/student ...
- 【CodeVS】1293
输入输出样例 思路:看到题目我萌第一眼想到的肯定是求联通快对吧,但是这个联通快有点奇特,因为 这样他也算是一个联通快.解决此题其实有三种解法:1)宽搜(这个符合基本法):2)并查集:3)灌水法 但是蒟 ...
- jsp页面根据当前时间和定义时间差计算动态倒计时
jsp页面根据当前时间和定义时间差计算动态倒计时http://www.jb51.net/article/74140.htm var maxtime =1000*60; //半个小时,按秒计算,自 ...
- CodeForces 667C Reberland Linguistics
$dp$. 题意中有一个词组:$in$ $a$ $row$,是连续的意思.... 因此这题只要倒着$dp$一下就可以了.$f[i][0]$表示从$i$位置往后割两个能否割,$f[i][1]$表示从$i ...
- python向服务器发送邮件事例
import osimport sysimport re __author__ = 'xiaoming' import requestststr = '<div>\n<ul>\ ...
- 杂谈3之English
1.面向过程(OPP):Orient Procedure Program (C语言) 2.面向对象(OOP):Orient ObjectProgram(Java) 3.面向对象的三大特征:继承Inhe ...