C#在WinFrom中Get/Post请求及QQ农场Cookie保存
2010-04-20 01:02
417 查看
本文地址:/article/4962742.html
注意Cookie的绑定,此处Cookie为获取验证码时记录的Cookie值。
注意Cookie的绑定,此处Cookie为获取验证码时记录的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保存
- C#在WinFrom程序实现Get和Post请求及QQ农场的Cookie保存
- Android 网页登录 POST 请求 保存 COOKIE
- 【转】C# WebRequest发起Http Post请求模拟登陆并cookie处理示例
- 对GET/POST请求返回cookie中的键值对进行重新组合
- Android 网页登录 POST 请求 保存 COOKIE
- Android 网页登录 POST 请求 保存 COOKIE
- Android 网页登录 POST 请求 保存 COOKIE
- C#源码刷新网页 最小化托盘http get和post请求配置保存版权时间限制定时调用 单实例运行,如果已经运行则激活窗口到最前显示
- C#发送Web请求(GET/POST)
- C# : WebRequest发起Http Post请求模拟登陆并cookie处理示例
- C#代码发送get/post请求的方法
- C# http Get/POST请求封装类
- 【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- Http中Get/Post请求区别
- C#利用HttpWebRequest进行post请求的示例(HTTPS)
- Java发送Get/Post请求
- 完整篇:python+fiddler抓包写接口请求:其中有cookie值和session ,post请求,xml数据格式
- Silverlight POST请求并返回数据后保存文件
- 向网络发送GET/POST请求参数