518 lines
14 KiB
C#
518 lines
14 KiB
C#
|
using CsharpHttpHelper.Enum;
|
|||
|
using CsharpHttpHelper.Static;
|
|||
|
using System;
|
|||
|
using System.Collections.Specialized;
|
|||
|
using System.IO;
|
|||
|
using System.IO.Compression;
|
|||
|
using System.Net;
|
|||
|
using System.Net.Security;
|
|||
|
using System.Security.Cryptography.X509Certificates;
|
|||
|
using System.Text;
|
|||
|
using System.Text.RegularExpressions;
|
|||
|
|
|||
|
namespace CsharpHttpHelper.Base
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Http连接操作帮助类 Copyright:http://www.httphelper.com/
|
|||
|
/// </summary>
|
|||
|
internal class HttphelperBase
|
|||
|
{
|
|||
|
private Encoding encoding = Encoding.Default;
|
|||
|
|
|||
|
private Encoding postencoding = Encoding.Default;
|
|||
|
|
|||
|
private HttpWebRequest request = null;
|
|||
|
|
|||
|
private HttpWebResponse response = null;
|
|||
|
|
|||
|
private IPEndPoint _IPEndPoint = null;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 根据相传入的数据,得到相应页面数据
|
|||
|
/// </summary>
|
|||
|
/// <param name="item">参数类对象</param>
|
|||
|
/// <returns>返回HttpResult类型</returns>
|
|||
|
internal HttpResult GetHtml(HttpItem item)
|
|||
|
{
|
|||
|
HttpResult httpResult = new HttpResult();
|
|||
|
try
|
|||
|
{
|
|||
|
SetRequest(item);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
HttpResult httpResult2 = new HttpResult();
|
|||
|
httpResult2.Cookie = string.Empty;
|
|||
|
httpResult2.Header = null;
|
|||
|
httpResult2.Html = ex.Message;
|
|||
|
httpResult2.StatusDescription = "配置参数时出错:" + ex.Message;
|
|||
|
return httpResult2;
|
|||
|
}
|
|||
|
try
|
|||
|
{
|
|||
|
using (response = (HttpWebResponse)request.GetResponse())
|
|||
|
{
|
|||
|
GetData(item, httpResult);
|
|||
|
}
|
|||
|
}
|
|||
|
catch (WebException ex2)
|
|||
|
{
|
|||
|
if (ex2.Response != null)
|
|||
|
{
|
|||
|
using (response = (HttpWebResponse)ex2.Response)
|
|||
|
{
|
|||
|
GetData(item, httpResult);
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
httpResult.Html = ex2.Message;
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
httpResult.Html = ex.Message;
|
|||
|
}
|
|||
|
if (item.IsToLower)
|
|||
|
{
|
|||
|
httpResult.Html = httpResult.Html.ToLower();
|
|||
|
}
|
|||
|
if (item.IsReset)
|
|||
|
{
|
|||
|
request = null;
|
|||
|
response = null;
|
|||
|
}
|
|||
|
return httpResult;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 快速Post数据这个访求与GetHtml一样,只是不接收返回数据,只做提交。
|
|||
|
/// </summary>
|
|||
|
/// <param name="item">参数类对象</param>
|
|||
|
/// <returns>返回HttpResult类型</returns>
|
|||
|
internal HttpResult FastRequest(HttpItem item)
|
|||
|
{
|
|||
|
HttpResult httpResult = new HttpResult();
|
|||
|
try
|
|||
|
{
|
|||
|
SetRequest(item);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
HttpResult httpResult2 = new HttpResult();
|
|||
|
httpResult2.Cookie = ((((NameValueCollection)response.Headers)["set-cookie"] != null) ? ((NameValueCollection)response.Headers)["set-cookie"] : string.Empty);
|
|||
|
httpResult2.Header = null;
|
|||
|
httpResult2.Html = ex.Message;
|
|||
|
httpResult2.StatusDescription = "配置参数时出错:" + ex.Message;
|
|||
|
return httpResult2;
|
|||
|
}
|
|||
|
try
|
|||
|
{
|
|||
|
using (response = (HttpWebResponse)request.GetResponse())
|
|||
|
{
|
|||
|
HttpResult httpResult3 = new HttpResult();
|
|||
|
httpResult3.Cookie = ((((NameValueCollection)response.Headers)["set-cookie"] != null) ? ((NameValueCollection)response.Headers)["set-cookie"] : string.Empty);
|
|||
|
httpResult3.Header = response.Headers;
|
|||
|
httpResult3.StatusCode = response.StatusCode;
|
|||
|
httpResult3.StatusDescription = response.StatusDescription;
|
|||
|
return httpResult3;
|
|||
|
}
|
|||
|
}
|
|||
|
catch (WebException ex2)
|
|||
|
{
|
|||
|
using (response = (HttpWebResponse)ex2.Response)
|
|||
|
{
|
|||
|
HttpResult httpResult4 = new HttpResult();
|
|||
|
httpResult4.Cookie = ((((NameValueCollection)response.Headers)["set-cookie"] != null) ? ((NameValueCollection)response.Headers)["set-cookie"] : string.Empty);
|
|||
|
httpResult4.Header = response.Headers;
|
|||
|
httpResult4.StatusCode = response.StatusCode;
|
|||
|
httpResult4.StatusDescription = response.StatusDescription;
|
|||
|
return httpResult4;
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
httpResult.Html = ex.Message;
|
|||
|
}
|
|||
|
if (item.IsToLower)
|
|||
|
{
|
|||
|
httpResult.Html = httpResult.Html.ToLower();
|
|||
|
}
|
|||
|
return httpResult;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 获取数据的并解析的方法
|
|||
|
/// </summary>
|
|||
|
/// <param name="item"></param>
|
|||
|
/// <param name="result"></param>
|
|||
|
private void GetData(HttpItem item, HttpResult result)
|
|||
|
{
|
|||
|
if (response != null)
|
|||
|
{
|
|||
|
result.StatusCode = response.StatusCode;
|
|||
|
result.ResponseUri = response.ResponseUri.ToString();
|
|||
|
result.StatusDescription = response.StatusDescription;
|
|||
|
result.Header = response.Headers;
|
|||
|
if (response.Cookies != null)
|
|||
|
{
|
|||
|
result.CookieCollection = response.Cookies;
|
|||
|
}
|
|||
|
if (((NameValueCollection)response.Headers)["set-cookie"] != null)
|
|||
|
{
|
|||
|
result.Cookie = ((NameValueCollection)response.Headers)["set-cookie"];
|
|||
|
}
|
|||
|
if (item.IsUpdateCookie)
|
|||
|
{
|
|||
|
item.Cookie = result.Cookie;
|
|||
|
item.CookieCollection = result.CookieCollection;
|
|||
|
}
|
|||
|
byte[] @byte = GetByte(item);
|
|||
|
if (@byte != null && @byte.Length > 0)
|
|||
|
{
|
|||
|
SetEncoding(item, result, @byte);
|
|||
|
SetResultByte(item, result, @byte);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
result.Html = string.Empty;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 设置返回的Byte
|
|||
|
/// </summary>
|
|||
|
/// <param name="item">HttpItem</param>
|
|||
|
/// <param name="result">result</param>
|
|||
|
/// <param name="enByte">byte</param>
|
|||
|
private void SetResultByte(HttpItem item, HttpResult result, byte[] enByte)
|
|||
|
{
|
|||
|
if (item.ResultType == ResultType.Byte)
|
|||
|
{
|
|||
|
result.ResultByte = enByte;
|
|||
|
}
|
|||
|
else if (item.ResultType == ResultType.String)
|
|||
|
{
|
|||
|
result.Html = encoding.GetString(enByte);
|
|||
|
}
|
|||
|
else if (item.ResultType == ResultType.StringByte)
|
|||
|
{
|
|||
|
result.ResultByte = enByte;
|
|||
|
result.Html = encoding.GetString(enByte);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 设置编码
|
|||
|
/// </summary>
|
|||
|
/// <param name="item">HttpItem</param>
|
|||
|
/// <param name="result">HttpResult</param>
|
|||
|
/// <param name="ResponseByte">byte[]</param>
|
|||
|
private void SetEncoding(HttpItem item, HttpResult result, byte[] ResponseByte)
|
|||
|
{
|
|||
|
if (encoding == null)
|
|||
|
{
|
|||
|
Match match = Regex.Match(Encoding.Default.GetString(ResponseByte), RegexString.Enconding, RegexOptions.IgnoreCase);
|
|||
|
string text = string.Empty;
|
|||
|
if (match != null && match.Groups.Count > 0)
|
|||
|
{
|
|||
|
text = match.Groups[1].Value.ToLower().Trim();
|
|||
|
}
|
|||
|
string text2 = string.Empty;
|
|||
|
if (!string.IsNullOrWhiteSpace(response.CharacterSet))
|
|||
|
{
|
|||
|
text2 = response.CharacterSet.Trim().Replace("\"", "").Replace("'", "");
|
|||
|
}
|
|||
|
if (text.Length > 2)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
encoding = Encoding.GetEncoding(text.Replace("\"", string.Empty).Replace("'", "").Replace(";", "")
|
|||
|
.Replace("iso-8859-1", "gbk")
|
|||
|
.Trim());
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(text2))
|
|||
|
{
|
|||
|
encoding = Encoding.UTF8;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
encoding = Encoding.GetEncoding(text2);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
else if (string.IsNullOrEmpty(text2))
|
|||
|
{
|
|||
|
encoding = Encoding.UTF8;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
encoding = Encoding.GetEncoding(text2);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 提取网页Byte
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
private byte[] GetByte(HttpItem item)
|
|||
|
{
|
|||
|
byte[] result = null;
|
|||
|
using (MemoryStream memoryStream = new MemoryStream())
|
|||
|
{
|
|||
|
if (item.IsGzip)
|
|||
|
{
|
|||
|
new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(memoryStream, 10240);
|
|||
|
}
|
|||
|
else if (response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
|
|||
|
{
|
|||
|
new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(memoryStream, 10240);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
response.GetResponseStream().CopyTo(memoryStream, 10240);
|
|||
|
}
|
|||
|
result = memoryStream.ToArray();
|
|||
|
}
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 为请求准备参数
|
|||
|
/// </summary>
|
|||
|
/// <param name="item">参数列表</param>
|
|||
|
private void SetRequest(HttpItem item)
|
|||
|
{
|
|||
|
if (!string.IsNullOrWhiteSpace(item.CerPath) || !string.IsNullOrWhiteSpace(item.CerPath))
|
|||
|
{
|
|||
|
ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult;
|
|||
|
}
|
|||
|
request = (HttpWebRequest)WebRequest.Create(item.URL);
|
|||
|
if (item.IPEndPoint != null)
|
|||
|
{
|
|||
|
_IPEndPoint = item.IPEndPoint;
|
|||
|
request.ServicePoint.BindIPEndPointDelegate = BindIPEndPointCallback;
|
|||
|
}
|
|||
|
request.AutomaticDecompression = item.AutomaticDecompression;
|
|||
|
SetCer(item);
|
|||
|
SetCerList(item);
|
|||
|
if (item.Header != null && item.Header.Count > 0)
|
|||
|
{
|
|||
|
string[] allKeys = item.Header.AllKeys;
|
|||
|
foreach (string name in allKeys)
|
|||
|
{
|
|||
|
request.Headers.Add(name, ((NameValueCollection)item.Header)[name]);
|
|||
|
}
|
|||
|
}
|
|||
|
SetProxy(item);
|
|||
|
if (item.ProtocolVersion != (Version)null)
|
|||
|
{
|
|||
|
request.ProtocolVersion = item.ProtocolVersion;
|
|||
|
}
|
|||
|
request.ServicePoint.Expect100Continue = item.Expect100Continue;
|
|||
|
request.Method = item.Method;
|
|||
|
request.Timeout = item.Timeout;
|
|||
|
request.KeepAlive = item.KeepAlive;
|
|||
|
request.ReadWriteTimeout = item.ReadWriteTimeout;
|
|||
|
if (!string.IsNullOrWhiteSpace(item.Host))
|
|||
|
{
|
|||
|
request.Host = item.Host;
|
|||
|
}
|
|||
|
if (item.IfModifiedSince.HasValue)
|
|||
|
{
|
|||
|
request.IfModifiedSince = Convert.ToDateTime(item.IfModifiedSince);
|
|||
|
}
|
|||
|
request.Accept = item.Accept;
|
|||
|
request.ContentType = item.ContentType;
|
|||
|
request.UserAgent = item.UserAgent;
|
|||
|
encoding = item.Encoding;
|
|||
|
request.Credentials = item.ICredentials;
|
|||
|
SetCookie(item);
|
|||
|
request.Referer = item.Referer;
|
|||
|
request.AllowAutoRedirect = item.Allowautoredirect;
|
|||
|
if (item.MaximumAutomaticRedirections > 0)
|
|||
|
{
|
|||
|
request.MaximumAutomaticRedirections = item.MaximumAutomaticRedirections;
|
|||
|
}
|
|||
|
SetPostData(item);
|
|||
|
if (item.Connectionlimit > 0)
|
|||
|
{
|
|||
|
request.ServicePoint.ConnectionLimit = item.Connectionlimit;
|
|||
|
}
|
|||
|
if (item.SecurityProtocol > SecurityProtocolType.SystemDefault)
|
|||
|
{
|
|||
|
ServicePointManager.SecurityProtocol = item.SecurityProtocol;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 设置证书
|
|||
|
/// </summary>
|
|||
|
/// <param name="item"></param>
|
|||
|
private void SetCer(HttpItem item)
|
|||
|
{
|
|||
|
if (!string.IsNullOrWhiteSpace(item.CerPath))
|
|||
|
{
|
|||
|
if (!string.IsNullOrWhiteSpace(item.CerPwd))
|
|||
|
{
|
|||
|
request.ClientCertificates.Add(new X509Certificate(item.CerPath, item.CerPwd));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
request.ClientCertificates.Add(new X509Certificate(item.CerPath));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 设置多个证书
|
|||
|
/// </summary>
|
|||
|
/// <param name="item"></param>
|
|||
|
private void SetCerList(HttpItem item)
|
|||
|
{
|
|||
|
if (item.ClentCertificates != null && item.ClentCertificates.Count > 0)
|
|||
|
{
|
|||
|
X509CertificateCollection.X509CertificateEnumerator enumerator = item.ClentCertificates.GetEnumerator();
|
|||
|
try
|
|||
|
{
|
|||
|
while (enumerator.MoveNext())
|
|||
|
{
|
|||
|
X509Certificate current = enumerator.Current;
|
|||
|
request.ClientCertificates.Add(current);
|
|||
|
}
|
|||
|
}
|
|||
|
finally
|
|||
|
{
|
|||
|
IDisposable disposable = enumerator as IDisposable;
|
|||
|
disposable?.Dispose();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 设置Cookie
|
|||
|
/// </summary>
|
|||
|
/// <param name="item">Http参数</param>
|
|||
|
private void SetCookie(HttpItem item)
|
|||
|
{
|
|||
|
if (!string.IsNullOrEmpty(item.Cookie))
|
|||
|
{
|
|||
|
request.Headers[HttpRequestHeader.Cookie] = item.Cookie;
|
|||
|
}
|
|||
|
if (item.ResultCookieType == ResultCookieType.CookieCollection)
|
|||
|
{
|
|||
|
request.CookieContainer = new CookieContainer();
|
|||
|
if (item.CookieCollection != null && item.CookieCollection.Count > 0)
|
|||
|
{
|
|||
|
request.CookieContainer.Add(item.CookieCollection);
|
|||
|
}
|
|||
|
}
|
|||
|
else if (item.ResultCookieType == ResultCookieType.CookieContainer)
|
|||
|
{
|
|||
|
request.CookieContainer = item.CookieContainer;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 设置Post数据
|
|||
|
/// </summary>
|
|||
|
/// <param name="item">Http参数</param>
|
|||
|
private void SetPostData(HttpItem item)
|
|||
|
{
|
|||
|
if (!request.Method.Trim().ToLower().Contains("get"))
|
|||
|
{
|
|||
|
if (item.PostEncoding != null)
|
|||
|
{
|
|||
|
postencoding = item.PostEncoding;
|
|||
|
}
|
|||
|
byte[] array = null;
|
|||
|
if (item.PostDataType == PostDataType.Byte && item.PostdataByte != null && item.PostdataByte.Length > 0)
|
|||
|
{
|
|||
|
array = item.PostdataByte;
|
|||
|
}
|
|||
|
else if (item.PostDataType == PostDataType.FilePath && !string.IsNullOrWhiteSpace(item.Postdata))
|
|||
|
{
|
|||
|
StreamReader streamReader = new StreamReader(item.Postdata, postencoding);
|
|||
|
array = postencoding.GetBytes(streamReader.ReadToEnd());
|
|||
|
streamReader.Close();
|
|||
|
}
|
|||
|
else if (!string.IsNullOrWhiteSpace(item.Postdata))
|
|||
|
{
|
|||
|
array = postencoding.GetBytes(item.Postdata);
|
|||
|
}
|
|||
|
if (array != null)
|
|||
|
{
|
|||
|
request.ContentLength = array.Length;
|
|||
|
request.GetRequestStream().Write(array, 0, array.Length);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
request.ContentLength = 0L;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 设置代理
|
|||
|
/// </summary>
|
|||
|
/// <param name="item">参数对象</param>
|
|||
|
private void SetProxy(HttpItem item)
|
|||
|
{
|
|||
|
bool flag = false;
|
|||
|
if (!string.IsNullOrWhiteSpace(item.ProxyIp))
|
|||
|
{
|
|||
|
flag = item.ProxyIp.ToLower().Contains("ieproxy");
|
|||
|
}
|
|||
|
if (!string.IsNullOrWhiteSpace(item.ProxyIp) && !flag)
|
|||
|
{
|
|||
|
if (item.ProxyIp.Contains(":"))
|
|||
|
{
|
|||
|
string[] array = item.ProxyIp.Split(':');
|
|||
|
WebProxy webProxy = new WebProxy(array[0].Trim(), Convert.ToInt32(array[1].Trim()));
|
|||
|
webProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd);
|
|||
|
request.Proxy = webProxy;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
WebProxy webProxy = new WebProxy(item.ProxyIp, false);
|
|||
|
webProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd);
|
|||
|
request.Proxy = webProxy;
|
|||
|
}
|
|||
|
}
|
|||
|
else if (!flag)
|
|||
|
{
|
|||
|
request.Proxy = item.WebProxy;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 回调验证证书问题
|
|||
|
/// </summary>
|
|||
|
/// <param name="sender">流对象</param>
|
|||
|
/// <param name="certificate">证书</param>
|
|||
|
/// <param name="chain">X509Chain</param>
|
|||
|
/// <param name="errors">SslPolicyErrors</param>
|
|||
|
/// <returns>bool</returns>
|
|||
|
private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 通过设置这个属性,可以在发出连接的时候绑定客户端发出连接所使用的IP地址。
|
|||
|
/// </summary>
|
|||
|
/// <param name="servicePoint"></param>
|
|||
|
/// <param name="remoteEndPoint"></param>
|
|||
|
/// <param name="retryCount"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
|
|||
|
{
|
|||
|
return _IPEndPoint;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|