使用C#發(fā)送Http請(qǐng)求實(shí)現(xiàn)模擬登陸實(shí)例
模擬登陸的原理很簡(jiǎn)單,就是發(fā)送一個(gè)Http 請(qǐng)求服務(wù)器獲得響應(yīng),然后客戶端獲取到cookie即可實(shí)現(xiàn)模擬登陸,比如一些搶票軟件的原理無(wú)非也是這樣模擬客戶端的cookie 然后發(fā)送請(qǐng)求去搶票,然后12306 本文將演示如何用C# 來(lái)實(shí)現(xiàn)模擬登陸的,推薦一款工具Fiddler,這是一款監(jiān)聽http 請(qǐng)求的利器。廢話不多說(shuō),我就以博客園為例來(lái)實(shí)現(xiàn)模擬登陸。首先我登陸博客園 http://passport.cnblogs.com/login.aspx 輸入用戶名和密碼點(diǎn)登陸 就會(huì)看到Fiddler 上的相關(guān)信息:
Ok,我首先需要發(fā)送一個(gè)http 請(qǐng)求 ,這個(gè)請(qǐng)求時(shí)POST的方式,然后用戶名和密碼就是POST的數(shù)據(jù)。代碼如下:
static CookieContainer GetCookie(string postString, string postUrl)
{
CookieContainer cookie = new CookieContainer();
HttpWebRequest httpRequset = (HttpWebRequest)HttpWebRequest.Create(postUrl);//創(chuàng)建http 請(qǐng)求
httpRequset.CookieContainer = cookie;//設(shè)置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";//以上信息在監(jiān)聽請(qǐng)求的時(shí)候都有的直接復(fù)制過(guò)來(lái)
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數(shù)據(jù)的寫入
HttpWebResponse httpResponse = (HttpWebResponse)httpRequset.GetResponse();//獲得 服務(wù)端響應(yīng)
return cookie;//拿到cookie
}
拿到cookie 之后我們就可以以用戶的什么去用戶的后臺(tái)或者其他的地方:
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 下面是調(diào)用 我寫的是一個(gè)控制臺(tái)程序:
static void Main(string[] args)
{
string loginstr = "{要post 的登陸數(shù)據(jù)包括用戶名和密碼}";
//從登陸的地址獲取cookie
CookieContainer cookie = GetCookie(loginstr, "http://passport.cnblogs.com/login.aspx");
//這個(gè)是進(jìn)入后臺(tái)地址
Console.WriteLine(GetContent(cookie, "http://i.cnblogs.com/EditPosts.aspx"));
Console.Read();
}
可以看到我已經(jīng)進(jìn)入了后臺(tái)了:
如果我是沒(méi)有登陸的情況下進(jìn)入這個(gè)地址是這樣的:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C#教程
下一篇:C#操作XML通用方法匯總
本文標(biāo)題:使用C#發(fā)送Http請(qǐng)求實(shí)現(xiàn)模擬登陸實(shí)例
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6229.html
您可能感興趣的文章
- 01-10C#使用Dispose模式實(shí)現(xiàn)手動(dòng)對(duì)資源的釋放
- 01-10C#3.0使用EventLog類寫Windows事件日志的方法
- 01-10C#使用windows服務(wù)開啟應(yīng)用程序的方法
- 01-10c# ArrayList的使用方法小總結(jié)
- 01-10C#使用ADO.Net部件來(lái)訪問(wèn)Access數(shù)據(jù)庫(kù)的方法
- 01-10C#使用Mutex簡(jiǎn)單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法
- 01-10使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
- 01-10http圖片上傳安全性問(wèn)題 根據(jù)ContentType (MIME) 判斷其實(shí)不準(zhǔn)確、不
- 01-10C#中yield用法使用說(shuō)明
- 01-10C#編程和Visual Studio使用技巧(下)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載


