如何HttpWebRequest模拟登陆,获取服务端返回Cookie以便登录请求后使用
生活随笔
收集整理的這篇文章主要介紹了
如何HttpWebRequest模拟登陆,获取服务端返回Cookie以便登录请求后使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public static string GetCookie(string requestUrlString, Encoding encoding, ref CookieContainer cookie) { //向服務端請求 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString); myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.CookieContainer = new CookieContainer(); //將請求的結果發送給客戶端(界面、應用) HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); cookie.Add(myResponse.Cookies); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), encoding); return reader.ReadToEnd(); } public static string GetHtml(string requestUrlString, Encoding encoding, CookieContainer cookie) { string ua = "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"; HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString); myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.UserAgent = ua; myRequest.CookieContainer = cookie; HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), encoding); return reader.ReadToEnd(); } public static string PostLogin(string postData, string requestUrlString, ref CookieContainer cookie) { ASCIIEncoding encoding = new ASCIIEncoding(); byte[] data = encoding.GetBytes(postData); //向服務端請求 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString); myRequest.Method = "POST"; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.ContentLength = data.Length; myRequest.CookieContainer = new CookieContainer(); Stream newStream = myRequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); //將請求的結果發送給客戶端(界面、應用) HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); cookie.Add(myResponse.Cookies); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); return reader.ReadToEnd(); } public static string PostRequest(string postData, string requestUrlString, CookieContainer cookie) { ASCIIEncoding encoding = new ASCIIEncoding(); byte[] data = encoding.GetBytes(postData); HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString); myRequest.Method = "POST"; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.ContentLength = data.Length; myRequest.CookieContainer = cookie; Stream newStream = myRequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); return reader.ReadToEnd(); }本文轉自黃聰博客園博客,原文鏈接:http://www.cnblogs.com/huangcong/p/7514327.html,如需轉載請自行聯系原作者
?
總結
以上是生活随笔為你收集整理的如何HttpWebRequest模拟登陆,获取服务端返回Cookie以便登录请求后使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (2)通信中为什么要进行AMC?
- 下一篇: 大数据泄露你的行踪?隐私不再是隐私