HttpWebRequest模拟登陆,存储Cookie以便登录请求后使用
2013-12-23 20:55
766 查看
[一篮饭特稀原创,转载请注明出自/article/6222987.html]
PostLogin :登录,并保存Cookie
PostRequest :登录后使用Cookie进行其他操作
e.g.
PostLogin :登录,并保存Cookie
1 public static string PostLogin(string postData, string requestUrlString, ref CookieContainer cookie) 2 { 3 ASCIIEncoding encoding = new ASCIIEncoding(); 4 byte[] data = encoding.GetBytes(postData); 5 //向服务端请求 6 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString); 7 myRequest.Method = "POST"; 8 myRequest.ContentType = "application/x-www-form-urlencoded"; 9 myRequest.ContentLength = data.Length; 10 myRequest.CookieContainer = new CookieContainer(); 11 Stream newStream = myRequest.GetRequestStream(); 12 newStream.Write(data, 0, data.Length); 13 newStream.Close(); 14 //将请求的结果发送给客户端(界面、应用) 15 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); 16 cookie.Add(myResponse.Cookies); 17 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); 18 return reader.ReadToEnd(); 19 }
PostRequest :登录后使用Cookie进行其他操作
1 public static string PostRequest(string postData, string requestUrlString, CookieContainer cookie) 2 { 3 ASCIIEncoding encoding = new ASCIIEncoding(); 4 byte[] data = encoding.GetBytes(postData); 5 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString); 6 myRequest.Method = "POST"; 7 myRequest.ContentType = "application/x-www-form-urlencoded"; 8 myRequest.ContentLength = data.Length; 9 myRequest.CookieContainer = cookie; 10 Stream newStream = myRequest.GetRequestStream(); 11 newStream.Write(data, 0, data.Length); 12 newStream.Close(); 13 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); 14 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); 15 return reader.ReadToEnd(); 16 }
e.g.
1 string strIMSPhone = tb_IMSPhone.Text.Trim(); 2 string strIMSPwd = tb_IMSPwd.Text.Trim(); 3 string postData = "username=" + strIMSPhone + "&password=" + strIMSPwd + "&type=2"; 4 CookieContainer cookie=new CookieContainer(); 5 if (IMSHelper.PostLogin(postData, post_signIn, ref cookie).Equals("ok")) 6 { 7 string strCont = PostRequest("", post_getContJsonData, cookie); 8 }
相关文章推荐
- [转发]HttpWebRequest模拟登陆,存储Cookie以便登录请求后使用
- HttpWebRequest模拟登陆,存储Cookie以便登录请求后使用
- C#如何HttpWebRequest模拟登陆,获取服务端返回Cookie以便登录请求后使用
- 【转】C# WebRequest发起Http Post请求模拟登陆并cookie处理示例
- 使用HttpWebRequest和HttpWebResponse实现模拟登录需要登陆后才可以访问的页面
- C# : WebRequest发起Http Post请求模拟登陆并cookie处理示例
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
- vb.net如何使用HttpWebRequest模拟登陆带验证码的网站
- CookieContainer模拟登陆存储Cookie以便二次登录用
- [ 转]c# 使用 HttpWebRequest模拟登陆(附带验证码)
- 【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- 利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录
- c# 使用 HttpWebRequest模拟登陆(附带验证码)
- 使用 HttpWebRequest 发送模拟 POST 请求
- 使用C#的HttpWebRequest模拟登陆网站
- 使用C#的HttpWebRequest模拟登陆网站
- 使用C#的HttpWebRequest模拟登陆网站
- c# 使用 HttpWebRequest模拟登陆(附带验证码)
- c# 使用 HttpWebRequest模拟登陆(附带验证码)
- c# 使用 HttpWebRequest模拟登陆(附带验证码)