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 ...
随机推荐
- Python第一天——入门Python(2)字符串的简单操作
数据的操作 字符串的一些常用操作: 1 1 #!/usr/bin/env python 2 # #coding=utf-8 3 # 4 # test='hello world' 5 # print(t ...
- LightOJ 1030 Discovering Gold
期望,$dp$. 设$ans[i]$为$i$为起点,到终点$n$获得的期望金币值.$ans[i]=(ans[i+1]+ans[i+2]+ans[i+3]+ans[i+4]+ans[i+5]+ans[i ...
- NG2入门 - 架构
AngularJS2 学习 继TypeScript之后,终于到了ng2的学习路程,同样学习根据angular官网文档进行,对文档中的内容根据自己的理解略有改动.看官可看官网文档,也可以看本系列博文 首 ...
- servlet笔记
开发servlet有三种方法: (1) 实现 Servlet接口 (2) 通过继承 GenericServlet (3) 通过继承 HttpServlet get提交和post提交的 ...
- 【LeetCode】22. Generate Parentheses (I thought I know Python...)
I thought I know Python... Actually , I know nothing... 这个题真想让人背下来啊,每一句都很帅!!! Given n pairs of paren ...
- socket 心跳包机制
心跳包的发送,通常有两种技术 方法1:应用层自己实现的心跳包 由应用程序自己发送心跳包来检测连接是否正常,大致的方法是:服务器在一个 Timer事件中定时 向客户端发送一个短小精悍的数据包,然后启动 ...
- [SQL基础教程] 4-2 数据删除(DELETE)
[SQL基础教程] C4 数据更新 4-2 数据删除(DELETE) DROP TABLE / DELETE DROP TABLE - 完全删除表 DELETE - 仅删除数据,保留表容器 DELET ...
- 第17章 使用PHP和MySQL实现身份验证
1.对密码进行加密:sha1(string str,bool raw_output) //将返回一个40个字符的伪随机字符串,若raw_output为true,着得到一个20个字符的二进制字符串数据 ...
- 如何安装chrome扩展?比如json-handle插件如何安装
chrome插件安装方法: 方式一,在线安装 直接插到json-handle地址,添加即可 https://chrome.google.com/webstore/detail/json-handle/ ...
- java自动装箱的陷阱
自动装箱和拆箱是java的一颗语法糖,在给我们带来使用便利的同时也带来一些疑惑,请看下面的代码: public class TestClass { public static void main(St ...