国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

使用C#發送Http請求實現模擬登陸實例

2020-01-24 00:58:04
字體:
來源:轉載
供稿:網友

模擬登陸的原理很簡單,就是發送一個Http 請求服務器獲得響應,然后客戶端獲取到cookie即可實現模擬登陸,比如一些搶票軟件的原理無非也是這樣模擬客戶端的cookie 然后發送請求去搶票,然后12306 本文將演示如何用C# 來實現模擬登陸的,推薦一款工具Fiddler,這是一款監聽http 請求的利器。廢話不多說,我就以博客園為例來實現模擬登陸。首先我登陸博客園 http://passport.cnblogs.com/login.aspx 輸入用戶名和密碼點登陸 就會看到Fiddler 上的相關信息:

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;//設置cookiehttpRequset.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 下面是調用 我寫的是一個控制臺程序:

static void Main(string[] args){string loginstr = "{要post 的登陸數據包括用戶名和密碼}";//從登陸的地址獲取cookieCookieContainer cookie = GetCookie(loginstr, "http://passport.cnblogs.com/login.aspx"); //這個是進入后臺地址 Console.WriteLine(GetContent(cookie, "http://i.cnblogs.com/EditPosts.aspx")); Console.Read();}

可以看到我已經進入了后臺了:

如果我是沒有登陸的情況下進入這個地址是這樣的:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东光县| 报价| 郁南县| 保亭| 武鸣县| 博兴县| 囊谦县| 竹山县| 抚州市| 巴彦县| 原平市| 古浪县| 建平县| 安平县| 科技| 拉萨市| 梁山县| 汶川县| 囊谦县| 班戈县| 福贡县| 江孜县| 潼关县| 蓬安县| 建水县| 沙湾县| 上犹县| 屯留县| 南部县| 天长市| 安康市| 湾仔区| 铜山县| 米易县| 屯门区| 谷城县| 盘山县| 昔阳县| 勐海县| 象山县| 东乡族自治县|