C#在WinFrom程序实现Get和Post请求及QQ农场的Cookie保存
来源:互联网 发布:全国房地产数据公司 编辑:程序博客网 时间:2024/05/02 04:53
意Cookie的绑定,此处Cookie为获取验证码时记录的Cookie值。
以下是实现代码:
以上介绍的就是C#在WinFrom程序实现Get和Post请求及QQ农场的Cookie保存,希望对你有所帮助。#region 提交数据方法 // 公用Cookie信息 public static CookieContainer myCookie; //返回的Html代码或结果 public static string html; /// <summary> /// Get方式提交 /// </summary> /// <param name="strUrl">目的资源链接</param> /// <returns>String:Response返回值</returns> public static string GetModel(string strUrl, string server) { string strRet = null; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl); request.Timeout = 2000; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); System.IO.Stream resStream = response.GetResponseStream(); Encoding encode = System.Text.Encoding.UTF8; StreamReader readStream = new StreamReader(resStream, encode); Char[] read = new Char[256]; int count = readStream.Read(read, 0, 256); while (count > 0) { String str = new String(read, 0, count); strRet = strRet + str; count = readStream.Read(read, 0, 256); } resStream.Close(); } catch (Exception e) { strRet = ""; } return strRet; } /// <summary> /// 绑定Cookie、获取验证码 /// </summary> /// <param name="strUrl">链接地址(验证码获取地址)</param> /// <param name="server">Referer地址</param> /// <returns>Stream:验证码的数据流</returns> public static Stream GetModelStream(string strUrl, string server)//, string proxy, bool isProxy) { System.IO.Stream resStream = null; string cookie; try { HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl); httpRequest.Timeout = 2000; httpRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"; httpRequest.Referer = server; httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)"; HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse(); cookie = httpResponse.Headers.Get("Set-Cookie"); myCookie = new CookieContainer(); myCookie.SetCookies(new Uri(server), cookie); resStream = httpResponse.GetResponseStream(); } catch { } return resStream; } /// <summary> /// Post方法提交 /// </summary> /// <param name="strUrl">目的资源链接</param> /// <param name="strParm">传递的参数和值</param> /// <returns>String:Response返回值</returns> public static string PostModel(string strUrl, string server, string strParm) { Encoding encode = System.Text.Encoding.UTF8; byte[] arrB = encode.GetBytes(strParm); HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(strUrl); myReq.Method = "POST"; myReq.ContentType = "application/x-www-form-urlencoded"; myReq.Timeout = 2000; myReq.Referer = server; myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)"; myReq.ContentLength = arrB.Length; myReq.CookieContainer = myCookie; Stream outStream = myReq.GetRequestStream(); outStream.Write(arrB, 0, arrB.Length); outStream.Close(); WebResponse myResp = null; try { //接收HTTP做出的响应 myResp = myReq.GetResponse(); } catch (Exception e) { } Stream ReceiveStream = myResp.GetResponseStream(); StreamReader readStream = new StreamReader(ReceiveStream, encode); Char[] read = new Char[256]; int count = readStream.Read(read, 0, 256); string str = null; while (count > 0) { str += new String(read, 0, count); count = readStream.Read(read, 0, 256); } readStream.Close(); myResp.Close(); return str; } #endregion
- C#在WinFrom程序实现Get和Post请求及QQ农场的Cookie保存
- Cookie在Get请求和Post请求中的区别
- c#简单实现GET和POST请求
- PHP的curl实现get,post 和 cookie
- java实现:http协议get和post方法的url参数请求响应及交互
- C#发起GET和POST请求的主流方法
- C#实现http协议GET、POST请求
- C#实现http协议GET、POST请求
- C# 实现http协议GET、POST请求
- C# 实现HTTP GET POST请求
- C#实现http协议GET、POST请求
- C#实现http协议GET、POST请求
- GET和POST的区别及get和post关于请求的编解码的问题
- 在PHP中使用CURL实现GET和POST请求的方法
- 在PHP中使用CURL实现GET和POST请求的方法
- PHP curl实现get,post和cookie
- c# HttpWebRequest post和get请求http
- c# 发送GET 和 POST请求
- 2013-9-4 PMP错题记录(自测201-390)
- C# get post之 get 方式
- zoj-1093-Monkey and Banana
- 谷歌地图api v3学习之:起步
- hdu 1305 字典树 模板
- C#在WinFrom程序实现Get和Post请求及QQ农场的Cookie保存
- 配置EIGRP身份认证的步骤为
- magento获取商品信息
- vs2008做的项目转换成vs2005
- 重走Java路
- java项目中遇到的问题及解决办法002
- asterisk常用调试监测命令
- 一款经典简洁的CSS滑动门代码
- 关于 struts2 单例 实例的思考