如何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,如需转载请自行联系原作者
 

相关文章
|
6月前
一个困扰几天的难题 cookie 登录
一个困扰几天的难题 cookie 登录
31 0
Nuxt中服务端请求无法获取LocalStorage和Cookie的解决办法!
Nuxt中服务端请求无法获取LocalStorage和Cookie的解决办法!
|
1月前
|
数据采集 存储 安全
登录态数据抓取:Python爬虫携带Cookie与Session的应用技巧
登录态数据抓取:Python爬虫携带Cookie与Session的应用技巧
|
3月前
|
存储 JSON 算法
登录认证-登录校验-会话技术方案选择和对比(cookie、session和JWT令牌)
登录认证-登录校验-会话技术方案选择和对比(cookie、session和JWT令牌)
|
3月前
|
存储 Web App开发 JavaScript
关于 HTTP 请求头部自动添加的 cookie 字段的逻辑
关于 HTTP 请求头部自动添加的 cookie 字段的逻辑
54 0
|
3月前
|
存储 安全
什么是 HTTP 请求的 Session cookie
什么是 HTTP 请求的 Session cookie
22 0
|
4月前
给requests请求添加cookie
给requests请求添加cookie
43 0
|
4月前
|
存储 移动开发 前端开发
HTTP请求中token、cookie、session有什么区别
HTTP请求中token、cookie、session有什么区别
144 0
|
5月前
利用cookie进行登录
利用cookie进行登录
45 0
|
8月前
|
JSON NoSQL Redis
cookie 免密登录了解一下
cookie 免密登录了解一下
cookie 免密登录了解一下