WebHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using System;
  2. using System.Collections.Generic;
  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. namespace TimedUpload
  11. {
  12. /// <summary>
  13. /// Web操作
  14. /// </summary>
  15. public static class WebHelper
  16. {
  17. #region Host(获取主机名)
  18. /// <summary>
  19. /// 获取主机名,即域名,
  20. /// 范例:用户输入网址http://www.a.com/b.htm?a=1&amp;b=2,
  21. /// 返回值为: www.a.com
  22. /// </summary>
  23. //public static string Host
  24. //{
  25. // get
  26. // {
  27. // return HttpContext.Current.Request.Url.Host;
  28. // }
  29. //}
  30. #endregion
  31. #region HttpWebRequest(请求网络资源)
  32. /// <summary>
  33. /// 请求网络资源,返回响应的文本
  34. /// </summary>
  35. /// <param name="url">网络资源地址</param>
  36. public static string HttpWebRequest(string url)
  37. {
  38. return HttpWebRequest(url, string.Empty, Encoding.GetEncoding("utf-8"));
  39. }
  40. /// <summary>
  41. /// 请求网络资源,返回响应的文本
  42. /// </summary>
  43. /// <param name="url">网络资源Url地址</param>
  44. /// <param name="parameters">提交的参数,格式:参数1=参数值1&amp;参数2=参数值2</param>
  45. public static string HttpWebRequest(string url, string parameters)
  46. {
  47. return HttpWebRequest(url, parameters, Encoding.GetEncoding("utf-8"), "POST");
  48. }
  49. /// <summary>
  50. /// 请求网络资源,返回响应的文本
  51. /// </summary>
  52. /// <param name="url">网络资源Url地址</param>
  53. /// <param name="parameters"></param>
  54. public static string HttpWebRequest(string url, string parameters, string contentType, string Authorization, string app_key)
  55. {
  56. return HttpWebRequest(url, parameters, Encoding.GetEncoding("utf-8"), "POST", contentType, Authorization, app_key);
  57. }
  58. /// <summary>
  59. /// 请求网络资源,返回响应的文本
  60. /// </summary>
  61. /// <param name="url">网络资源Url地址</param>
  62. /// <param name="parameters"></param>
  63. public static string HttpWebRequest(string url, string parameters, string mehtod, string contentType, string Authorization, string app_key)
  64. {
  65. return HttpWebRequest(url, parameters, Encoding.GetEncoding("utf-8"), mehtod, contentType, Authorization, app_key);
  66. }
  67. /// <summary>
  68. /// 请求网络资源,返回响应的文本
  69. /// </summary>
  70. /// <param name="url">网络资源地址</param>
  71. /// <param name="parameters">提交的参数,格式:参数1=参数值1&amp;参数2=参数值2</param>
  72. /// <param name="encoding">字符编码</param>
  73. /// <param name="isPost">是否Post提交</param>
  74. /// <param name="contentType">内容类型</param>
  75. /// <param name="cookie">Cookie容器</param>
  76. /// <param name="timeout">超时时间</param>
  77. public static string HttpWebRequest(string url, string parameters, Encoding encoding, string mehtod = "POST",
  78. string contentType = "application/x-www-form-urlencoded", string Authorization = null, string app_key = null, CookieContainer cookie = null, int timeout = 120000)
  79. {
  80. HttpWebRequest request = null;
  81. try
  82. {
  83. //如果是发送HTTPS请求
  84. //if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
  85. //{
  86. // ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
  87. // ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
  88. // //X509Certificate cerCaiShang = new X509Certificate(System.Web.HttpContext.Current.Server.MapPath(Config.GetValue("NB_PfxPath")), Config.GetValue("NB_PfxKey"));
  89. // //X509Certificate2 cerCaiShang = GetSentosaCertificate();
  90. // X509Certificate2 cerCaiShang = new X509Certificate2(Config.GetValue("NB_PfxPath"), Config.GetValue("NB_PfxKey"));
  91. // request = WebRequest.Create(url) as HttpWebRequest;
  92. // request.ClientCertificates.Add(cerCaiShang);
  93. // request.ProtocolVersion = HttpVersion.Version10;
  94. //}
  95. //else
  96. //{
  97. request = WebRequest.Create(url) as HttpWebRequest;
  98. //}
  99. request.Timeout = timeout;
  100. if (!string.IsNullOrEmpty(Authorization))
  101. {
  102. request.Headers["Authorization"] = Authorization;
  103. }
  104. if (!string.IsNullOrEmpty(app_key))
  105. {
  106. request.Headers["app_key"] = app_key;
  107. }
  108. request.CookieContainer = cookie;
  109. request.ContentType = contentType;
  110. request.Method = mehtod;
  111. if (mehtod == "POST")
  112. {
  113. byte[] postData = encoding.GetBytes(parameters);
  114. request.Method = "POST";
  115. request.ContentType = contentType;
  116. request.ContentLength = postData.Length;
  117. using (Stream stream = request.GetRequestStream())
  118. {
  119. stream.Write(postData, 0, postData.Length);
  120. }
  121. }
  122. if (mehtod == "PUT")
  123. {
  124. using (StreamWriter requestStream = new StreamWriter(request.GetRequestStream()))
  125. {
  126. requestStream.Write(parameters);
  127. }
  128. }
  129. var response = (HttpWebResponse)request.GetResponse();
  130. string result;
  131. using (Stream stream = response.GetResponseStream())
  132. {
  133. if (stream == null)
  134. return string.Empty;
  135. using (var reader = new StreamReader(stream, encoding))
  136. {
  137. result = reader.ReadToEnd();
  138. }
  139. }
  140. return result;
  141. }
  142. catch (Exception ex)
  143. {
  144. throw ex;
  145. }
  146. }
  147. private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  148. {
  149. return true;
  150. }
  151. private static X509Certificate2 GetSentosaCertificate()
  152. {
  153. X509Store userCaStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
  154. try
  155. {
  156. userCaStore.Open(OpenFlags.ReadOnly);
  157. X509Certificate2Collection certificatesInStore = userCaStore.Certificates;
  158. X509Certificate2Collection findResult = certificatesInStore.Find(X509FindType.FindBySubjectName, "server", true);
  159. X509Certificate2 clientCertificate = null;
  160. if (findResult.Count == 1)
  161. {
  162. clientCertificate = findResult[0];
  163. }
  164. else
  165. {
  166. throw new Exception("Unable to locate the correct client certificate.");
  167. }
  168. return clientCertificate;
  169. }
  170. catch
  171. {
  172. throw;
  173. }
  174. finally
  175. {
  176. userCaStore.Close();
  177. }
  178. }
  179. #endregion
  180. #region 去除HTML标记
  181. /// <summary>
  182. /// 去除HTML标记
  183. /// </summary>
  184. /// <param name="NoHTML">包括HTML的源码 </param>
  185. /// <returns>已经去除后的文字</returns>
  186. //public static string NoHtml(string Htmlstring)
  187. //{
  188. // //删除脚本
  189. // Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
  190. // //删除HTML
  191. // Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
  192. // Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
  193. // Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
  194. // Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
  195. // Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
  196. // Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
  197. // Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
  198. // Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
  199. // Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
  200. // Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
  201. // Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
  202. // Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
  203. // Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
  204. // Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
  205. // Htmlstring = Regex.Replace(Htmlstring, @"&hellip;", "", RegexOptions.IgnoreCase);
  206. // Htmlstring = Regex.Replace(Htmlstring, @"&mdash;", "", RegexOptions.IgnoreCase);
  207. // Htmlstring = Regex.Replace(Htmlstring, @"&ldquo;", "", RegexOptions.IgnoreCase);
  208. // Htmlstring.Replace("<", "");
  209. // Htmlstring = Regex.Replace(Htmlstring, @"&rdquo;", "", RegexOptions.IgnoreCase);
  210. // Htmlstring.Replace(">", "");
  211. // Htmlstring.Replace("\r\n", "");
  212. // Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
  213. // return Htmlstring;
  214. //}
  215. #endregion
  216. #region 格式化文本(防止SQL注入)
  217. /// <summary>
  218. /// 格式化文本(防止SQL注入)
  219. /// </summary>
  220. /// <param name="str"></param>
  221. /// <returns></returns>
  222. public static string Formatstr(string html)
  223. {
  224. System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  225. System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  226. System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" on[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  227. System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  228. System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  229. System.Text.RegularExpressions.Regex regex10 = new System.Text.RegularExpressions.Regex(@"select", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  230. System.Text.RegularExpressions.Regex regex11 = new System.Text.RegularExpressions.Regex(@"update", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  231. System.Text.RegularExpressions.Regex regex12 = new System.Text.RegularExpressions.Regex(@"delete", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  232. html = regex1.Replace(html, ""); //过滤<script></script>标记
  233. html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
  234. html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
  235. html = regex4.Replace(html, ""); //过滤iframe
  236. html = regex10.Replace(html, "s_elect");
  237. html = regex11.Replace(html, "u_pudate");
  238. html = regex12.Replace(html, "d_elete");
  239. html = html.Replace("'", "’");
  240. html = html.Replace("&nbsp;", " ");
  241. return html;
  242. }
  243. #endregion
  244. }
  245. }