RoadFlow2.1 临时演示

HttpHelper.cs 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Security;
  7. using System.Security.Cryptography.X509Certificates;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Web;
  11. using System.IO.Compression;
  12. using System.Collections.Generic;
  13. using System.Collections.Specialized;
  14. namespace RoadFlow.Utility
  15. {
  16. public class HttpHelper
  17. {
  18. private static bool RemoteCertificateValidate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  19. {
  20. //用户https请求
  21. return true; //总是接受
  22. }
  23. public static string SendPost(string url, string data)
  24. {
  25. return Send(url, "POST", data, null);
  26. }
  27. public static string SendGet(string url)
  28. {
  29. return Send(url, "GET", null, null);
  30. }
  31. public static string Send(string url, string method, string data, HttpConfig config)
  32. {
  33. if (config == null) config = new HttpConfig();
  34. string result;
  35. using (HttpWebResponse response = GetResponse(url, method, data, config))
  36. {
  37. Stream stream = response.GetResponseStream();
  38. if (!String.IsNullOrEmpty(response.ContentEncoding))
  39. {
  40. if (response.ContentEncoding.Contains("gzip"))
  41. {
  42. stream = new GZipStream(stream, CompressionMode.Decompress);
  43. }
  44. else if (response.ContentEncoding.Contains("deflate"))
  45. {
  46. stream = new DeflateStream(stream, CompressionMode.Decompress);
  47. }
  48. }
  49. byte[] bytes = null;
  50. using (MemoryStream ms = new MemoryStream())
  51. {
  52. int count;
  53. byte[] buffer = new byte[4096];
  54. while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
  55. {
  56. ms.Write(buffer, 0, count);
  57. }
  58. bytes = ms.ToArray();
  59. }
  60. #region 检测流编码
  61. Encoding encoding;
  62. //检测响应头是否返回了编码类型,若返回了编码类型则使用返回的编码
  63. //注:有时响应头没有编码类型,CharacterSet经常设置为ISO-8859-1
  64. if (!string.IsNullOrEmpty(response.CharacterSet) && response.CharacterSet.ToUpper() != "ISO-8859-1")
  65. {
  66. encoding = Encoding.GetEncoding(response.CharacterSet == "utf8" ? "utf-8" : response.CharacterSet);
  67. }
  68. else
  69. {
  70. //若没有在响应头找到编码,则去html找meta头的charset
  71. result = Encoding.Default.GetString(bytes);
  72. //在返回的html里使用正则匹配页面编码
  73. Match match = Regex.Match(result, @"<meta.*charset=""?([\w-]+)""?.*>", RegexOptions.IgnoreCase);
  74. if (match.Success)
  75. {
  76. encoding = Encoding.GetEncoding(match.Groups[1].Value);
  77. }
  78. else
  79. {
  80. //若html里面也找不到编码,默认使用utf-8
  81. encoding = Encoding.GetEncoding(config.CharacterSet);
  82. }
  83. }
  84. #endregion
  85. result = encoding.GetString(bytes);
  86. }
  87. return result;
  88. }
  89. private static HttpWebResponse GetResponse(string url, string method, string data, HttpConfig config)
  90. {
  91. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  92. request.Method = method;
  93. request.Referer = config.Referer;
  94. //有些页面不设置用户代理信息则会抓取不到内容
  95. request.UserAgent = config.UserAgent;
  96. request.Timeout = config.Timeout;
  97. request.Accept = config.Accept;
  98. request.Headers.Set("Accept-Encoding", config.AcceptEncoding);
  99. request.ContentType = config.ContentType;
  100. request.KeepAlive = config.KeepAlive;
  101. if (url.ToLower().StartsWith("https"))
  102. {
  103. //这里加入解决生产环境访问https的问题--Could not establish trust relationship for the SSL/TLS secure channel
  104. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteCertificateValidate);
  105. }
  106. if (method.ToUpper() == "POST")
  107. {
  108. if (!string.IsNullOrEmpty(data))
  109. {
  110. byte[] bytes = Encoding.UTF8.GetBytes(data);
  111. if (config.GZipCompress)
  112. {
  113. using (MemoryStream stream = new MemoryStream())
  114. {
  115. using (GZipStream gZipStream = new GZipStream(stream, CompressionMode.Compress))
  116. {
  117. gZipStream.Write(bytes, 0, bytes.Length);
  118. }
  119. bytes = stream.ToArray();
  120. }
  121. }
  122. request.ContentLength = bytes.Length;
  123. request.GetRequestStream().Write(bytes, 0, bytes.Length);
  124. }
  125. else
  126. {
  127. request.ContentLength = 0;
  128. }
  129. }
  130. return (HttpWebResponse)request.GetResponse();
  131. }
  132. }
  133. public class HttpConfig
  134. {
  135. public string Referer { get; set; }
  136. /// <summary>
  137. /// 默认(text/html)
  138. /// </summary>
  139. public string ContentType { get; set; }
  140. public string Accept { get; set; }
  141. public string AcceptEncoding { get; set; }
  142. /// <summary>
  143. /// 超时时间(毫秒)默认100000
  144. /// </summary>
  145. public int Timeout { get; set; }
  146. public string UserAgent { get; set; }
  147. /// <summary>
  148. /// POST请求时,数据是否进行gzip压缩
  149. /// </summary>
  150. public bool GZipCompress { get; set; }
  151. public bool KeepAlive { get; set; }
  152. public string CharacterSet { get; set; }
  153. public HttpConfig()
  154. {
  155. this.Timeout = 100000;
  156. this.ContentType = "text/html; charset=" + Encoding.UTF8.WebName;
  157. this.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36";
  158. this.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
  159. this.AcceptEncoding = "gzip,deflate";
  160. this.GZipCompress = false;
  161. this.KeepAlive = true;
  162. this.CharacterSet = "UTF-8";
  163. }
  164. }
  165. }