/// <summary> /// 获取响应结果 /// </summary> private HttpResult GetResponse(HttpWebResponse response, HttpRequestItem item) { var result = new HttpResult(); //获取StatusCode result.StatusCode = response.StatusCode; //获取StatusDescription result.StatusDescription = response.StatusDescription; //获取Headers result.Header = response.Headers; //获取CookieCollection if (response.Cookies != null) { result.CookieCollection = response.Cookies; } //获取set-cookie if (response.Headers["set-cookie"] != null) { result.Cookie = response.Headers["set-cookie"]; } byte[] ResponseByte = GetResponseBytes(response); result.ResponseBytes = ResponseByte; var encoding = Encoding.UTF8; if (item.Encoding != null) { encoding = item.Encoding; } result.Html = encoding.GetString(ResponseByte); return(result); }
/// <summary> /// 设置代理 /// </summary> private void SetProxy(HttpWebRequest request, HttpRequestItem item) { if (item.WebProxy != null) { request.Proxy = item.WebProxy; } }
/// <summary> /// 设置多个证书 /// </summary> /// <param name="item"></param> private void SetCerList(HttpWebRequest request, HttpRequestItem item) { if (item.ClentCertificates != null && item.ClentCertificates.Count > 0) { foreach (X509Certificate c in item.ClentCertificates) { request.ClientCertificates.Add(c); } } }
/// <summary> /// 设置Cookie /// </summary> private void SetCookie(HttpWebRequest request, HttpRequestItem item) { if (!string.IsNullOrEmpty(item.Cookie)) { request.Headers[HttpRequestHeader.Cookie] = item.Cookie; } if (item.CookieContainer != null) { request.CookieContainer = item.CookieContainer; } }
/// <summary> /// 设置证书 /// </summary> private void SetCer(HttpWebRequest request, HttpRequestItem item) { if (!string.IsNullOrWhiteSpace(item.CerPath)) { //将证书添加到请求里 request.ClientCertificates.Add(new X509Certificate(item.CerPath)); } else { //多个证书 SetCerList(request, item); } }
/// <summary> /// 设置Post数据 /// </summary> private void SetPostData(HttpWebRequest request, HttpRequestItem item) { if (request.Method.Trim().ToLower().Contains("post")) { if (item.PostDatas != null) { request.ContentLength = item.PostDatas.Length; using (var requestStream = request.GetRequestStream()) { requestStream.Write(item.PostDatas, 0, item.PostDatas.Length); } } } }
public HttpResult Request(HttpRequestItem item) { var request = (HttpWebRequest)WebRequest.Create(item.URL); try { //设置证书 SetCer(request, item); //设置Header if (item.Header != null && item.Header.Count > 0) { foreach (string key in item.Header.AllKeys) { request.Headers.Add(key, item.Header[key]); } } //设置代理 SetProxy(request, item); 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; } request.Accept = item.Accept; request.ContentType = item.ContentType; request.UserAgent = item.UserAgent; request.Referer = item.Referer; request.AllowAutoRedirect = item.AllowAutoRedirect; if (item.MaximumAutomaticRedirections > 0) { request.MaximumAutomaticRedirections = item.MaximumAutomaticRedirections; } //设置安全凭证 request.Credentials = item.ICredentials; //设置Cookie SetCookie(request, item); //设置Post数据 SetPostData(request, item); //设置最大连接 if (item.Connectionlimit > 0) { request.ServicePoint.ConnectionLimit = item.Connectionlimit; } } catch (Exception ex) { return(new HttpResult() { Html = ex.Message, StatusDescription = $"请求参数错误:{ex.Message}" }); } try { //请求数据 using (var response = (HttpWebResponse)request.GetResponse()) { return(GetResponse(response, item)); } } catch (WebException ex) { using (var response = (HttpWebResponse)ex.Response) { return(GetResponse(response, item)); } } catch (Exception ex) { return(new HttpResult() { Html = ex.Message, StatusDescription = ex.Message }); } }