使用C#发送Http 请求实现模拟登陆
来源:互联网 发布:淘宝怎么申请授权书 编辑:程序博客网 时间:2024/06/06 07:35
使用C#发送Http 请求实现模拟登陆
模拟登陆的原理很简单,就是发送一个Http 请求服务器获得响应,然后客户端获取到cookie即可实现模拟登陆,比如一些抢票软件的原理无非也是这样模拟客户端的cookie 然后发送请求去抢票,然后12306 本文将演示如何用C# 来实现模拟登陆的,推荐一款工具Fiddler,这是一款监听http 请求的利器。废话不多说,我就以博客园为例来实现模拟登陆。首先我登陆博客园 http://passport.cnblogs.com/login.aspx 输入用户名和密码点登陆 就会看到Fiddler 上的相关信息:
Ok,我首先需要发送一个http 请求 ,这个请求时POST的方式,然后用户名和密码就是POST的数据。代码如下:
1 static CookieContainer GetCookie(string postString, string postUrl) 2 { 3 4 CookieContainer cookie = new CookieContainer(); 5 6 HttpWebRequest httpRequset = (HttpWebRequest)HttpWebRequest.Create(postUrl);//创建http 请求 7 httpRequset.CookieContainer = cookie;//设置cookie 8 httpRequset.Method = "POST";//POST 提交 9 httpRequset.KeepAlive = true;10 httpRequset.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko";11 httpRequset.Accept = "text/html, application/xhtml+xml, */*";12 httpRequset.ContentType = "application/x-www-form-urlencoded";//以上信息在监听请求的时候都有的直接复制过来13 byte[] bytes = System.Text.Encoding.UTF8.GetBytes(postString);14 httpRequset.ContentLength = bytes.Length;15 Stream stream = httpRequset.GetRequestStream();16 stream.Write(bytes, 0, bytes.Length);17 stream.Close();//以上是POST数据的写入18 19 HttpWebResponse httpResponse = (HttpWebResponse)httpRequset.GetResponse();//获得 服务端响应20 return cookie;//拿到cookie21 }
拿到cookie 之后我们就可以以用户的什么去用户的后台或者其他的地方:
1 static string GetContent(CookieContainer cookie, string url) 2 { 3 string content; 4 HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(url); 5 httpRequest.CookieContainer = cookie; 6 httpRequest.Referer = url; 7 httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko"; 8 httpRequest.Accept = "text/html, application/xhtml+xml, */*"; 9 httpRequest.ContentType = "application/x-www-form-urlencoded";10 httpRequest.Method = "GET";11 12 HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();13 14 using (Stream responsestream = httpResponse.GetResponseStream())15 {16 17 using (StreamReader sr = new StreamReader(responsestream, System.Text.Encoding.UTF8))18 {19 content = sr.ReadToEnd();20 }21 }22 23 return content;24 }
OK 下面是调用 我写的是一个控制台程序:
1 1 static void Main(string[] args) 2 2 { 3 3 string loginstr = "{要post 的登陆数据包括用户名和密码}"; 4 4 5 5 //从登陆的地址获取cookie 6 6 CookieContainer cookie = GetCookie(loginstr, "http://passport.cnblogs.com/login.aspx"); 7 7 8 8 //这个是进入后台地址 9 9 Console.WriteLine(GetContent(cookie, "http://i.cnblogs.com/EditPosts.aspx"));10 10 11 11 Console.Read();12 12 }
可以看到我已经进入了后台了:
如果我是没有登陆的情况下进入这个地址是这样的:
下次我就写一下怎么在模拟登陆之后发送http 请求实现添加删除这些效果。
0 0
- 使用C#发送Http 请求实现模拟登陆
- 使用C#发送Http请求实现模拟登陆实例
- 模拟发送http请求
- C#模拟http 发送post请求 模拟登录
- C# 模拟发送Http请求获取请求页面
- c# 使用HttpWebRequest发送http请求
- PostMan使用模拟http请求 发送xml报文请求
- C#发送HTTP请求
- C#发送 http请求
- C#发送http请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟发送http get、post请求的方式
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- OpenGL学习笔记1:环境配置和基本流程
- 12
- Linux-----系统安装【一】【挂载点配置】
- Android_IPC机制知识梳理
- 【随手记】 安卓开发 界面屏蔽虚拟按键
- 使用C#发送Http 请求实现模拟登陆
- textview旋转文字
- 412. Fizz Buzz
- Android中的序列化
- c#小技巧总结
- UIButton,UILabel文字旋转(倾斜)
- python数据类型
- makefile中的@ $ :
- java - 四种强弱软虚引用使用到的场景