HttpWebRequest、HttpWebResponse封装类源码.docx_第1页
HttpWebRequest、HttpWebResponse封装类源码.docx_第2页
HttpWebRequest、HttpWebResponse封装类源码.docx_第3页
HttpWebRequest、HttpWebResponse封装类源码.docx_第4页
HttpWebRequest、HttpWebResponse封装类源码.docx_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

HttpWebRequest、HttpWebResponse封装类 源码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Net;usingSystem.IO;usingSystem.Threading;namespacejaylekepublicclassHttpHelper#region 私有变量privatestaticCookieContainer cc=newCookieContainer();privatestaticstringcontentType = application/x-www-form-urlencoded;privatestaticstringaccept = image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*;privatestaticstringuserAgent = Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022);privatestaticEncoding encoding = Encoding.GetEncoding(utf-8);privatestaticintdelay = 3000;/延迟访问防止连续访问被发现privatestaticintmaxTry = 300;privatestaticintcurrentTry = 0;#endregion#region 属性/ / Cookie容器/ publicstaticCookieContainer CookieContainergetreturncc;/ / 获取网页源码时使用的编码/ / publicstaticEncoding Encodinggetreturnencoding;setencoding = value;publicstaticintNetworkDelaygetRandom r = newRandom();return(r.Next(delay / 1000, delay / 1000 * 2)*1000;setdelay = value;publicstaticintMaxTrygetreturnmaxTry;setmaxTry = value;#endregion#region 公共方法/ / 获取指定页面的HTML代码/ / 指定页面的路径/ 回发的数据/ 是否以post方式发送请求/ Cookie集合/ publicstaticstringGetHtml(stringurl, stringpostData, boolisPost, CookieContainer cookieContainer)if(string.IsNullOrEmpty(postData)returnGetHtml(url, cookieContainer);Thread.Sleep(NetworkDelay);/延迟访问currentTry+;HttpWebRequest httpWebRequest=null;HttpWebResponse httpWebResponse=null;trybyte byteRequest = Encoding.Default.GetBytes(postData);httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);httpWebRequest.CookieContainer = cookieContainer;httpWebRequest.ContentType = contentType;httpWebRequest.ServicePoint.ConnectionLimit = maxTry;httpWebRequest.Referer = url;httpWebRequest.Accept = accept;httpWebRequest.UserAgent = userAgent;httpWebRequest.Method = isPost ? POST: GET;httpWebRequest.ContentLength = byteRequest.Length;Stream stream = httpWebRequest.GetRequestStream();stream.Write(byteRequest, 0, byteRequest.Length);stream.Close();httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();Stream responseStream = httpWebResponse.GetResponseStream();StreamReader streamReader = newStreamReader(responseStream, encoding);stringhtml = streamReader.ReadToEnd();streamReader.Close();responseStream.Close();currentTry = 0;httpWebRequest.Abort();httpWebResponse.Close();returnhtml;catch(Exception e)Console.ForegroundColor = ConsoleColor.Red;Console.WriteLine(DateTime.Now.ToString(HH:mm:ss ) + e.Message);Console.ForegroundColor = ConsoleColor.White;if(currentTry = maxTry)GetHtml(url, postData, isPost, cookieContainer);currentTry-;if(httpWebRequest!=null)httpWebRequest.Abort();if(httpWebResponse!=null)httpWebResponse.Close();returnstring.Empty;/ / 获取指定页面的HTML代码/ / 指定页面的路径/ Cookie集合/ publicstaticstringGetHtml(stringurl, CookieContainer cookieContainer)Thread.Sleep(NetworkDelay);currentTry+;HttpWebRequest httpWebRequest=null;HttpWebResponse httpWebResponse=null;tryhttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);httpWebRequest.CookieContainer = cookieContainer;httpWebRequest.ContentType = contentType;httpWebRequest.ServicePoint.ConnectionLimit = maxTry;httpWebRequest.Referer = url;httpWebRequest.Accept = accept;httpWebRequest.UserAgent = userAgent;httpWebRequest.Method = GET;httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();Stream responseStream = httpWebResponse.GetResponseStream();StreamReader streamReader = newStreamReader(responseStream, encoding);stringhtml = streamReader.ReadToEnd();streamReader.Close();responseStream.Close();currentTry-;httpWebRequest.Abort();httpWebResponse.Close();returnhtml;catch(Exception e)Console.ForegroundColor = ConsoleColor.Red;Console.WriteLine(DateTime.Now.ToString(HH:mm:ss ) + e.Message);Console.ForegroundColor = ConsoleColor.White;if(currentTry = maxTry)GetHtml(url, cookieContainer);currentTry-;if(httpWebRequest!=null)httpWebRequest.Abort();if(httpWebResponse!=null)httpWebResponse.Close();returnstring.Empty;/ / 获取指定页面的HTML代码/ / 指定页面的路径/ publicstaticstringGetHtml(stringurl)returnGetHtml(url, cc);/ / 获取指定页面的HTML代码/ / 指定页面的路径/ 回发的数据/ 是否以post方式发送请求/ publicstaticstringGetHtml(stringurl, stringpostData, boolisPost)returnGetHtml(url, postData, isPost, cc);/ / 获取指定页面的Stream/ / 指定页面的路径/ 回发的数据/ 是否以post方式发送请求/ Cookie集合/ publicstaticStream GetStream(stringurl, CookieContainer cookieContainer)/Thread.Sleep(delay);currentTry+;HttpWebRequest httpWebRequest=null;HttpWebResponse httpWebResponse=null;tryhttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);httpWebRequest.CookieContainer = cookieContainer;httpWebRequest.ContentType = contentType;httpWebRequest.ServicePoint.ConnectionLimit = maxTry;httpWebRequest.Referer = url;httpWebRequest.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论