webreqeust/webresponse抓取URL信息

一般情况下,如果无特殊的需要,对于抓去网页数据,可以使用WebClient,也可以使用webrequest/webresponse这对组合,不过我使用webrequest/webresponse的时候比较多一些,所以,自己整理了一个网页抓取类,上代码:


 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
  1ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
  2    /// Web下载工具
  3    /// </summary>

  4    public class WebDownload : IDisposable
  5ExpandedBlockStart.gifContractedBlock.gif    {
  6ContractedSubBlock.gifExpandedSubBlockStart.gif        事件#region 事件
  7
  8ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
  9        /// 下载完成事件
 10        /// </summary>

 11        //public event EventHandler DownloadComplete;
 12
 13        #endregion

 14
 15ContractedSubBlock.gifExpandedSubBlockStart.gif        字段#region 字段
 16ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 17        /// 请求对象
 18        /// </summary>

 19        private HttpWebRequest m_request;
 20
 21ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 22        /// 请求返回对象
 23        /// </summary>

 24        private HttpWebResponse m_response;
 25
 26ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 27        /// 请求URL
 28        /// </summary>

 29        public string Url = string.Empty;
 30
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 32        /// Method
 33        /// </summary>

 34        public string Method = string.Empty;
 35
 36ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 37        /// 数据类型
 38        /// </summary>

 39        public string ContentType = string.Empty;
 40
 41ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 42        /// referer
 43        /// </summary>

 44        public string Referer = string.Empty;
 45
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 47        /// 是否允许重定向
 48        /// </summary>

 49        public bool AllowAutoRedirect = true;
 50
 51ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 52        /// accept
 53        /// </summary>

 54        public string Accept = string.Empty;
 55
 56ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 57        /// 提交的数据
 58        /// </summary>

 59        public string PostData = string.Empty;
 60
 61ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 62        /// cookies
 63        /// </summary>

 64        public CookieContainer Cookies = null;
 65
 66ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 67        /// 连接超时时间
 68        /// </summary>

 69        public int Timeout = 100000;
 70
 71ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 72        /// 编码类型
 73        /// </summary>

 74        public Encoding Encode = Encoding.Default;
 75
 76ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 77        /// useragent
 78        /// </summary>

 79        public string UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
 80
 81ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 82        /// 请求返回的数据流
 83        /// </summary>

 84        //public Stream ReceiveData = null;
 85        #endregion

 86
 87ContractedSubBlock.gifExpandedSubBlockStart.gif        构造函数#region 构造函数
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 89        /// 下载工具
 90        /// </summary>
 91        /// <param name="url"></param>
 92        /// <param name="method"></param>

 93        public WebDownload(string url, string method) : this( url,method,string.Empty )
 94ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 95        }

 96
 97ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 98        /// 下载工具
 99        /// </summary>
100        /// <param name="url"></param>
101        /// <param name="method"></param>
102        /// <param name="postData"></param>

103        public WebDownload(string url, string method, string postData) : this( url,method,postData,"application/x-www-form-urlencoded" )
104ExpandedSubBlockStart.gifContractedSubBlock.gif        {
105        }

106
107ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
108        /// 下载工具
109        /// </summary>
110        /// <param name="url"></param>
111        /// <param name="method"></param>
112        /// <param name="postData"></param>
113        /// <param name="contentType"></param>

114        public WebDownload(string url, string method, string postData, string contentType) : this( url,method,postData,contentType,"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-silverlight, */*" )
115ExpandedSubBlockStart.gifContractedSubBlock.gif        {
116        }

117
118ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
119        /// 下载工具
120        /// </summary>
121        /// <param name="url"></param>
122        /// <param name="method"></param>
123        /// <param name="postData"></param>
124        /// <param name="contentType"></param>
125        /// <param name="accept"></param>

126        public WebDownload(string url, string method, string postData, string contentType, string accept) : this( url,method,postData,contentType,accept,string.Empty )
127ExpandedSubBlockStart.gifContractedSubBlock.gif        {
128        }

129
130ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
131        /// 下载工具
132        /// </summary>
133        /// <param name="url"></param>
134        /// <param name="method"></param>
135        /// <param name="postData"></param>
136        /// <param name="contentType"></param>
137        /// <param name="accept"></param>
138        /// <param name="referer"></param>

139        public WebDownload(string url, string method, string postData, string contentType, string accept,string referer) : this( url,method,postData,contentType,accept,referer,true )
140ExpandedSubBlockStart.gifContractedSubBlock.gif        {
141        }

142
143ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
144        /// 下载工具
145        /// </summary>
146        /// <param name="url"></param>
147        /// <param name="method"></param>
148        /// <param name="postData"></param>
149        /// <param name="contentType"></param>
150        /// <param name="accept"></param>
151        /// <param name="referer"></param>
152        /// <param name="autoredirect"></param>

153        public WebDownload(string url, string method, string postData, string contentType, string accept, string referer,bool autoredirect)
154ExpandedSubBlockStart.gifContractedSubBlock.gif        {
155            Url = url;
156            Method = method;
157            PostData = postData;
158            ContentType = contentType;
159            Accept = accept;
160            Referer = referer;
161            AllowAutoRedirect = autoredirect;
162        }

163        #endregion

164
165ContractedSubBlock.gifExpandedSubBlockStart.gif        方法#region 方法
166
167ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
168        /// 下载数据
169        /// </summary>

170        public Stream Download()
171ExpandedSubBlockStart.gifContractedSubBlock.gif        {
172            m_request = (HttpWebRequest)WebRequest.Create(Url);
173
174            //创建cookies容器
175            CookieContainer cc = null;
176            if (Cookies != null)
177                cc = Cookies;
178            else
179                cc = new CookieContainer();
180            m_request.ContentType = ContentType;
181            m_request.UserAgent = UserAgent;
182            m_request.Referer = Referer;
183            m_request.AllowAutoRedirect = AllowAutoRedirect;
184            m_request.Accept = Accept;
185            m_request.Timeout = Timeout;
186            m_request.Method = Method;
187
188            //检查是否有提交数据,只有post才可提交数据
189            if (PostData.Length > 0 && Method == "POST")
190ExpandedSubBlockStart.gifContractedSubBlock.gif            {
191                byte[] bytes = Encode.GetBytes(PostData);
192                Stream postStream = m_request.GetRequestStream();
193                postStream.Write(bytes, 0, bytes.Length);
194                postStream.Close();
195            }

196
197            try
198ExpandedSubBlockStart.gifContractedSubBlock.gif            {
199                m_response = (HttpWebResponse)m_request.GetResponse();
200            }

201            catch (WebException ex)
202ExpandedSubBlockStart.gifContractedSubBlock.gif            {
203                if (ex.Response != null)
204ExpandedSubBlockStart.gifContractedSubBlock.gif                {
205                    m_response = (HttpWebResponse)ex.Response;
206                }

207                else
208ExpandedSubBlockStart.gifContractedSubBlock.gif                {
209                    throw ex;
210                }

211            }

212            return m_response.GetResponseStream();
213            //if (DownloadComplete != null)
214            //{
215            //    DownloadComplete(this, EventArgs.Empty);
216            //}
217        }

218
219        #endregion

220
221ContractedSubBlock.gifExpandedSubBlockStart.gif        IDisposable 成员#region IDisposable 成员
222
223        public void Dispose()
224ExpandedSubBlockStart.gifContractedSubBlock.gif        {
225            if (m_response != null)
226                m_response.Close();
227        }

228
229        #endregion

230    }

转载于:https://www.cnblogs.com/land/archive/2009/04/28/1445043.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值