使用C#发送Http 请求实现模拟登陆(以博客园为例)
2015-01-09 14:31
766 查看
模拟登陆的原理很简单,就是发送一个Http 请求服务器获得响应,然后客户端获取到cookie即可实现模拟登陆,比如一些抢票软件的原理无非也是这样模拟客户端的cookie 然后发送请求去抢票。 本文将演示如何用C# 来实现模拟登陆的,推荐一款工具Fiddler,这是一款监听http 请求的利器。废话不多说,我就以博客园为例来实现模拟登陆。首先我登陆博客园 http://passport.cnblogs.com/login.aspx 输入用户名和密码点登陆 就会看到Fiddler 上的相关信息:
Ok,我首先需要发送一个http 请求 ,这个请求时POST的方式,然后用户名和密码就是POST的数据。代码如下:
拿到cookie 之后我们就可以以用户的什么去用户的后台或者其他的地方:
OK 下面是调用 我写的是一个控制台程序:
可以看到我已经进入了后台了:
如果我是没有登陆的情况下进入这个地址是这样的:
下次我就写一下怎么在模拟登陆之后发送http 请求实现添加删除这些效果。
Ok,我首先需要发送一个http 请求 ,这个请求时POST的方式,然后用户名和密码就是POST的数据。代码如下:
static CookieContainer GetCookie(string postString, string postUrl) { CookieContainer cookie = new CookieContainer(); HttpWebRequest httpRequset = (HttpWebRequest)HttpWebRequest.Create(postUrl);//创建http 请求 httpRequset.CookieContainer = cookie;//设置cookie httpRequset.Method = "POST";//POST 提交 httpRequset.KeepAlive = true; httpRequset.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko"; httpRequset.Accept = "text/html, application/xhtml+xml, */*"; httpRequset.ContentType = "application/x-www-form-urlencoded";//以上信息在监听请求的时候都有的直接复制过来 byte[] bytes = System.Text.Encoding.UTF8.GetBytes(postString); httpRequset.ContentLength = bytes.Length; Stream stream = httpRequset.GetRequestStream(); stream.Write(bytes, 0, bytes.Length); stream.Close();//以上是POST数据的写入 HttpWebResponse httpResponse = (HttpWebResponse)httpRequset.GetResponse();//获得 服务端响应 return cookie;//拿到cookie }
拿到cookie 之后我们就可以以用户的什么去用户的后台或者其他的地方:
static string GetContent(CookieContainer cookie, string url) { string content; HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(url); httpRequest.CookieContainer = cookie; httpRequest.Referer = url; httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko"; httpRequest.Accept = "text/html, application/xhtml+xml, */*"; httpRequest.ContentType = "application/x-www-form-urlencoded"; httpRequest.Method = "GET"; HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse(); using (Stream responsestream = httpResponse.GetResponseStream()) { using (StreamReader sr = new StreamReader(responsestream, System.Text.Encoding.UTF8)) { content = sr.ReadToEnd(); } } return content; }
OK 下面是调用 我写的是一个控制台程序:
1 static void Main(string[] args) 2 { 3 string loginstr = "{要post 的登陆数据包括用户名和密码}"; 4 5 //从登陆的地址获取cookie 6 CookieContainer cookie = GetCookie(loginstr, "http://passport.cnblogs.com/login.aspx"); 7 8 //这个是进入后台地址 9 Console.WriteLine(GetContent(cookie, "http://i.cnblogs.com/EditPosts.aspx")); 10 11 Console.Read(); 12 }
可以看到我已经进入了后台了:
如果我是没有登陆的情况下进入这个地址是这样的:
下次我就写一下怎么在模拟登陆之后发送http 请求实现添加删除这些效果。
相关文章推荐
- 使用C#发送Http 请求实现模拟登陆(以博客园为例)
- 使用C#发送Http 请求实现模拟登陆
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- c#使用Socket发送HTTP/HTTPS请求的实现代码
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- C#HttpClient或使用CookieContainer模拟登陆后HttpRequest不发送cookie的解决方法及原因
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- [导入]C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- c#第一篇 在WPF的window窗体中使用httpwebrequest实现模拟登陆网页,并在webbroser控件中显示
- 使用C#发送Http请求实现模拟登陆实例
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- 使用C#发送Http请求实现模拟登陆实例
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- 使用C#的HttpWebRequest模拟登陆网站
- 通过HttpWebRequest 发送 POST 请求实现自动登陆
- C# : WebRequest发起Http Post请求模拟登陆并cookie处理示例
- c# 使用 HttpWebRequest模拟登陆(附带验证码)