C#如何HttpWebRequest模拟登陆,获取服务端返回Cookie以便登录请求后使用
2017-09-13 12:38
2046 查看
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(); }
相关文章推荐
- [转发]HttpWebRequest模拟登陆,存储Cookie以便登录请求后使用
- HttpWebRequest模拟登陆,存储Cookie以便登录请求后使用
- HttpWebRequest模拟登陆,存储Cookie以便登录请求后使用
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
- C# 利用HttpWebRequest模拟登陆获取数据设置Accept-Encoding为gzip,deflate后返回的网页是乱码处理
- C# : WebRequest发起Http Post请求模拟登陆并cookie处理示例
- 利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录
- 利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录
- vb.net如何使用HttpWebRequest模拟登陆带验证码的网站
- 【转】C# WebRequest发起Http Post请求模拟登陆并cookie处理示例
- 利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录
- 使用HttpWebRequest和HttpWebResponse实现模拟登录需要登陆后才可以访问的页面
- 使用C#的HttpWebRequest模拟登陆网站
- httpwebrequest 模拟登录 获取cookies 以前的代码,记录备忘!
- C#使用HttpWebRequest模拟登陆(附带验证码)
- c# 使用 HttpWebRequest模拟登陆(附带验证码)
- [ 转]c# 使用 HttpWebRequest模拟登陆(附带验证码)
- 用 c# 使用HttpWebRequest模拟登陆(带验证码)
- [C#]使用HttpWebRequest请求远端服务器时如何加载SSL证书
- c# 使用 HttpWebRequest模拟登陆(附带验证码)