HttpWebRequest、HttpWebResponse封装类 源码

代码
using System;

002 using System.Collections.Generic;

003 using System.Text;

004 using System.Net;

005 using System.IO;

006 using System.Threading;

007

008 namespace jayleke

009 {

010 public class HttpHelper

011 {

012 #region 私有变量

013 private static CookieContainer cc=new CookieContainer();

014 private static string contentType = "application/x-www-form-urlencoded";

015 private static string accept = "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, */*";

016 private static string userAgent = "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)";

017 private static Encoding encoding = Encoding.GetEncoding("utf-8");

018 private static int delay = 3000;//延迟访问防止连续访问被发现

019 private static int maxTry = 300;

020 private static int currentTry = 0;

021 #endregion

022

023 #region 属性

024 /// <summary></summary>

025 /// Cookie容器

026 ///

027 public static CookieContainer CookieContainer

028 {

029 get

030 {

031 return cc;

032 }

033 }

034

035 /// <summary></summary>

036 /// 获取网页源码时使用的编码

037 ///

038 /// <value></value>

039 public static Encoding Encoding

040 {

041 get

042 {

043 return encoding;

044 }

045 set

046 {

047 encoding = value;

048 }

049 }

050

051 public static int NetworkDelay

052 {

053 get

054 {

055 Random r = new Random();

056 return (r.Next(delay / 1000, delay / 1000 * 2))*1000;

057 }

058 set

059 {

060 delay = value;

061 }

062 }

063

064 public static int MaxTry

065 {

066 get

067 {

068 return maxTry;

069 }

070 set

071 {

072 maxTry = value;

073 }

074 }

075 #endregion

076

077 #region 公共方法

078 /// <summary></summary>

079 /// 获取指定页面的HTML代码

080 ///

081 /// <param name="url">指定页面的路径

082 /// <param name="postData">回发的数据

083 /// <param name="isPost">是否以post方式发送请求

084 /// <param name="cookieCollection">Cookie集合

085 /// <returns></returns>

086 public static string GetHtml(string url, string postData, bool isPost, CookieContainer cookieContainer)

087 {

088 if (string.IsNullOrEmpty(postData))

089 {

090 return GetHtml(url, cookieContainer);

091 }

092

093 Thread.Sleep(NetworkDelay);//延迟访问

094

095 currentTry++;

096

097 HttpWebRequest httpWebRequest=null;

098 HttpWebResponse httpWebResponse=null;

099 try

100 {

101 byte[] byteRequest = Encoding.Default.GetBytes(postData);

102

103 httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);

104 httpWebRequest.CookieContainer = cookieContainer;

105 httpWebRequest.ContentType = contentType;

106 httpWebRequest.ServicePoint.ConnectionLimit = maxTry;

107 httpWebRequest.Referer = url;

108 httpWebRequest.Accept = accept;

109 httpWebRequest.UserAgent = userAgent;

110 httpWebRequest.Method = isPost ? "POST" : "GET";

111 httpWebRequest.ContentLength = byteRequest.Length;

112

113 Stream stream = httpWebRequest.GetRequestStream();

114 stream.Write(byteRequest, 0, byteRequest.Length);

115 stream.Close();

116

117 httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

118 Stream responseStream = httpWebResponse.GetResponseStream();

119 StreamReader streamReader = new StreamReader(responseStream, encoding);

120 string html = streamReader.ReadToEnd();

121 streamReader.Close();

122 responseStream.Close();

123 currentTry = 0;

124

125 httpWebRequest.Abort();

126 httpWebResponse.Close();

127

128 return html;

129 }

130 catch (Exception e)

131 {

132 Console.ForegroundColor = ConsoleColor.Red;

133 Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);

134 Console.ForegroundColor = ConsoleColor.White;

135

136 if (currentTry <= maxTry)

137 {

138 GetHtml(url, postData, isPost, cookieContainer);

139 }

140 currentTry--;

141

142 if(httpWebRequest!=null){

143 httpWebRequest.Abort();

144 }if(httpWebResponse!=null){

145 httpWebResponse.Close();

146 }

147 return string.Empty;

148 }

149 }

150

151 /// <summary></summary>

152 /// 获取指定页面的HTML代码

153 ///

154 /// <param name="url">指定页面的路径

