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模拟登陆(附带验证码) 分类: C# .net2010-06-04 00:50 35647人阅读 评论(43) 收藏 举报 c#exceptionstreams ...
- 使用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
之前的后天管理系统的电子面单打印使用的是灵通打单. 使用相对比较麻烦,需要到处Excel之后再导入,麻烦. 快递鸟有电子面单api,后台系统直接对接很是方便,不过也遇到了好些问题. 不难是不难,但是遇 ...
- python获取文件属性
提供三段代码 1.os模块 import os def TestFileAttributes(): # This function is platform indepedent. statinfo = ...
- 安装部署VMware vSphere 5.5文档 (6-6) 集群和vMotion
部署VMware vSphere 5.5 实施文档 ########################################################################## ...
- 使用IIS实现反向代理
IIS的反向代理是通过ARR模块来完成的,ARR模块需要另外安装,而且只能通过Web PlatForm Installer安装.关于安装来源与步骤,帖子已有很多,不做描述.启用“Application ...
- [POI2000]Repetitions
题目大意: 求多个字符串的LCS. 思路: 同SPOJ-LCS2,不过因为SPOJ上数据比较水,当时用错误的写法过掉了,这次用正确的写法重新过了一遍. 拓扑排序按照每个状态的len值,用计数排序实现. ...
- Git 初学者使用指南及Git 资源整理
Git 资源整理 Git is a free and open source distributed version control system designed to handle everyth ...
- vijos p1881 线段树
题意:点我 我就想问,现在换代码风格还来得及吗? 2015-05-19:线段树进一步加强,看来不用换风格了 维护左右节点左右端颜色和长度即可 #include<cstdio> #inclu ...
- 仅100行的JavaScript DOM操作类库
如果你构建过Web引用程序,你可能处理过很多DOM操作.访问和操作DOM元素几乎是每一个Web应用程序的通用需求.我们我们经常从不同的控件收集信息,我们需要设置value值,修改div或span标签的 ...
- POJ 2104 && POJ 2761 (静态区间第k大,主席树)
查询区间第K大,而且没有修改. 使用划分树是可以做的. 作为主席树的入门题,感觉太神奇了,Orz /* *********************************************** ...
- Iptables 指南 1.1.19
Iptables 指南 1.1.19 Oskar Andreasson oan@frozentux.net Copyright © 2001-2003 by Oskar Andreasson 本文在符 ...