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 ...
随机推荐
- leetcode easy problem set
*勿以浮沙筑高台* 持续更新........ 题目网址:https://leetcode.com/problemset/all/?difficulty=Easy 1. Two Sum [4m ...
- .Net中的IO
C#中的IO 本文环境为: Win 10 VS 2015 .net Framework 版本 4.0 File 类 File 类是一个工具类,可以用来判断文件是否存在和方便的创建文件, 读取.写入等操 ...
- [leetcode tree]104. Maximum Depth of Binary Tree
求树的最大深度 class Solution(object): def maxDepth(self, root): if not root: return 0 left = self.maxDepth ...
- [leetcode sort]75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- bzoj4668: 冷战 并查集按秩合并
题目链接 bzoj4668: 冷战 题解 按秩合并并查集,每次增长都是小集合倍数的两倍以上,层数不超过logn 查询路径最大值 LCT同解 代码 #include<bits/stdc++.h&g ...
- 2018 计蒜之道复赛 贝壳找房魔法师顾问(并查集+dfs判环)
贝壳找房在遥远的传奇境外,找到了一个强大的魔法师顾问.他有 22 串数量相同的法力水晶,每个法力水晶可能有不同的颜色.为了方便起见,可以将每串法力水晶视为一个长度不大于 10^5105,字符集不大于 ...
- hash课堂测试补分博客
题目要求: 开放地址法: 概念: 所谓的开放定址法就是一旦发生了冲突,就去寻找下一个空的散列地址,只要散列表足够大,空的散列地址总能找到,并将记录存入. 它的公式为: 解题过程(在下图中): 拉链法: ...
- Linux免密登录
ssh连接上服务器 ssh -p 端口 用户名@ip地址 获取本地的pub ssh key cd ~/.ssh vi id_rsa.pub 拷贝里面的内容 将拷贝的内容放到服务器的authorized ...
- bzoj 2460 拟阵+判线性相关
/************************************************************** Problem: 2460 User: idy002 Language: ...
- 更好的浏览器动画实现 requestAnimationFrame
requestAnimationFrame 是专门为实现高性能的帧动画而设计的一个API: js一般是借助setTimeout或setInterval这两个函数实现动画,性能不佳. css3动画,性能 ...