Common.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace iNethinkCMS.Helper
  5. {
  6. class Common
  7. {
  8. /// <summary>
  9. /// Web GET方式提交
  10. /// </summary>
  11. /// <param name="url"></param>
  12. /// <param name="_Encoding">字符编码</param>
  13. /// <returns></returns>
  14. public static string HttpWebRequestGET(string url, Encoding _Encoding = null, bool isUrlDecode = true, string _ContentType = null)
  15. {
  16. try
  17. {
  18. System.GC.Collect();
  19. if (_Encoding == null)
  20. {
  21. _Encoding = Encoding.Default;
  22. }
  23. if (System.Net.ServicePointManager.DefaultConnectionLimit < 512)
  24. {
  25. System.Net.ServicePointManager.DefaultConnectionLimit = 512;
  26. }
  27. System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
  28. webRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/json,*/*";
  29. webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Zune 4.7; BOIE9;ZHCN)";
  30. webRequest.Method = "GET";
  31. webRequest.Timeout = 600000;
  32. webRequest.KeepAlive = false;
  33. webRequest.ProtocolVersion = System.Net.HttpVersion.Version10;
  34. if (_ContentType != null)
  35. {
  36. webRequest.ContentType = _ContentType;
  37. }
  38. System.Threading.Thread.Sleep(5);
  39. using (System.Net.WebResponse wr = webRequest.GetResponse())
  40. {
  41. System.IO.StreamReader str = new System.IO.StreamReader(wr.GetResponseStream(), _Encoding);
  42. string strMsg = str.ReadToEnd();
  43. str.Close();
  44. str.Dispose();
  45. str = null;
  46. webRequest.Abort();
  47. webRequest = null;
  48. if (isUrlDecode)
  49. {
  50. strMsg = System.Web.HttpUtility.UrlDecode(strMsg);
  51. }
  52. return strMsg;
  53. //在这里对接收到的页面内容进行处理
  54. }
  55. }
  56. catch (Exception ex)
  57. {
  58. System.Net.ServicePointManager.Expect100Continue = false;
  59. return "Request failed,Exception:" + ex.Message;
  60. }
  61. }
  62. public bool IsReusable
  63. {
  64. get
  65. {
  66. return false;
  67. }
  68. }
  69. }
  70. }