155 /// <param name="cookieCollection">Cookie集合

156 /// <returns></returns>

157 public static string GetHtml(string url, CookieContainer cookieContainer)

158 {

159 Thread.Sleep(NetworkDelay);

160

161 currentTry++;

162 HttpWebRequest httpWebRequest=null;

163 HttpWebResponse httpWebResponse=null;

164 try

165 {

166

167 httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);

168 httpWebRequest.CookieContainer = cookieContainer;

169 httpWebRequest.ContentType = contentType;

170 httpWebRequest.ServicePoint.ConnectionLimit = maxTry;

171 httpWebRequest.Referer = url;

172 httpWebRequest.Accept = accept;

173 httpWebRequest.UserAgent = userAgent;

174 httpWebRequest.Method = "GET";

175

176 httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

177 Stream responseStream = httpWebResponse.GetResponseStream();

178 StreamReader streamReader = new StreamReader(responseStream, encoding);

179 string html = streamReader.ReadToEnd();

180 streamReader.Close();

181 responseStream.Close();

182

183 currentTry--;

184

185 httpWebRequest.Abort();

186 httpWebResponse.Close();

187

188 return html;

189 }

190 catch (Exception e)

191 {

192 Console.ForegroundColor = ConsoleColor.Red;

193 Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);

194 Console.ForegroundColor = ConsoleColor.White;

195

196 if (currentTry <= maxTry)

197 {

198 GetHtml(url, cookieContainer);

199 }

200

201 currentTry--;

202

203 if(httpWebRequest!=null){

204 httpWebRequest.Abort();

205 }if(httpWebResponse!=null){

206 httpWebResponse.Close();

207 }

208 return string.Empty;

209 }

210 }

211

212 /// <summary></summary>

213 /// 获取指定页面的HTML代码

214 ///

215 /// <param name="url">指定页面的路径

216 /// <returns></returns>

217 public static string GetHtml(string url)

218 {

219 return GetHtml(url, cc);

220 }

221

222 /// <summary></summary>

223 /// 获取指定页面的HTML代码

224 ///

225 /// <param name="url">指定页面的路径

226 /// <param name="postData">回发的数据

227 /// <param name="isPost">是否以post方式发送请求

228 /// <returns></returns>

229 public static string GetHtml(string url, string postData, bool isPost)

230 {

231 return GetHtml(url, postData, isPost, cc);

232 }

233

234 /// <summary></summary>

235 /// 获取指定页面的Stream

236 ///

237 /// <param name="url">指定页面的路径

238 /// <param name="postData">回发的数据

239 /// <param name="isPost">是否以post方式发送请求

240 /// <param name="cookieCollection">Cookie集合

241 /// <returns></returns>

242 public static Stream GetStream(string url, CookieContainer cookieContainer)

243 {

244 //Thread.Sleep(delay);

245

246 currentTry++;

247 HttpWebRequest httpWebRequest=null;

248 HttpWebResponse httpWebResponse=null;

249

250 try

251 {

252

253 httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);

254 httpWebRequest.CookieContainer = cookieContainer;

255 httpWebRequest.ContentType = contentType;

256 httpWebRequest.ServicePoint.ConnectionLimit = maxTry;

257 httpWebRequest.Referer = url;

258 httpWebRequest.Accept = accept;

259 httpWebRequest.UserAgent = userAgent;

260 httpWebRequest.Method = "GET";

261

262 httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

263 Stream responseStream= httpWebResponse.GetResponseStream();

264 currentTry--;

265

266 //httpWebRequest.Abort();

267 //httpWebResponse.Close();

268

269 return responseStream;

270 }

271 catch (Exception e)

272 {

273 Console.ForegroundColor = ConsoleColor.Red;

274 Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);

275 Console.ForegroundColor = ConsoleColor.White;

276

277 if (currentTry <= maxTry)

278 {

279 GetHtml(url, cookieContainer);

280 }

281

282 currentTry--;

283

284 if(httpWebRequest!=null){

285 httpWebRequest.Abort();

286 }if(httpWebResponse!=null){

287 httpWebResponse.Close();

288 }

289 return null;

290 }

291 }

292

293 #endregion

294 }

295 }

 

posted @ 2010-08-19 16:59  lcqjiyi  阅读(633)  评论(0)    收藏  举报