123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Security;
- using System.Security.Cryptography.X509Certificates;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace TimedUpload
- {
- /// <summary>
- /// Web操作
- /// </summary>
- public static class WebHelper
- {
- #region Host(获取主机名)
- /// <summary>
- /// 获取主机名,即域名,
- /// 范例:用户输入网址http://www.a.com/b.htm?a=1&b=2,
- /// 返回值为: www.a.com
- /// </summary>
- //public static string Host
- //{
- // get
- // {
- // return HttpContext.Current.Request.Url.Host;
- // }
- //}
- #endregion
- #region HttpWebRequest(请求网络资源)
- /// <summary>
- /// 请求网络资源,返回响应的文本
- /// </summary>
- /// <param name="url">网络资源地址</param>
- public static string HttpWebRequest(string url)
- {
- return HttpWebRequest(url, string.Empty, Encoding.GetEncoding("utf-8"));
- }
- /// <summary>
- /// 请求网络资源,返回响应的文本
- /// </summary>
- /// <param name="url">网络资源Url地址</param>
- /// <param name="parameters">提交的参数,格式:参数1=参数值1&参数2=参数值2</param>
- public static string HttpWebRequest(string url, string parameters)
- {
- return HttpWebRequest(url, parameters, Encoding.GetEncoding("utf-8"), "POST");
- }
- /// <summary>
- /// 请求网络资源,返回响应的文本
- /// </summary>
- /// <param name="url">网络资源Url地址</param>
- /// <param name="parameters"></param>
- public static string HttpWebRequest(string url, string parameters, string contentType, string Authorization, string app_key)
- {
- return HttpWebRequest(url, parameters, Encoding.GetEncoding("utf-8"), "POST", contentType, Authorization, app_key);
- }
- /// <summary>
- /// 请求网络资源,返回响应的文本
- /// </summary>
- /// <param name="url">网络资源Url地址</param>
- /// <param name="parameters"></param>
- public static string HttpWebRequest(string url, string parameters, string mehtod, string contentType, string Authorization, string app_key)
- {
- return HttpWebRequest(url, parameters, Encoding.GetEncoding("utf-8"), mehtod, contentType, Authorization, app_key);
- }
- /// <summary>
- /// 请求网络资源,返回响应的文本
- /// </summary>
- /// <param name="url">网络资源地址</param>
- /// <param name="parameters">提交的参数,格式:参数1=参数值1&参数2=参数值2</param>
- /// <param name="encoding">字符编码</param>
- /// <param name="isPost">是否Post提交</param>
- /// <param name="contentType">内容类型</param>
- /// <param name="cookie">Cookie容器</param>
- /// <param name="timeout">超时时间</param>
- public static string HttpWebRequest(string url, string parameters, Encoding encoding, string mehtod = "POST",
- string contentType = "application/x-www-form-urlencoded", string Authorization = null, string app_key = null, CookieContainer cookie = null, int timeout = 120000)
- {
- HttpWebRequest request = null;
- try
- {
- //如果是发送HTTPS请求
- //if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
- //{
- // ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
- // ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
- // //X509Certificate cerCaiShang = new X509Certificate(System.Web.HttpContext.Current.Server.MapPath(Config.GetValue("NB_PfxPath")), Config.GetValue("NB_PfxKey"));
- // //X509Certificate2 cerCaiShang = GetSentosaCertificate();
- // X509Certificate2 cerCaiShang = new X509Certificate2(Config.GetValue("NB_PfxPath"), Config.GetValue("NB_PfxKey"));
- // request = WebRequest.Create(url) as HttpWebRequest;
- // request.ClientCertificates.Add(cerCaiShang);
- // request.ProtocolVersion = HttpVersion.Version10;
- //}
- //else
- //{
- request = WebRequest.Create(url) as HttpWebRequest;
- //}
- request.Timeout = timeout;
- if (!string.IsNullOrEmpty(Authorization))
- {
- request.Headers["Authorization"] = Authorization;
- }
- if (!string.IsNullOrEmpty(app_key))
- {
- request.Headers["app_key"] = app_key;
- }
- request.CookieContainer = cookie;
- request.ContentType = contentType;
- request.Method = mehtod;
- if (mehtod == "POST")
- {
- byte[] postData = encoding.GetBytes(parameters);
- request.Method = "POST";
- request.ContentType = contentType;
- request.ContentLength = postData.Length;
- using (Stream stream = request.GetRequestStream())
- {
- stream.Write(postData, 0, postData.Length);
- }
- }
- if (mehtod == "PUT")
- {
- using (StreamWriter requestStream = new StreamWriter(request.GetRequestStream()))
- {
- requestStream.Write(parameters);
- }
- }
- var response = (HttpWebResponse)request.GetResponse();
- string result;
- using (Stream stream = response.GetResponseStream())
- {
- if (stream == null)
- return string.Empty;
- using (var reader = new StreamReader(stream, encoding))
- {
- result = reader.ReadToEnd();
- }
- }
- return result;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
- {
- return true;
- }
- private static X509Certificate2 GetSentosaCertificate()
- {
- X509Store userCaStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
- try
- {
- userCaStore.Open(OpenFlags.ReadOnly);
- X509Certificate2Collection certificatesInStore = userCaStore.Certificates;
- X509Certificate2Collection findResult = certificatesInStore.Find(X509FindType.FindBySubjectName, "server", true);
- X509Certificate2 clientCertificate = null;
- if (findResult.Count == 1)
- {
- clientCertificate = findResult[0];
- }
- else
- {
- throw new Exception("Unable to locate the correct client certificate.");
- }
- return clientCertificate;
- }
- catch
- {
- throw;
- }
- finally
- {
- userCaStore.Close();
- }
- }
- #endregion
- #region 去除HTML标记
- /// <summary>
- /// 去除HTML标记
- /// </summary>
- /// <param name="NoHTML">包括HTML的源码 </param>
- /// <returns>已经去除后的文字</returns>
- //public static string NoHtml(string Htmlstring)
- //{
- // //删除脚本
- // Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
- // //删除HTML
- // Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
- // Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
- // Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
- // Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
- // Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
- // Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
- // Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
- // Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
- // Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
- // Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
- // Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
- // Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
- // Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
- // Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
- // Htmlstring = Regex.Replace(Htmlstring, @"…", "", RegexOptions.IgnoreCase);
- // Htmlstring = Regex.Replace(Htmlstring, @"—", "", RegexOptions.IgnoreCase);
- // Htmlstring = Regex.Replace(Htmlstring, @"“", "", RegexOptions.IgnoreCase);
- // Htmlstring.Replace("<", "");
- // Htmlstring = Regex.Replace(Htmlstring, @"”", "", RegexOptions.IgnoreCase);
- // Htmlstring.Replace(">", "");
- // Htmlstring.Replace("\r\n", "");
- // Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
- // return Htmlstring;
- //}
- #endregion
- #region 格式化文本(防止SQL注入)
- /// <summary>
- /// 格式化文本(防止SQL注入)
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static string Formatstr(string html)
- {
- System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
- System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
- System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" on[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
- System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
- System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
- System.Text.RegularExpressions.Regex regex10 = new System.Text.RegularExpressions.Regex(@"select", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
- System.Text.RegularExpressions.Regex regex11 = new System.Text.RegularExpressions.Regex(@"update", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
- System.Text.RegularExpressions.Regex regex12 = new System.Text.RegularExpressions.Regex(@"delete", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
- html = regex1.Replace(html, ""); //过滤<script></script>标记
- html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
- html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
- html = regex4.Replace(html, ""); //过滤iframe
- html = regex10.Replace(html, "s_elect");
- html = regex11.Replace(html, "u_pudate");
- html = regex12.Replace(html, "d_elete");
- html = html.Replace("'", "’");
- html = html.Replace(" ", " ");
- return html;
- }
- #endregion
- }
- }
|