internal HttpWebResponse(Uri responseUri, KnownHttpVerb verb, CoreResponseData coreData, string mediaType, bool usesProxySemantics, DecompressionMethods decompressionMethod) { m_Uri = responseUri; m_Verb = verb; m_MediaType = mediaType; m_UsesProxySemantics = usesProxySemantics; m_ConnectStream = coreData.m_ConnectStream; m_HttpResponseHeaders = coreData.m_ResponseHeaders; m_ContentLength = coreData.m_ContentLength; m_StatusCode = coreData.m_StatusCode; m_StatusDescription = coreData.m_StatusDescription; m_IsVersionHttp11 = coreData.m_IsVersionHttp11; //if the returned contentlength is zero, preemptively invoke calldone on the stream. //this will wake up any pending reads. if (m_ContentLength == 0 && m_ConnectStream is ConnectStream) { ((ConnectStream)m_ConnectStream).CallDone(); } // handle Content-Location header, by combining it with the orginal request. string contentLocation = m_HttpResponseHeaders[HttpKnownHeaderNames.ContentLocation]; if (contentLocation != null) { try { m_Uri = new Uri(m_Uri, contentLocation); } catch (UriFormatException e) { GlobalLog.Assert("Exception on response Uri parsing.", e.ToString()); } } // decompress responses by hooking up a final response Stream - only if user required it if(decompressionMethod != DecompressionMethods.None) { string contentEncoding = m_HttpResponseHeaders[HttpKnownHeaderNames.ContentEncoding]; if (contentEncoding != null){ if(((decompressionMethod & DecompressionMethods.GZip) != 0) && contentEncoding.IndexOf(HttpWebRequest.GZipHeader) != -1) { m_ConnectStream = new GZipWrapperStream(m_ConnectStream, CompressionMode.Decompress); m_ContentLength = -1; // unknown on compressed streams // Setting a response header after parsing will ruin the Common Header optimization. // This seems like a corner case. ContentEncoding could be added as a common header, with a special // property allowing it to be nulled. m_HttpResponseHeaders[HttpKnownHeaderNames.ContentEncoding] = null; } else if (((decompressionMethod & DecompressionMethods.Deflate) != 0) && contentEncoding.IndexOf(HttpWebRequest.DeflateHeader) != -1) { m_ConnectStream = new DeflateWrapperStream(m_ConnectStream, CompressionMode.Decompress); m_ContentLength = -1; // unknown on compressed streams // Setting a response header after parsing will ruin the Common Header optimization. // This seems like a corner case. ContentEncoding could be added as a common header, with a special // property allowing it to be nulled. m_HttpResponseHeaders[HttpKnownHeaderNames.ContentEncoding] = null; } } } }
internal HttpWebResponse(Uri responseUri, KnownHttpVerb verb, CoreResponseData coreData, string mediaType, bool usesProxySemantics, DecompressionMethods decompressionMethod) { this.m_Uri = responseUri; this.m_Verb = verb; this.m_MediaType = mediaType; this.m_UsesProxySemantics = usesProxySemantics; this.m_ConnectStream = coreData.m_ConnectStream; this.m_HttpResponseHeaders = coreData.m_ResponseHeaders; this.m_ContentLength = coreData.m_ContentLength; this.m_StatusCode = coreData.m_StatusCode; this.m_StatusDescription = coreData.m_StatusDescription; this.m_IsVersionHttp11 = coreData.m_IsVersionHttp11; if ((this.m_ContentLength == 0L) && (this.m_ConnectStream is ConnectStream)) { ((ConnectStream) this.m_ConnectStream).CallDone(); } string relativeUri = this.m_HttpResponseHeaders["Content-Location"]; if (relativeUri != null) { try { this.m_Uri = new Uri(this.m_Uri, relativeUri); } catch (UriFormatException) { } } if (decompressionMethod != DecompressionMethods.None) { string str2 = this.m_HttpResponseHeaders["Content-Encoding"]; if (str2 != null) { if (((decompressionMethod & DecompressionMethods.GZip) != DecompressionMethods.None) && (str2.IndexOf("gzip") != -1)) { this.m_ConnectStream = new GZipWrapperStream(this.m_ConnectStream, CompressionMode.Decompress); this.m_ContentLength = -1L; this.m_HttpResponseHeaders["Content-Encoding"] = null; } else if (((decompressionMethod & DecompressionMethods.Deflate) != DecompressionMethods.None) && (str2.IndexOf("deflate") != -1)) { this.m_ConnectStream = new DeflateWrapperStream(this.m_ConnectStream, CompressionMode.Decompress); this.m_ContentLength = -1L; this.m_HttpResponseHeaders["Content-Encoding"] = null; } } } }
internal HttpResponse(Uri responseUri, HttpMethod method, CoreResponseData coreData, DecompressionMethods decompressionMethod) { _uri = responseUri; _method = method; _coreResponseData = coreData; _connectStream = coreData.ConnectStream; _headers = coreData.ResponseHeaders; _statusCode = coreData.StatusCode; _contentLength = coreData.ContentLength; _statusDescription = coreData.StatusDescription; _version = coreData.HttpVersion; //if (this.m_ContentLength == 0L && this.m_ConnectStream is ConnectStream) //{ // ((ConnectStream)this.m_ConnectStream).CallDone(); //} var text = _headers[HttpHeaderNames.ContentLocation]; if (text != null) { try { Uri uri; if (Uri.TryCreate(_uri, text, out uri)) { _uri = uri; } } catch { } } if (decompressionMethod != DecompressionMethods.None) { if ((text = _headers[HttpHeaderNames.ContentEncoding]) != null) { if ((decompressionMethod & DecompressionMethods.GZip) == DecompressionMethods.GZip && text.IndexOf(HttpRequest.GZipHeader, 0, StringComparison.OrdinalIgnoreCase) >= 0) { _connectStream = new GZipStream(_connectStream, CompressionMode.Decompress); _contentLength = -1L; _headers.SetInternal(HttpHeaderNames.TransferEncoding, null); } else if ((decompressionMethod & DecompressionMethods.Deflate) == DecompressionMethods.Deflate && text.IndexOf(HttpRequest.DeflateHeader, 0, StringComparison.OrdinalIgnoreCase) >= 0) { _connectStream = new DeflateStream(_connectStream, CompressionMode.Decompress); _contentLength = -1L; _headers.SetInternal(HttpHeaderNames.TransferEncoding, null); } } } }
public static void SetResponse(DecompressionMethods compressionKind, string responseBody) { byte[] responseBodyBytes = Encoding.UTF8.GetBytes(responseBody); byte[] compressedBytes; string responseHeadersFormat = "HTTP/1.1 200 OK\r\n" + "{0}" + "Content-Length: {1}\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "Date: Tue, 08 Jul 2014 20:45:02 GMT\r\n"; if (compressionKind == DecompressionMethods.None) { compressedBytes = responseBodyBytes; } else { using (var memoryStream = new MemoryStream()) { Stream compressedStream = null; if (compressionKind == DecompressionMethods.Deflate) { compressedStream = new DeflateStream(memoryStream, CompressionMode.Compress); } else { compressedStream = new GZipStream(memoryStream, CompressionMode.Compress); } compressedStream.Write(responseBodyBytes, 0, responseBodyBytes.Length); compressedStream.Dispose(); compressedBytes = memoryStream.ToArray(); } } ResponseBody = compressedBytes; string contentEncodingHeader = string.Empty; if (compressionKind != DecompressionMethods.None) { contentEncodingHeader = string.Format("Content-Encoding: {0}\r\n", compressionKind.ToString().ToLower()); } ResponseHeaders = string.Format(responseHeadersFormat, contentEncodingHeader, compressedBytes.Length); }
public HttpClientChannel() { this.getRequestStreamCallback = GetRequestStreamCallback; this.getResponseCallback = GetResponseCallback; // Set HWR default values this.allowAutoRedirect = true; this.maxRequestContentBufferSize = HttpContent.DefaultMaxBufferSize; this.automaticDecompression = DecompressionMethods.None; this.clientCertificates = null; // only create collection when required. this.cookieContainer = new CookieContainer(); // default container used for dealing with auto-cookies. this.credentials = null; this.maxAutomaticRedirections = 50; this.preAuthenticate = false; this.proxy = null; this.useProxy = true; this.useCookies = true; // deal with cookies by default. this.useDefaultCredentials = false; }
public HttpRequest(HttpMethod method, Uri uri, HttpVersion version) { _allowAutoRedirect = true; _automaticDecompression = DecompressionMethods.None; _contentLength = -1L; _httpWriteMode = HttpWriteMode.Unknown; _keepAlive = true; _maximumResponseHeadersLength = 8190; _maxAutomaticRedirections = 50; _originUri = uri; _originMethod = method; _timeout = 100000; _uri = _originUri; _method = _originMethod; _version = version; _sendChunked = false; _headers = new HttpRequestHeaders(); _startTimestamp = DateTime.UtcNow.Ticks; }
private static async Task SendAsync_WithMockedInnerHandler_DecodesResponse(DecompressionMethods decompressionMethods, Func<string, Mock<HttpMessageHandler>> getHandlerMock) { // ARRANGE var mock = getHandlerMock(OriginalContent); var pipeline = new DecompressingHandler { AutomaticDecompression = decompressionMethods, InnerHandler = mock.Object }; var client = new HttpClient(pipeline); // ACT HttpResponseMessage response = await client.GetAsync("http://localhost"); // ASSERT string actualContent = await response.Content.ReadAsStringAsync(); response.Content.Headers.ContentEncoding.Should().BeEmpty(); actualContent.Should().Be(OriginalContent); mock .Protected() .Verify("SendAsync", Times.Once(), ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()); }
public AsyncHttpClient AutomaticDecompression(DecompressionMethods automaticDecompression) { _automaticDecompression = automaticDecompression; return(this); }
/// <summary> /// Set the request's <see cref="DecompressionMethods"/> /// </summary> /// <param name="decompressionMethods"></param> /// <returns></returns> public RequestParameters SetAutomaticDecompression(DecompressionMethods decompressionMethods) { _decompressionMethod = decompressionMethods; return(this); }
private void ApplyDecompressionSetting(HttpRequestMessage request, DecompressionMethods method, string methodName) { if ((AutomaticDecompression & method) == method) { bool found = false; foreach (StringWithQualityHeaderValue encoding in request.Headers.AcceptEncoding) { if (methodName.Equals(encoding.Value, StringComparison.OrdinalIgnoreCase)) { found = true; break; } } if (!found) { request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue(methodName)); } } }
static void \u206B([In] HttpClientHandler obj0, [In] DecompressionMethods obj1) { // ISSUE: unable to decompile the method. }
public static XmlDocument GetHttpXmlDocument(string adress, string username, string password, DecompressionMethods decompressionMethod) { //first we get a Stream of the Xml, for that we use a webrequest with username and password and compression Stream resStream = GetHttpStream(adress, username, password, decompressionMethod); //Then we use the Parser method and return it return(ParseStreamToXmlDocument(resStream)); }
/// <summary> /// Returns decompressed strem from file. /// </summary> /// <param name="fileName">source file</param> /// <param name="method">method used for compression (none, deflate, gzip)</param> /// <returns>output stream</returns> public static Stream DecompressStream(string fileName, DecompressionMethods method) { // TODO : Implement DecompressStream method throw new NotImplementedException(); }
private void SetAutomaticDecompression(DecompressionMethods value) => InvokeNativeHandlerMethod("set_AutomaticDecompression", value);
public static string Get(string url, string referer, string userAgent, string accept, int timeout, WebHeaderCollection header, CookieCollection cookies, Encoding encoding, DecompressionMethods decompression = DecompressionMethods.None) { string result = string.Empty; try { HttpWebRequest httpWebRequest = null; if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult; httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(url)); httpWebRequest.ProtocolVersion = HttpVersion.Version10; } else { httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(url)); } httpWebRequest.UserAgent = userAgent; httpWebRequest.Referer = referer; httpWebRequest.Method = "GET"; httpWebRequest.Timeout = timeout; httpWebRequest.Accept = accept; if (cookies != null) { httpWebRequest.CookieContainer = new CookieContainer(); httpWebRequest.CookieContainer.Add(cookies); } if (header != null) { httpWebRequest.Headers = header; } httpWebRequest.AutomaticDecompression = decompression; HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse(); result = GetResponseString(webResponse, encoding); return(result); } catch { return(result); } }
public static void CopyTo(this Stream source, Stream destination, CopyToOptions options, CompressionMode mode, DecompressionMethods method = DecompressionMethods.Deflate, int bufferSize = 4096, long maxCount = -1, Action <CopyToEventArgs> onProgress = null) { if (mode == CompressionMode.Compress) { switch (method) { case DecompressionMethods.Deflate: { using (DeflateStream deflaterStream = new DeflateStream(destination, CompressionMode.Compress, true)) { source.CopyTo(deflaterStream, options, bufferSize, maxCount, onProgress); deflaterStream.Close(); } } break; case DecompressionMethods.GZip: { using (GZipStream gzipStream = new GZipStream(destination, CompressionMode.Compress, true)) { source.CopyTo(gzipStream, options, bufferSize, maxCount, onProgress); gzipStream.Close(); } } break; default: { source.CopyTo(destination, options, bufferSize, maxCount, onProgress); } break; } } else { switch (method) { case DecompressionMethods.Deflate: { using (DeflateStream deflaterStream = new DeflateStream(source, CompressionMode.Decompress, true)) { deflaterStream.CopyTo(destination, options, bufferSize, maxCount, onProgress); deflaterStream.Close(); } } break; case DecompressionMethods.GZip: { using (GZipStream gzipStream = new GZipStream(source, CompressionMode.Decompress, true)) { gzipStream.CopyTo(destination, options, bufferSize, maxCount, onProgress); gzipStream.Close(); } } break; default: { source.CopyTo(destination, options, bufferSize, maxCount, onProgress); } break; } } }
/// <summary> /// 向HTTP服务器发送Get请求。 /// </summary> /// <param name="url">请求地址。</param> /// <param name="referer">参照页。</param> /// <param name="userAgent">User-Agent HTTP标头。</param> /// <param name="accept">Accept HTTP标头。</param> /// <param name="timeout">超时时间。</param> /// <param name="header">HTTP 标头。</param> /// <param name="cookies">Cookies。</param> /// <param name="encoding">文本编码。</param> /// <param name="decompression">加密方式。</param> /// <returns></returns> public static string Get(string url, string referer, string userAgent, string accept, int timeout, WebHeaderCollection header, CookieCollection cookies, Encoding encoding, DecompressionMethods decompression = DecompressionMethods.None) { string content = String.Empty; try { HttpWebRequest webRequest = null; if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { //对服务端证书进行有效性校验(非第三方权威机构颁发的证书,如自己生成的,不进行验证,这里返回true) ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url)); webRequest.ProtocolVersion = HttpVersion.Version10; //http版本,默认是1.1,这里设置为1.0 } else { webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url)); } //初始WebRequest属性 webRequest.UserAgent = userAgent; webRequest.Referer = referer; webRequest.Method = "GET"; webRequest.Timeout = timeout; webRequest.Accept = accept; if (cookies != null) { webRequest.CookieContainer = new CookieContainer(); webRequest.CookieContainer.Add(cookies); } if (header != null) { webRequest.Headers = header; } webRequest.AutomaticDecompression = decompression; //接收返回字串 HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); content = GetResponseString(webResponse, encoding); } catch { } return(content); }
static public async Task Get(string url, Dictionary <string, string> headers, DecompressionMethods decompress = DecompressionMethods.GZip, Action <string> callback = null) { try { var header = GetObj(headers); var handler = new HttpClientHandler() { UseCookies = false, AutomaticDecompression = decompress }; HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, url); SetHead(req.Headers, headers); HttpClient client = new HttpClient(handler); client.DefaultRequestHeaders.Add("User-Agent", header.UserAgent); var res = await client.SendAsync(req); var str = await res.Content.ReadAsStringAsync(); if (callback != null) { callback(str); } } catch (Exception ex) { if (callback != null) { callback(ex.Message); } } }
/// <summary> /// 向HTTP服务器发送Post请求。 /// </summary> /// <param name="url">请求地址。</param> /// <param name="parameters">请求参数。</param> /// <param name="referer">参照页。</param> /// <param name="userAgent">User-Agent HTTP标头。</param> /// <param name="accept">Accept HTTP标头。</param> /// <param name="timeout">超时时间。</param> /// <param name="header">HTTP 标头。</param> /// <param name="cookies">Cookies。</param> /// <param name="encoding">文本编码。</param> /// <param name="decompression">加密方式。</param> /// <returns></returns> public static string Post(string url, Dictionary <string, string> parameters, string referer, string userAgent, string accept, int timeout, WebHeaderCollection header, CookieCollection cookies, Encoding encoding, DecompressionMethods decompression = DecompressionMethods.None) { string content = String.Empty; try { HttpWebRequest webRequest = null; if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { //对服务端证书进行有效性校验(非第三方权威机构颁发的证书,如自己生成的,不进行验证,这里返回true) ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url)); webRequest.ProtocolVersion = HttpVersion.Version10; //http版本,默认是1.1,这里设置为1.0 } else { webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url)); } //初始WebRequest属性 webRequest.UserAgent = userAgent; webRequest.Referer = referer; webRequest.Method = "POST"; webRequest.Timeout = timeout; webRequest.Accept = accept; if (cookies != null) { webRequest.CookieContainer = new CookieContainer(); webRequest.CookieContainer.Add(cookies); } if (header != null) { webRequest.Headers = header; } webRequest.AutomaticDecompression = decompression; webRequest.ContentType = "application/x-www-form-urlencoded"; if (parameters != null && parameters.Count > 0) { StringBuilder postData = new StringBuilder(); int i = 0; foreach (string key in parameters.Keys) { postData.AppendFormat("{0}={1}", key, parameters[key]); if (i != parameters.Keys.Count - 1) { postData.Append("&"); } i++; } webRequest.ContentLength = postData.ToString().Length; //获取请求数据的流 byte[] buffer = Encoding.ASCII.GetBytes(postData.ToString()); using (Stream stream = webRequest.GetRequestStream()) { stream.Write(buffer, 0, buffer.Length); } } //接收返回字符串 HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); content = GetResponseString(webResponse, encoding); } catch { } return(content); }
public RTHttpClientHandler() { _rtFilter = new RTHttpBaseProtocolFilter(); _handlerToFilter = new RTHttpHandlerToFilter(_rtFilter); InnerHandler = _handlerToFilter; // TODO: Fix up client certificate options _clientCertificates = new X509Certificate2Collection(); InitRTCookieUsageBehavior(); _useCookies = true; // deal with cookies by default. _cookieContainer = new CookieContainer(); // default container used for dealing with auto-cookies. // Managed at this layer for granularity, but uses the desktop default. _rtFilter.AutomaticDecompression = false; _automaticDecompression = DecompressionMethods.None; // We don't support using the UI model in HttpBaseProtocolFilter() especially for auto-handling 401 responses. _rtFilter.AllowUI = false; // The .NET Desktop System.Net Http APIs (based on HttpWebRequest/HttpClient) uses no caching by default. // To preserve app-compat, we turn off caching (as much as possible) in the WinRT HttpClient APIs. _rtFilter.CacheControl.ReadBehavior = RTHttpCacheReadBehavior.MostRecent; _rtFilter.CacheControl.WriteBehavior = RTHttpCacheWriteBehavior.NoCache; }
/// <summary> /// 设置HttpWebRequest对象 /// </summary> /// <param name="url"></param> /// <param name="method"></param> /// <param name="cc"></param> /// <param name="referUrl"></param> /// <param name="nocache"></param> /// <param name="dm"></param> private void SetHttpRequestOptions(string url, string method, CookieCollection cc, string referUrl, bool nocache, DecompressionMethods dm) { httpRequest = (HttpWebRequest)HttpWebRequest.Create(url); httpRequest.UnsafeAuthenticatedConnectionSharing = true; httpRequest.ServicePoint.ConnectionLimit = MyConnectionLimit; if (null != this.webProxySrv) { httpRequest.Proxy = this.webProxySrv; } if (this.certificatedMode && url.ToLower().Substring(0, 5).Equals("https")) { ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); if (null == this.credentialCache) { httpRequest.UseDefaultCredentials = true; } else { httpRequest.Credentials = this.credentialCache; } if (!string.IsNullOrEmpty(this.certFilepath)) { httpRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile(this.certFilepath)); } } httpRequest.CookieContainer = this.cookieContainer; if (!string.IsNullOrEmpty(referUrl)) { httpRequest.Referer = referUrl; } httpRequest.AutomaticDecompression = dm; httpRequest.ServicePoint.Expect100Continue = false; httpRequest.ServicePoint.UseNagleAlgorithm = false; httpRequest.ContentType = "application/x-www-form-urlencoded"; // httpRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*"; // httpRequest.AllowWriteStreamBuffering = true; 默认值就是true // httpRequest.AllowAutoRedirect = true; 默认值就是true httpRequest.Method = method; httpRequest.Timeout = 30000;//Convert.ToInt32(ConfigurationManager.AppSettings["HTTP_REQUEST_TIMEOUT"]); // 读写超时 //httpRequest.ReadWriteTimeout = ApplicationConfig.HTTP_REQUEST_TIMEOUT; // httpRequest.MaximumAutomaticRedirections = 50; 默认值就是50 httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; .NET4.0E)"; httpRequest.Headers.Add("Accept-Language", "zh-CN"); // httpRequest.Headers.Add("UA-CPU", "x86"); httpRequest.Headers.Add("Accept-Encoding", "gzip, deflate"); if (nocache) { httpRequest.Headers.Add("Cache-Control", "no-cache"); //httpRequest.Headers.Add("Pragma", "no-cache"); } if (null != cc) { httpRequest.CookieContainer.Add(cc); } // 回调发起请求前事件 if (null != this.OnGetPostReadyHandler) { try { this.OnGetPostReadyHandler(this.httpRequest); //Logger.WriteError("KeepAlive = " + this.httpRequest.KeepAlive.ToString()); } catch (System.Exception ex) { this.LastAccessError = true; LogHelper.WriteLog(ex); } } }
private void SetHttpRequestOptions_Accept(string url, string method, CookieCollection cc, string referUrl, bool nocache, DecompressionMethods dm, string httpAccept) { this.SetHttpRequestOptions(url, method, cc, referUrl, nocache, dm); this.httpRequest.Accept = httpAccept; }
static void \u206B([In] HttpWebRequest obj0, [In] DecompressionMethods obj1) { // ISSUE: unable to decompile the method. }
public static string Post(string url, Dictionary <string, string> parameters, DecompressionMethods decompression = DecompressionMethods.None) { return(Post(url, parameters, "", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 30000, null, null, Encoding.UTF8, decompression)); }
/// <summary> /// 获取 HttpClientHandler 对象 /// </summary> /// <param name="cookieContainer"></param> /// <param name="webProxy"></param> /// <returns></returns> public static HttpClientHandler GetHttpClientHandler(CookieContainer cookieContainer = null, IWebProxy webProxy = null, DecompressionMethods decompressionMethods = DecompressionMethods.None) { var httpClientHandler = new HttpClientHandler() { UseProxy = webProxy != null, Proxy = webProxy, UseCookies = cookieContainer != null, //CookieContainer = cookieContainer,//如果为null,赋值的时候会出现异常 AutomaticDecompression = decompressionMethods }; if (cookieContainer != null) { httpClientHandler.CookieContainer = cookieContainer; } return(httpClientHandler); }
public static string Get(string url, string referer, WebHeaderCollection header, CookieCollection cookies, Encoding encoding, DecompressionMethods decompression = DecompressionMethods.None) { return(Get(url, referer, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.89 Safari/537.36", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", 30000, header, cookies, encoding, decompression)); }
public EasyHttp AutomaticDecompression(DecompressionMethods decompressionMethods) { _tempRequest.AutomaticDecompression = decompressionMethods; return(this); }
public static HttpResponseMessage CreateResponseMessage( WinHttpRequestState state, DecompressionMethods manuallyProcessedDecompressionMethods) { HttpRequestMessage request = state.RequestMessage; SafeWinHttpHandle requestHandle = state.RequestHandle; CookieUsePolicy cookieUsePolicy = state.Handler.CookieUsePolicy; CookieContainer cookieContainer = state.Handler.CookieContainer; var response = new HttpResponseMessage(); bool stripEncodingHeaders = false; // Create a single buffer to use for all subsequent WinHttpQueryHeaders string interop calls. // This buffer is the length needed for WINHTTP_QUERY_RAW_HEADERS_CRLF, which includes the status line // and all headers separated by CRLF, so it should be large enough for any individual status line or header queries. int bufferLength = GetResponseHeaderCharBufferLength(requestHandle, Interop.WinHttp.WINHTTP_QUERY_RAW_HEADERS_CRLF); char[] buffer = ArrayPool <char> .Shared.Rent(bufferLength); try { // Get HTTP version, status code, reason phrase from the response headers. if (IsResponseHttp2(requestHandle)) { response.Version = WinHttpHandler.HttpVersion20; } else { int versionLength = GetResponseHeader(requestHandle, Interop.WinHttp.WINHTTP_QUERY_VERSION, buffer); response.Version = CharArrayHelpers.EqualsOrdinalAsciiIgnoreCase("HTTP/1.1", buffer, 0, versionLength) ? HttpVersion.Version11 : CharArrayHelpers.EqualsOrdinalAsciiIgnoreCase("HTTP/1.0", buffer, 0, versionLength) ? HttpVersion.Version10 : WinHttpHandler.HttpVersionUnknown; } response.StatusCode = (HttpStatusCode)GetResponseHeaderNumberInfo( requestHandle, Interop.WinHttp.WINHTTP_QUERY_STATUS_CODE); int reasonPhraseLength = GetResponseHeader(requestHandle, Interop.WinHttp.WINHTTP_QUERY_STATUS_TEXT, buffer); response.ReasonPhrase = reasonPhraseLength > 0 ? GetReasonPhrase(response.StatusCode, buffer, reasonPhraseLength) : string.Empty; // Create response stream and wrap it in a StreamContent object. var responseStream = new WinHttpResponseStream(requestHandle, state); state.RequestHandle = null; // ownership successfully transfered to WinHttpResponseStram. Stream decompressedStream = responseStream; if (manuallyProcessedDecompressionMethods != DecompressionMethods.None) { int contentEncodingStartIndex = 0; int contentEncodingLength = GetResponseHeader( requestHandle, Interop.WinHttp.WINHTTP_QUERY_CONTENT_ENCODING, buffer); CharArrayHelpers.Trim(buffer, ref contentEncodingStartIndex, ref contentEncodingLength); if (contentEncodingLength > 0) { if ((manuallyProcessedDecompressionMethods & DecompressionMethods.GZip) == DecompressionMethods.GZip && CharArrayHelpers.EqualsOrdinalAsciiIgnoreCase(EncodingNameGzip, buffer, contentEncodingStartIndex, contentEncodingLength)) { decompressedStream = new GZipStream(responseStream, CompressionMode.Decompress); stripEncodingHeaders = true; } else if ((manuallyProcessedDecompressionMethods & DecompressionMethods.Deflate) == DecompressionMethods.Deflate && CharArrayHelpers.EqualsOrdinalAsciiIgnoreCase(EncodingNameDeflate, buffer, contentEncodingStartIndex, contentEncodingLength)) { decompressedStream = new DeflateStream(responseStream, CompressionMode.Decompress); stripEncodingHeaders = true; } } } response.Content = new NoWriteNoSeekStreamContent(decompressedStream); response.RequestMessage = request; // Parse raw response headers and place them into response message. ParseResponseHeaders(requestHandle, response, buffer, stripEncodingHeaders); if (response.RequestMessage.Method != HttpMethod.Head) { state.ExpectedBytesToRead = response.Content.Headers.ContentLength; } return(response); } finally { ArrayPool <char> .Shared.Return(buffer); } }
public EasyHttp DefaultAutomaticDecompression(DecompressionMethods decompressionMethods) { _defaultHeaderRequest.AutomaticDecompression = decompressionMethods; return(this); }
/// <summary> /// 获取 HttpClientHandler 对象 /// </summary> /// <param name="cookieContainer"></param> /// <param name="webProxy"></param> /// <returns></returns> public static HttpClientHandler GetHttpClientHandler(CookieContainer cookieContainer = null, IWebProxy webProxy = null, DecompressionMethods decompressionMethods = DecompressionMethods.None) { return(new HttpClientHandler() { UseProxy = webProxy != null, Proxy = webProxy, UseCookies = cookieContainer != null, CookieContainer = cookieContainer, AutomaticDecompression = decompressionMethods }); }
/// <summary>初始化常用的东西</summary> /// <param name="ie">是否模拟ie</param> /// <param name="iscompress">是否压缩</param> public WebClientX(Boolean ie, Boolean iscompress) { if (ie) { Headers[HttpRequestHeader.Accept] = "image/jpeg, image/gif, */*"; Headers[HttpRequestHeader.AcceptLanguage] = "zh-CN"; //Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate"; Headers[HttpRequestHeader.UserAgent] = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)"; } if (iscompress) AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; }
internal HttpWebResponse(Uri responseUri, KnownHttpVerb verb, CoreResponseData coreData, string mediaType, bool usesProxySemantics, DecompressionMethods decompressionMethod, bool isWebSocketResponse, string connectionGroupName) { m_Uri = responseUri; m_Verb = verb; m_MediaType = mediaType; m_UsesProxySemantics = usesProxySemantics; m_CoreResponseData = coreData; m_ConnectStream = coreData.m_ConnectStream; m_HttpResponseHeaders = coreData.m_ResponseHeaders; m_ContentLength = coreData.m_ContentLength; m_StatusCode = coreData.m_StatusCode; m_StatusDescription = coreData.m_StatusDescription; m_IsVersionHttp11 = coreData.m_IsVersionHttp11; m_IsWebSocketResponse = isWebSocketResponse; m_ConnectionGroupName = connectionGroupName; //if the returned contentlength is zero, preemptively invoke calldone on the stream. //this will wake up any pending reads. if (m_ContentLength == 0 && m_ConnectStream is ConnectStream) { ((ConnectStream)m_ConnectStream).CallDone(); } // handle Content-Location header, by combining it with the orginal request. string contentLocation = m_HttpResponseHeaders[HttpKnownHeaderNames.ContentLocation]; if (contentLocation != null) { try { m_Uri = new Uri(m_Uri, contentLocation); } catch (UriFormatException e) { GlobalLog.Assert("Exception on response Uri parsing.", e.ToString()); } } // decompress responses by hooking up a final response Stream - only if user required it if (decompressionMethod != DecompressionMethods.None) { string contentEncoding = m_HttpResponseHeaders[HttpKnownHeaderNames.ContentEncoding]; if (contentEncoding != null) { if (((decompressionMethod & DecompressionMethods.GZip) != 0) && contentEncoding.IndexOf(HttpWebRequest.GZipHeader, StringComparison.CurrentCulture) != -1) { m_ConnectStream = new GZipWrapperStream(m_ConnectStream, CompressionMode.Decompress); m_ContentLength = -1; // unknown on compressed streams // Setting a response header after parsing will ruin the Common Header optimization. // This seems like a corner case. ContentEncoding could be added as a common header, with a special // property allowing it to be nulled. m_HttpResponseHeaders[HttpKnownHeaderNames.ContentEncoding] = null; } else if (((decompressionMethod & DecompressionMethods.Deflate) != 0) && contentEncoding.IndexOf(HttpWebRequest.DeflateHeader, StringComparison.CurrentCulture) != -1) { m_ConnectStream = new DeflateWrapperStream(m_ConnectStream, CompressionMode.Decompress); m_ContentLength = -1; // unknown on compressed streams // Setting a response header after parsing will ruin the Common Header optimization. // This seems like a corner case. ContentEncoding could be added as a common header, with a special // property allowing it to be nulled. m_HttpResponseHeaders[HttpKnownHeaderNames.ContentEncoding] = null; } } } }
// TODO AuthenticationLevel public static Builder AutomaticDecompression(DecompressionMethods value) => req => req.AutomaticDecompression = value;
/// <summary> /// 根据Url地址Get请求返回数据 /// </summary> /// <param name="url">请求地址</param> /// <param name="parameters">请求参数</param> /// <param name="decompressionMethods">解压缩方式,默认:GZip</param> /// <param name="mediaType">互联网媒体类型</param> /// <param name="headers">头部信息</param> /// <returns>返回请求结果和状态结果</returns> public static async Task <(string result, HttpStatusCode code)> GetAsync(string url, Dictionary <string, string> parameters, DecompressionMethods decompressionMethods = DecompressionMethods.GZip, string mediaType = "application/json", Dictionary <string, string> headers = null) { if (url?.StartsWith("https", StringComparison.OrdinalIgnoreCase) == true) { ServicePointManager.ServerCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true; } using (var httpClient = new HttpClient(new HttpClientHandler { AutomaticDecompression = decompressionMethods })) { httpClient.CancelPendingRequests(); httpClient.DefaultRequestHeaders.Clear(); httpClient.DefaultRequestHeaders.Add("user-agent", UserAgent); if (!string.IsNullOrEmpty(mediaType)) { httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaType)); } if (headers?.Count > 0) { foreach (var item in headers) { if (httpClient.DefaultRequestHeaders.Contains(item.Key)) { httpClient.DefaultRequestHeaders.Remove(item.Key); } httpClient.DefaultRequestHeaders.Add(item.Key, item.Value); } } using (var response = await httpClient.GetAsync(url + parameters.ToUrl("?", false, false))) { var httpStatusCode = response.StatusCode; if (response.IsSuccessStatusCode) { return(await response.Content.ReadAsStringAsync(), httpStatusCode); } return(null, httpStatusCode); } } }
public ISocketsHttpHandlerBuilder SetAutomaticDecompression(DecompressionMethods value) { _socketsHttpHandler.AutomaticDecompression = value; return(this); }
static void \u206B([In] HttpClientHandler obj0, [In] DecompressionMethods obj1) { // ISSUE: unable to decompile the method. }
public RestRequestBuilder WithAutomaticDecompression(DecompressionMethods automaticDecompression) { _request.AutomaticDecompression = automaticDecompression; return this; }
public HttpClientHandler() { this.rtFilter = new RTHttpBaseProtocolFilter(); this.handlerToFilter = new HttpHandlerToFilter(this.rtFilter); this.clientCertificateOptions = ClientCertificateOption.Manual; InitRTCookieUsageBehavior(); this.useCookies = true; // deal with cookies by default. this.cookieContainer = new CookieContainer(); // default container used for dealing with auto-cookies. // Managed at this layer for granularity, but uses the desktop default. this.rtFilter.AutomaticDecompression = false; this.automaticDecompression = DecompressionMethods.None; // Set initial proxy credentials based on default system proxy. SetProxyCredential(null); // We don't support using the UI model in HttpBaseProtocolFilter() especially for auto-handling 401 responses. this.rtFilter.AllowUI = false; // The .NET Desktop System.Net Http APIs (based on HttpWebRequest/HttpClient) uses no caching by default. // To preserve app-compat, we turn off caching (as much as possible) in the WinRT HttpClient APIs. // TODO (#7877): use RTHttpCacheReadBehavior.NoCache when available in the next version of WinRT HttpClient API. this.rtFilter.CacheControl.ReadBehavior = RTHttpCacheReadBehavior.MostRecent; this.rtFilter.CacheControl.WriteBehavior = RTHttpCacheWriteBehavior.NoCache; }
public static string Post(string url, Dictionary <string, string> parameters, string referer, string userAgent, string accept, int timeout, WebHeaderCollection header, CookieCollection cookies, Encoding encoding, DecompressionMethods decompression = DecompressionMethods.None) { string result = string.Empty; try { HttpWebRequest httpWebRequest = null; if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult; httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(url)); httpWebRequest.ProtocolVersion = HttpVersion.Version10; } else { httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(url)); } httpWebRequest.UserAgent = userAgent; httpWebRequest.Referer = referer; httpWebRequest.Method = "POST"; httpWebRequest.Timeout = timeout; httpWebRequest.Accept = accept; if (cookies != null) { httpWebRequest.CookieContainer = new CookieContainer(); httpWebRequest.CookieContainer.Add(cookies); } if (header != null) { httpWebRequest.Headers = header; } httpWebRequest.AutomaticDecompression = decompression; httpWebRequest.ContentType = "application/x-www-form-urlencoded"; if (parameters != null && parameters.Count > 0) { StringBuilder stringBuilder = new StringBuilder(); int num = 0; foreach (string key in parameters.Keys) { stringBuilder.AppendFormat("{0}={1}", key, parameters[key]); if (num != parameters.Keys.Count - 1) { stringBuilder.Append("&"); } num++; } httpWebRequest.ContentLength = stringBuilder.ToString().Length; byte[] bytes = Encoding.ASCII.GetBytes(stringBuilder.ToString()); using (Stream stream = httpWebRequest.GetRequestStream()) { stream.Write(bytes, 0, bytes.Length); } } HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse(); result = GetResponseString(webResponse, encoding); return(result); } catch { return(result); } }
private void Randomize() { string[] useragents = { "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13", "Mozilla/5.0 (Macintosh; U; PPC Max OS X Mach-O; en-US; rv:1.8.0.7) Gecko/200609211 Camino/1.0.3" }; string[] acceptlang = { "en-us;q=0.5,en;q=0.3", "ru-ru,ru; q=0.3", "q=0.8,en-us; q=0.3", "q=0.5,en" }; string[] accepts = { "application/xml" }; DecompressionMethods[] dmethods = { DecompressionMethods.Deflate, DecompressionMethods.GZip, DecompressionMethods.None, (DecompressionMethods.Deflate | DecompressionMethods.GZip), (DecompressionMethods.Deflate | DecompressionMethods.None), (DecompressionMethods.GZip | DecompressionMethods.None) }; AcceptLang = acceptlang[new Random().Next(acceptlang.Length)]; UserAgent = useragents[new Random().Next(useragents.Length)]; Accept = accepts[new Random().Next(accepts.Length)]; DMethod = dmethods[new Random().Next(dmethods.Length)]; }
public static string Post(string url, Dictionary <string, string> parameters, string referer, WebHeaderCollection header, CookieCollection cookies, Encoding encoding, DecompressionMethods decompression = DecompressionMethods.None) { return(Post(url, parameters, referer, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 30000, header, cookies, encoding, decompression)); }
public static string Get(string url, DecompressionMethods decompression = DecompressionMethods.None) { return(Get(url, "", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.89 Safari/537.36", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", 30000, null, null, Encoding.UTF8, decompression)); }
static public async Task Post(string url, Dictionary <string, string> headers, string body, DecompressionMethods decompress = DecompressionMethods.GZip, Action <string> callback = null) { try { var header = GetObj(headers); var handler = new HttpClientHandler() { UseCookies = false, AutomaticDecompression = decompress }; HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, url) { Content = new StringContent(body, Encoding.UTF8, header.ContentType) }; SetHead(req.Headers, headers); HttpClient client = new HttpClient(handler); client.DefaultRequestHeaders.Add("User-Agent", header.UserAgent); client.DefaultRequestHeaders.Add("Referer", header.Referer); client.DefaultRequestHeaders.Add("Origin", header.Origin); client.DefaultRequestHeaders.Add("Accept-Language", header.AcceptLanguage); client.DefaultRequestHeaders.Connection.Add(header.Connection); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(header.Accept)); var res = await client.SendAsync(req); var str = await res.Content.ReadAsStringAsync(); if (callback != null) { callback(str); } } catch (Exception ex) { if (callback != null) { callback(ex.Message); } } }