1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015 |
- using System;
- 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 SCC.Common
- {
-
-
-
- public class HttpHelper
- {
- #region 预定义方变量
-
- private Encoding _encoding = Encoding.Default;
-
- private Encoding _postencoding = Encoding.Default;
-
- private HttpWebRequest _request = null;
-
- private HttpWebResponse _response = null;
-
- private IPEndPoint _ipEndPoint = null;
- #endregion
- #region Public
-
-
-
-
-
- public HttpResult GetHtml(HttpItem item)
- {
-
- HttpResult result = new HttpResult();
- try
- {
-
- SetRequest(item);
- }
- catch (Exception ex)
- {
- result.Cookie = string.Empty;
- result.Header = null;
- result.Html = ex.Message;
- result.StatusDescription = "配置参数时出错:" + ex.Message;
-
- return result;
- }
- try
- {
-
- using (_response = (HttpWebResponse)_request.GetResponse())
- {
- GetData(item, result);
- }
- }
- catch (WebException ex)
- {
- if (ex.Response != null)
- {
- using (_response = (HttpWebResponse)ex.Response)
- {
- GetData(item, result);
- }
- }
- else
- {
- result.Html = ex.Message;
- }
- }
- catch (Exception ex)
- {
- result.Html = ex.Message;
- }
- if (item.IsToLower)
- result.Html = result.Html.ToLower();
- return result;
- }
- #endregion
- #region GetData
-
-
-
-
-
- private void GetData(HttpItem item, HttpResult result)
- {
- #region base
-
- result.StatusCode = _response.StatusCode;
-
- result.StatusDescription = _response.StatusDescription;
-
- result.ResponseUri = _response.ResponseUri.ToString();
-
- result.Header = _response.Headers;
-
- if (_response.Cookies != null) result.CookieCollection = _response.Cookies;
-
- if (_response.Headers["set-cookie"] != null) result.Cookie = _response.Headers["set-cookie"];
- #endregion
- #region byte
-
- byte[] responseByte = GetByte();
- #endregion
- #region Html
- if (responseByte != null && responseByte.Length > 0)
- {
-
- SetEncoding(item, result, responseByte);
-
- result.Html = _encoding.GetString(responseByte);
- }
- else
- {
-
- result.Html = string.Empty;
- }
- #endregion
- }
-
-
-
-
-
-
- private void SetEncoding(HttpItem item, HttpResult result, byte[] responseByte)
- {
-
- if (item.ResultType == ResultType.Byte) result.ResultByte = responseByte;
-
- if (_encoding == null)
- {
- Match meta = Regex.Match(Encoding.Default.GetString(responseByte), "<meta[^<]*charset=([^<]*)[\"']", RegexOptions.IgnoreCase);
- string c = string.Empty;
- if (meta != null && meta.Groups.Count > 0)
- {
- c = meta.Groups[1].Value.ToLower().Trim();
- }
- if (c.Length > 2)
- {
- try
- {
- _encoding = Encoding.GetEncoding(c.Replace("\"", string.Empty).Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk").Trim());
- }
- catch
- {
- _encoding = string.IsNullOrEmpty(_response.CharacterSet) ? Encoding.UTF8 : Encoding.GetEncoding(_response.CharacterSet);
- }
- }
- else
- {
- _encoding = string.IsNullOrEmpty(_response.CharacterSet) ? Encoding.UTF8 : Encoding.GetEncoding(_response.CharacterSet);
- }
- }
- }
-
-
-
-
- private byte[] GetByte()
- {
- byte[] responseByte = null;
- MemoryStream stream;
-
- if (_response.ContentEncoding != null && _response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
- {
-
- stream = GetMemoryStream(new GZipStream(_response.GetResponseStream(), CompressionMode.Decompress));
- }
- else
- {
-
- stream = GetMemoryStream(_response.GetResponseStream());
- }
-
- responseByte = stream.ToArray();
- stream.Close();
- return responseByte;
- }
-
-
-
-
- private MemoryStream GetMemoryStream(Stream streamResponse)
- {
- MemoryStream stream = new MemoryStream();
- int Length = 256;
- Byte[] buffer = new Byte[Length];
- int bytesRead = streamResponse.Read(buffer, 0, Length);
- while (bytesRead > 0)
- {
- stream.Write(buffer, 0, bytesRead);
- bytesRead = streamResponse.Read(buffer, 0, Length);
- }
- return stream;
- }
- #endregion
- #region SetRequest
-
-
-
-
- private void SetRequest(HttpItem item)
- {
-
- SetCer(item);
- if (item.IPEndPoint != null)
- {
- _ipEndPoint = item.IPEndPoint;
-
- _request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);
- }
-
- if (item.Header != null && item.Header.Count > 0)
- {
- foreach (string key in item.Header.AllKeys)
- {
- _request.Headers.Add(key, item.Header[key]);
- }
- }
-
- SetProxy(item);
- if (item.ProtocolVersion != 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 (item.IfModifiedSince != null)
- {
-
- _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;
- }
- }
-
-
-
-
- private void SetCer(HttpItem item)
- {
- if (!string.IsNullOrEmpty(item.CerPath))
- {
-
- ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
-
- _request = (HttpWebRequest)WebRequest.Create(item.Url);
- SetCerList(item);
-
- _request.ClientCertificates.Add(new X509Certificate(item.CerPath));
- }
- else
- {
-
- _request = (HttpWebRequest)WebRequest.Create(item.Url);
- SetCerList(item);
- }
- }
-
-
-
-
- private void SetCerList(HttpItem item)
- {
- if (item.ClentCertificates != null && item.ClentCertificates.Count > 0)
- {
- foreach (X509Certificate c in item.ClentCertificates)
- {
- _request.ClientCertificates.Add(c);
- }
- }
- }
-
-
-
-
- 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);
- }
- }
-
-
-
-
- private void SetPostData(HttpItem item)
- {
-
- if (!_request.Method.Trim().ToLower().Contains("get"))
- {
- if (item.PostEncoding != null)
- {
- _postencoding = item.PostEncoding;
- }
- byte[] buffer = null;
-
- if (item.PostDataType == PostDataType.Byte && item.PostdataByte != null && item.PostdataByte.Length > 0)
- {
-
- buffer = item.PostdataByte;
- }
- else if (item.PostDataType == PostDataType.FilePath && !string.IsNullOrEmpty(item.Postdata))
- {
- StreamReader r = new StreamReader(item.Postdata, _postencoding);
- buffer = _postencoding.GetBytes(r.ReadToEnd());
- r.Close();
- }
- else if (!string.IsNullOrEmpty(item.Postdata))
- {
- buffer = _postencoding.GetBytes(item.Postdata);
- }
- if (buffer != null)
- {
- _request.ContentLength = buffer.Length;
- _request.GetRequestStream().Write(buffer, 0, buffer.Length);
- }
- else
- {
- _request.ContentLength = 0;
- }
- }
- }
-
-
-
-
- private void SetProxy(HttpItem item)
- {
- bool isIeProxy = false;
- if (!string.IsNullOrEmpty(item.ProxyIp))
- {
- isIeProxy = item.ProxyIp.ToLower().Contains("ieproxy");
- }
- if (!string.IsNullOrEmpty(item.ProxyIp) && !isIeProxy)
- {
-
- if (item.ProxyIp.Contains(":"))
- {
- string[] plist = item.ProxyIp.Split(':');
- WebProxy myProxy = new WebProxy(plist[0].Trim(), Convert.ToInt32(plist[1].Trim()))
- {
-
- Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd)
- };
-
- _request.Proxy = myProxy;
- }
- else
- {
- WebProxy myProxy = new WebProxy(item.ProxyIp, false)
- {
-
- Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd)
- };
-
- _request.Proxy = myProxy;
- }
- }
- else if (isIeProxy)
- {
-
- }
- else
- {
- _request.Proxy = item.WebProxy;
- }
- }
- #endregion
- #region private main
-
-
-
-
-
-
-
-
- private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }
-
-
-
-
-
-
-
- private IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
- {
- return _ipEndPoint;
- }
- #endregion
- }
-
-
-
- public class HttpItem
- {
- private string _url = string.Empty;
-
-
-
- public string Url
- {
- get { return _url; }
- set { _url = value; }
- }
- private string _method = "GET";
-
-
-
- public string Method
- {
- get { return _method; }
- set { _method = value; }
- }
- private int _timeout = 90 * 1000;
-
-
-
- public int Timeout
- {
- get { return _timeout; }
- set { _timeout = value; }
- }
- private int _readWriteTimeout = 60 * 1000;
-
-
-
- public int ReadWriteTimeout
- {
- get { return _readWriteTimeout; }
- set { _readWriteTimeout = value; }
- }
- private Boolean _keepAlive = true;
-
-
-
- public Boolean KeepAlive
- {
- get { return _keepAlive; }
- set { _keepAlive = value; }
- }
- private string _accept = "text/html, application/xhtml+xml, */*";
-
-
-
-
- public string Accept
- {
- get { return _accept; }
- set { _accept = value; }
- }
- private string _contentType = "text/html";
-
-
-
-
-
-
-
- public string ContentType
- {
- get { return _contentType; }
- set { _contentType = value; }
- }
- private string _userAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public string UserAgent
- {
- get { return _userAgent; }
- set { _userAgent = value; }
- }
- private Encoding _encoding = null;
-
-
-
- public Encoding Encoding
- {
- get { return _encoding; }
- set { _encoding = value; }
- }
- private PostDataType _postDataType = PostDataType.String;
-
-
-
- public PostDataType PostDataType
- {
- get { return _postDataType; }
- set { _postDataType = value; }
- }
- private string _postdata = string.Empty;
-
-
-
- public string Postdata
- {
- get { return _postdata; }
- set { _postdata = value; }
- }
- private byte[] _postdataByte = null;
-
-
-
- public byte[] PostdataByte
- {
- get { return _postdataByte; }
- set { _postdataByte = value; }
- }
- private WebProxy _webProxy;
-
-
-
- public WebProxy WebProxy
- {
- get { return _webProxy; }
- set { _webProxy = value; }
- }
- private CookieCollection _cookiecollection = null;
-
-
-
- public CookieCollection CookieCollection
- {
- get { return _cookiecollection; }
- set { _cookiecollection = value; }
- }
- private string _cookie = string.Empty;
-
-
-
- public string Cookie
- {
- get { return _cookie; }
- set { _cookie = value; }
- }
- private string _referer = string.Empty;
-
-
-
- public string Referer
- {
- get { return _referer; }
- set { _referer = value; }
- }
- private string _cerPath = string.Empty;
-
-
-
- public string CerPath
- {
- get { return _cerPath; }
- set { _cerPath = value; }
- }
- private Boolean _isToLower = false;
-
-
-
- public Boolean IsToLower
- {
- get { return _isToLower; }
- set { _isToLower = value; }
- }
- private Boolean _allowautoredirect = false;
-
-
-
- public Boolean Allowautoredirect
- {
- get { return _allowautoredirect; }
- set { _allowautoredirect = value; }
- }
- private int _connectionlimit = 1024;
-
-
-
- public int Connectionlimit
- {
- get { return _connectionlimit; }
- set { _connectionlimit = value; }
- }
- private string _proxyusername = string.Empty;
-
-
-
- public string ProxyUserName
- {
- get { return _proxyusername; }
- set { _proxyusername = value; }
- }
- private string _proxypwd = string.Empty;
-
-
-
- public string ProxyPwd
- {
- get { return _proxypwd; }
- set { _proxypwd = value; }
- }
- private string _proxyip = string.Empty;
-
-
-
- public string ProxyIp
- {
- get { return _proxyip; }
- set { _proxyip = value; }
- }
- private ResultType _resulttype = ResultType.String;
-
-
-
- public ResultType ResultType
- {
- get { return _resulttype; }
- set { _resulttype = value; }
- }
- private WebHeaderCollection _header = new WebHeaderCollection();
-
-
-
- public WebHeaderCollection Header
- {
- get { return _header; }
- set { _header = value; }
- }
- private Version _protocolVersion = System.Net.HttpVersion.Version11;
-
-
-
- public Version ProtocolVersion
- {
- get { return _protocolVersion; }
- set { _protocolVersion = value; }
- }
- private Boolean _expect100Continue = false;
-
-
-
- public Boolean Expect100Continue
- {
- get { return _expect100Continue; }
- set { _expect100Continue = value; }
- }
- private X509CertificateCollection _clentCertificates;
-
-
-
- public X509CertificateCollection ClentCertificates
- {
- get { return _clentCertificates; }
- set { _clentCertificates = value; }
- }
- private Encoding _postEncoding = Encoding.Default;
-
-
-
- public Encoding PostEncoding
- {
- get { return _postEncoding; }
- set { _postEncoding = value; }
- }
- private ResultCookieType _resultCookieType = ResultCookieType.String;
-
-
-
- public ResultCookieType ResultCookieType
- {
- get { return _resultCookieType; }
- set { _resultCookieType = value; }
- }
- private ICredentials _iCredentials = CredentialCache.DefaultCredentials;
-
-
-
- public ICredentials ICredentials
- {
- get { return _iCredentials; }
- set { _iCredentials = value; }
- }
-
-
-
- private int _maximumAutomaticRedirections;
- public int MaximumAutomaticRedirections
- {
- get { return _maximumAutomaticRedirections; }
- set { _maximumAutomaticRedirections = value; }
- }
- private DateTime? _ifModifiedSince = null;
-
-
-
- public DateTime? IfModifiedSince
- {
- get { return _ifModifiedSince; }
- set { _ifModifiedSince = value; }
- }
- #region ip-port
- private IPEndPoint _ipEndPoint = null;
-
-
-
-
-
-
- public IPEndPoint IPEndPoint
- {
- get { return _ipEndPoint; }
- set { _ipEndPoint = value; }
- }
- #endregion
- }
-
-
-
- public class HttpResult
- {
- private string _cookie;
-
-
-
- public string Cookie
- {
- get { return _cookie; }
- set { _cookie = value; }
- }
- private CookieCollection _cookieCollection;
-
-
-
- public CookieCollection CookieCollection
- {
- get { return _cookieCollection; }
- set { _cookieCollection = value; }
- }
- private string _html = string.Empty;
-
-
-
- public string Html
- {
- get { return _html; }
- set { _html = value; }
- }
- private byte[] _resultByte;
-
-
-
- public byte[] ResultByte
- {
- get { return _resultByte; }
- set { _resultByte = value; }
- }
- private WebHeaderCollection _header;
-
-
-
- public WebHeaderCollection Header
- {
- get { return _header; }
- set { _header = value; }
- }
- private string _statusDescription;
-
-
-
- public string StatusDescription
- {
- get { return _statusDescription; }
- set { _statusDescription = value; }
- }
- private HttpStatusCode _statusCode;
-
-
-
- public HttpStatusCode StatusCode
- {
- get { return _statusCode; }
- set { _statusCode = value; }
- }
-
-
-
- public string ResponseUri { get; set; }
-
-
-
- public string RedirectUrl
- {
- get
- {
- try
- {
- if (Header != null && Header.Count > 0)
- {
- string baseurl = Header["location"].ToString().Trim();
- string locationurl = baseurl.ToLower();
- if (!string.IsNullOrWhiteSpace(locationurl))
- {
- bool b = locationurl.StartsWith("http://") || locationurl.StartsWith("https://");
- if (!b)
- {
- baseurl = new Uri(new Uri(ResponseUri), baseurl).AbsoluteUri;
- }
- }
- return baseurl;
- }
- }
- catch { }
- return string.Empty;
- }
- }
- }
-
-
-
- public enum ResultType
- {
-
-
-
- String,
-
-
-
- Byte
- }
-
-
-
- public enum PostDataType
- {
-
-
-
- String,
-
-
-
- Byte,
-
-
-
- FilePath
- }
-
-
-
- public enum ResultCookieType
- {
-
-
-
- String,
-
-
-
- CookieCollection
- }
- }
|