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
{
///
/// Web操作
///
public static class WebHelper
{
#region Host(获取主机名)
///
/// 获取主机名,即域名,
/// 范例:用户输入网址http://www.a.com/b.htm?a=1&b=2,
/// 返回值为: www.a.com
///
//public static string Host
//{
// get
// {
// return HttpContext.Current.Request.Url.Host;
// }
//}
#endregion
#region HttpWebRequest(请求网络资源)
///
/// 请求网络资源,返回响应的文本
///
/// 网络资源地址
public static string HttpWebRequest(string url)
{
return HttpWebRequest(url, string.Empty, Encoding.GetEncoding("utf-8"));
}
///
/// 请求网络资源,返回响应的文本
///
/// 网络资源Url地址
/// 提交的参数,格式:参数1=参数值1&参数2=参数值2
public static string HttpWebRequest(string url, string parameters)
{
return HttpWebRequest(url, parameters, Encoding.GetEncoding("utf-8"), "POST");
}
///
/// 请求网络资源,返回响应的文本
///
/// 网络资源Url地址
///
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);
}
///
/// 请求网络资源,返回响应的文本
///
/// 网络资源Url地址
///
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);
}
///
/// 请求网络资源,返回响应的文本
///
/// 网络资源地址
/// 提交的参数,格式:参数1=参数值1&参数2=参数值2
/// 字符编码
/// 是否Post提交
/// 内容类型
/// Cookie容器
/// 超时时间
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标记
///
/// 去除HTML标记
///
/// 包括HTML的源码
/// 已经去除后的文字
//public static string NoHtml(string Htmlstring)
//{
// //删除脚本
// Htmlstring = Regex.Replace(Htmlstring, @"", "", 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, @"