HttpHelper 帮助类
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Net; 5 using System.Drawing; 6 using System.Collections; 7 using System.IO; 8 9 namespace Common 10 { 11 /// <summary> 12 /// 网络请求的简单封装 13 /// </summary> 14 public class HttpHelper 15 { 16 #region 私有变量 17 private CookieContainer cc; 18 private string contentType = "application/x-www-form-urlencoded; charset=UTF-8"; 19 private string accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; 20 private string userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5"; 21 private Encoding encoding = Encoding.GetEncoding("GB2312"); 22 private int connectTimeOutRetryTryMaxTimes = 3; 23 private int currentTry = 0; 24 #endregion 25 26 public string ContentType 27 { 28 get 29 { 30 return contentType; 31 } 32 set 33 { 34 contentType = value; 35 } 36 } 37 38 public string UserAgent 39 { 40 get 41 { 42 return userAgent; 43 } 44 set 45 { 46 userAgent = value; 47 } 48 } 49 50 public string Accept 51 { 52 get 53 { 54 return accept; 55 } 56 set 57 { 58 accept = value; 59 } 60 } 61 62 #region 属性 63 /// <summary> 64 /// Cookie容器 65 /// </summary> 66 public CookieContainer CookieContainer 67 { 68 get 69 { 70 return cc; 71 } 72 set 73 { 74 cc = value; 75 } 76 } 77 78 /// <summary> 79 /// 获取网页源码时使用的编码 80 /// </summary> 81 /// <value></value> 82 public Encoding Encoding 83 { 84 get 85 { 86 return encoding; 87 } 88 set 89 { 90 encoding = value; 91 } 92 } 93 94 /// <summary> 95 /// 连接超时的重试次数 96 /// </summary> 97 public int ConnectTimeOutRetryTryMaxTimes 98 { 99 get 100 { 101 return connectTimeOutRetryTryMaxTimes; 102 } 103 set 104 { 105 connectTimeOutRetryTryMaxTimes = value; 106 } 107 } 108 109 private IWebProxy webProxy; 110 /// <summary> 111 /// 网络代理 112 /// </summary> 113 public IWebProxy WebProxy 114 { 115 set 116 { 117 webProxy = value; 118 } 119 } 120 121 private Uri responseUri; 122 public Uri ResponseUri 123 { 124 get 125 { 126 return responseUri; 127 } 128 set 129 { 130 responseUri = value; 131 } 132 } 133 134 private string referer =string.Empty; 135 public string Referer 136 { 137 get 138 { 139 return referer; 140 } 141 set 142 { 143 referer = value; 144 } 145 } 146 #endregion 147 148 #region 构造函数 149 /// <summary> 150 /// Initializes a new instance of the <see cref="HttpHelper"/> class. 151 /// </summary> 152 public HttpHelper() 153 { 154 cc = new CookieContainer(); 155 } 156 157 /// <summary> 158 /// Initializes a new instance of the <see cref="HttpHelper"/> class. 159 /// </summary> 160 /// <param name="cc">The cc.</param> 161 public HttpHelper(CookieContainer cc) 162 { 163 this.cc = cc; 164 if (cc == null) cc = new CookieContainer(); 165 } 166 167 /// <summary> 168 /// Initializes a new instance of the <see cref="HttpHelper"/> class. 169 /// </summary> 170 /// <param name="contentType">Type of the content.</param> 171 /// <param name="accept">The accept.</param> 172 /// <param name="userAgent">The user agent.</param> 173 public HttpHelper(string contentType, string accept, string userAgent) 174 { 175 this.contentType = contentType; 176 this.accept = accept; 177 this.userAgent = userAgent; 178 } 179 180 /// <summary> 181 /// Initializes a new instance of the <see cref="HttpHelper"/> class. 182 /// </summary> 183 /// <param name="cc">The cc.</param> 184 /// <param name="contentType">Type of the content.</param> 185 /// <param name="accept">The accept.</param> 186 /// <param name="userAgent">The user agent.</param> 187 public HttpHelper(CookieContainer cc, string contentType, string accept, string userAgent) 188 { 189 this.cc = cc; 190 if (cc == null) cc = new CookieContainer(); 191 this.contentType = contentType; 192 this.accept = accept; 193 this.userAgent = userAgent; 194 } 195 #endregion 196 197 #region 公共方法 198 199 /// <summary> 200 /// 将COOKIE字符串转成COOKIE集合 201 /// </summary> 202 /// <param name="strCookie">COOKIE字符串</param> 203 /// <returns></returns> 204 public static CookieCollection StringToCookieCollection(string strCookie) 205 { 206 CookieCollection cookies = new CookieCollection(); 207 string[] strArray = strCookie.TrimEnd(new char[] { ';' }).Split(new char[] { ';' }); 208 foreach (string str in strArray) 209 { 210 string[] strArray2 = str.Split(new char[] { '=' }); 211 if (strArray2[0].Trim() != "") 212 { 213 Cookie c = new Cookie(); 214 c.Name = strArray2[0].Trim(); 215 c.Value = strArray2[1].Trim(); 216 if (strArray2.Length > 2) 217 { 218 c.Value += "="+strArray2[2].Trim(); 219 } 220 c.Value = c.Value.Split(',')[0]; 221 cookies.Add(c); 222 } 223 } 224 return cookies; 225 } 226 /// <summary> 227 /// 将COOKIE集合转成COOKIE字符串 228 /// </summary> 229 /// <param name="cookies">COOKIE集合</param> 230 /// <returns></returns> 231 public static string CookieCollectionToString(CookieCollection cookies) 232 { 233 string str = string.Empty; 234 if ((cookies != null) && (cookies.Count > 0)) 235 { 236 foreach (Cookie cookie in cookies) 237 { 238 string name = cookie.Name; 239 string str3 = cookie.Value; 240 str = str + string.Format("{0}={1};", name, str3); 241 } 242 } 243 return str; 244 } 245 /// <summary> 246 /// 获取指定页面的HTML代码 247 /// </summary> 248 /// <param name="url">指定页面的路径</param> 249 /// <param name="postData">回发的数据</param> 250 /// <param name="isPost">是否以post方式发送请求</param> 251 /// <param name="cookieCollection">Cookie集合</param> 252 /// <returns></returns> 253 public string GetHtml(string url, string postData, bool isPost, CookieContainer cookieContainer) 254 { 255 currentTry ++; 256 257 try 258 { 259 byte[] byteRequest = this.Encoding.GetBytes(postData); 260 261 HttpWebRequest httpWebRequest; 262 httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); 263 httpWebRequest.CookieContainer = cookieContainer; 264 httpWebRequest.ContentType = contentType; 265 httpWebRequest.Referer = referer; 266 httpWebRequest.Accept = accept; 267 httpWebRequest.UserAgent = userAgent; 268 httpWebRequest.Method = isPost ? "POST" : "GET"; 269 httpWebRequest.ContentLength = byteRequest.Length; 270 271 272 if (webProxy != null) 273 { 274 httpWebRequest.Proxy = webProxy; 275 } 276 277 Stream stream = httpWebRequest.GetRequestStream(); 278 stream.Write(byteRequest, 0, byteRequest.Length); 279 280 281 HttpWebResponse httpWebResponse; 282 httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 283 Stream responseStream = httpWebResponse.GetResponseStream(); 284 StreamReader streamReader = new StreamReader(responseStream, encoding); 285 string html = streamReader.ReadToEnd(); 286 stream.Close(); 287 streamReader.Close(); 288 responseStream.Close(); 289 290 currentTry = 0; 291 return html; 292 } 293 catch (Exception e) 294 { 295 //if (currentTry <= connectTimeOutRetryTryMaxTimes) 296 //{ 297 // GetHtml(url, postData, isPost, cookieContainer); 298 //} 299 300 301 currentTry = 0; 302 return string.Empty; 303 } 304 } 305 306 /// <summary> 307 /// 获取指定页面的HTML代码 308 /// </summary> 309 /// <param name="url">指定页面的路径</param> 310 /// <param name="cookieCollection">Cookie集合</param> 311 /// <returns></returns> 312 public string GetHtml(string url, CookieContainer cookieContainer) 313 { 314 currentTry++; 315 316 try 317 { 318 HttpWebRequest httpWebRequest; 319 httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); 320 httpWebRequest.CookieContainer = cookieContainer; 321 httpWebRequest.ContentType = contentType; 322 //httpWebRequest.Referer = System.Web.HttpUtility.UrlPathEncode(url); 323 httpWebRequest.Referer = referer; 324 httpWebRequest.Accept = accept; 325 httpWebRequest.UserAgent = userAgent; 326 httpWebRequest.Method = "GET"; 327 328 if (webProxy != null) 329 { 330 httpWebRequest.Proxy = webProxy; 331 } 332 333 HttpWebResponse httpWebResponse; 334 httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 335 responseUri = httpWebResponse.ResponseUri; 336 Stream responseStream = httpWebResponse.GetResponseStream(); 337 StreamReader streamReader = new StreamReader(responseStream, encoding); 338 string html = streamReader.ReadToEnd(); 339 streamReader.Close(); 340 responseStream.Close(); 341 342 currentTry = 0; 343 return html; 344 } 345 catch (Exception e) 346 { 347 //if (currentTry <= connectTimeOutRetryTryMaxTimes) 348 //{ 349 // GetHtml(url, cookieContainer); 350 //} 351 352 353 currentTry = 0; 354 return string.Empty; 355 } 356 } 357 358 /// <summary> 359 /// 获取指定页面的HTML代码 360 /// </summary> 361 /// <param name="url">指定页面的路径</param> 362 /// <returns></returns> 363 public string GetHtml(string url) 364 { 365 return GetHtml(url, cc); 366 } 367 368 /// <summary> 369 /// 获取指定页面的HTML代码 370 /// </summary> 371 /// <param name="url">指定页面的路径</param> 372 /// <param name="postData">回发的数据</param> 373 /// <param name="isPost">是否以post方式发送请求</param> 374 /// <returns></returns> 375 public string GetHtml(string url, string postData, bool isPost) 376 { 377 return GetHtml(url, postData, isPost, cc); 378 } 379 380 /// <summary> 381 /// 获取一个随机延迟 382 /// </summary> 383 /// <returns></returns> 384 private int GetDynamicDelay() 385 { 386 return 0; 387 //return new Random(DateTime.Now.Millisecond).Next(2000); 388 } 389 390 /// <summary> 391 /// 请求一个图片 392 /// </summary> 393 /// <param name="url"></param> 394 /// <returns></returns> 395 public Bitmap GetImage(string url) 396 { 397 return GetImage(url, cc); 398 } 399 400 /// <summary> 401 /// 请求一个图片 402 /// </summary> 403 /// <param name="url"></param> 404 /// <param name="cookieContainer"></param> 405 /// <returns></returns> 406 public Bitmap GetImage(string url, CookieContainer cookieContainer) 407 { 408 currentTry++; 409 410 try 411 { 412 HttpWebRequest httpWebRequest; 413 httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); 414 httpWebRequest.CookieContainer = cookieContainer; 415 httpWebRequest.Method = "GET"; 416 httpWebRequest.Accept = accept; 417 httpWebRequest.Referer = referer; 418 httpWebRequest.UserAgent = userAgent; 419 420 if (webProxy != null) 421 { 422 httpWebRequest.Proxy = webProxy; 423 } 424 425 HttpWebResponse httpWebResponse; 426 httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 427 Stream responseStream = httpWebResponse.GetResponseStream(); 428 429 Bitmap bitmap = new Bitmap(responseStream); 430 431 responseStream.Close(); 432 433 currentTry = 0; 434 return bitmap; 435 } 436 catch (Exception e) 437 { 438 if (currentTry <= connectTimeOutRetryTryMaxTimes) 439 { 440 GetHtml(url, cookieContainer); 441 } 442 443 444 currentTry = 0; 445 return null; 446 } 447 } 448 449 450 public void Download(string url, string localfile) 451 { 452 new WebClient().DownloadFile(url, localfile); 453 } 454 455 456 #endregion 457 458 459 private static ArrayList alllist = new ArrayList(); 460 461 public static int GetConnectToServerTimes() 462 { 463 return alllist.Count; 464 } 465 466 } 467 }
RequestParamHelper帮助类
1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Web; 5 using System.Web.Security; 6 using System.Web.UI; 7 using System.Web.UI.WebControls; 8 using System.Web.UI.WebControls.WebParts; 9 using System.Web.UI.HtmlControls; 10 11 /// <summary> 12 /// GetRequestParam 的摘要说明 13 /// </summary> 14 public class RequestParamHelper 15 { 16 public RequestParamHelper() 17 { 18 // 19 // TODO: 在此处添加构造函数逻辑 20 // 21 } 22 /// <summary> 23 /// 获取string类型的参数 24 /// </summary> 25 /// <param name="page">当前页</param> 26 /// <param name="session">当前session</param> 27 /// <param name="keyParam">参数名称</param> 28 /// <param name="defaultValue">默认值</param> 29 /// <returns></returns> 30 public static string GetRequestParam(Page page, System.Web.SessionState.HttpSessionState session,string keyParam,string defaultValue) 31 { 32 if (string.IsNullOrEmpty(page.Request.QueryString[keyParam])) 33 { 34 return defaultValue; 35 } 36 else 37 { 38 return page.Request.QueryString[keyParam]; 39 } 40 } 41 /// <summary> 42 /// 获取int类型的参数 43 /// </summary> 44 /// <param name="page">当前页</param> 45 /// <param name="session">当前session</param> 46 /// <param name="keyParam">参数名称</param> 47 /// <param name="minNum">最小值</param> 48 /// <param name="maxNum">最大值</param> 49 /// <param name="defaultValue">默认值</param> 50 /// <returns></returns> 51 public static int GetRequestParam(Page page, System.Web.SessionState.HttpSessionState session, string keyParam,int minNum,int maxNum,int defaultValue) 52 { 53 if (string.IsNullOrEmpty(page.Request.QueryString[keyParam])) 54 { 55 return defaultValue; 56 } 57 else 58 { 59 int temp = 0; 60 int.TryParse(page.Request.QueryString[keyParam], out temp); 61 if (temp > maxNum || temp < minNum) 62 { 63 return defaultValue; 64 } 65 else 66 { 67 return temp; 68 } 69 70 } 71 } 72 /// <summary> 73 /// 获取int类型的参数 74 /// </summary> 75 /// <param name="page">当前页</param> 76 /// <param name="session">当前session</param> 77 /// <param name="keyParam">参数名称</param> 78 /// <param name="defaultValue">默认值</param> 79 /// <returns></returns> 80 public static int GetRequestParam(Page page, System.Web.SessionState.HttpSessionState session, string keyParam, int defaultValue) 81 { 82 if (string.IsNullOrEmpty(page.Request.QueryString[keyParam])) 83 { 84 return defaultValue; 85 } 86 else 87 { 88 int temp = 0; 89 int.TryParse(page.Request.QueryString[keyParam], out temp); 90 if (temp == 0) 91 { 92 return defaultValue; 93 } 94 else 95 { 96 return temp; 97 } 98 99 } 100 } 101 }
SerializeHelper 帮助类
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.IO; 5 using System.Runtime.Serialization; 6 using System.Runtime.Serialization.Formatters.Binary; 7 8 namespace Common 9 { 10 public class SerializeHelper 11 { 12 public static Dictionary<T, K> GetDic<T, K>(string path) 13 { 14 object obj2 = GetObject(path); 15 if (obj2 == null) 16 { 17 return null; 18 } 19 return (obj2 as Dictionary<T, K>); 20 } 21 22 public static IList<T> GetList<T>(string path) 23 { 24 object obj2 = GetObject(path); 25 if (obj2 == null) 26 { 27 return null; 28 } 29 return (obj2 as IList<T>); 30 } 31 32 public static object GetObject(string path) 33 { 34 if (File.Exists(path)) 35 { 36 IFormatter formatter = new BinaryFormatter(); 37 try 38 { 39 Stream serializationStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); 40 object obj2 = formatter.Deserialize(serializationStream); 41 serializationStream.Close(); 42 return obj2; 43 } 44 catch 45 { 46 return null; 47 } 48 } 49 return null; 50 } 51 52 public static string ReadFile(string path) 53 { 54 StreamReader reader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read), Encoding.Default); 55 string str = reader.ReadToEnd(); 56 reader.Close(); 57 return str; 58 } 59 60 public static void SaveObject(object obj, string path) 61 { 62 if (File.Exists(path)) 63 { 64 File.Delete(path); 65 } 66 Stream serializationStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None); 67 try 68 { 69 IFormatter formatter = new BinaryFormatter(); 70 71 formatter.Serialize(serializationStream, obj); 72 serializationStream.Close(); 73 } 74 finally 75 { 76 serializationStream.Close(); 77 } 78 } 79 } 80 }