/// <summary> /// HttpClient初始化 /// </summary> /// <param name="timeout"></param> private HttpClient InitHttpClient(int?timeout) { HttpClient client; if (_decompressionMethods != DecompressionMethods.None) { _handler.AutomaticDecompression = _decompressionMethods; //client = new HttpClient(new HttpClientHandler() { AutomaticDecompression = _decompressionMethods }); } client = new HttpClient(_handler); if (_decompressionMethods != DecompressionMethods.None) { client.DefaultRequestHeaders.Add("Accept-encoding", _decompressionMethods.ToString().ToLower()); } if (timeout.HasValue) { client.Timeout = TimeSpan.FromMilliseconds(timeout.Value); } else { client.Timeout = TimeSpan.FromMilliseconds(this._timeout); } if (!string.IsNullOrWhiteSpace(_baseUrl)) { client.BaseAddress = new Uri(_baseUrl); } client.DefaultRequestHeaders.Accept.Add(_mediaTypeWithQualityHeaderValue); client.DefaultRequestHeaders.Connection.Add("keep-alive"); return(client); }
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 { compressedBytes = CompressBytes(responseBodyBytes, compressionKind == DecompressionMethods.GZip); } 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 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 async Task SendAsync_NoWinHttpDecompressionSupport_AutoDecompressionSettingDiffers_ResponseIsNotDecompressed(bool responseIsGZip) { DecompressionMethods decompressionMethods = responseIsGZip ? DecompressionMethods.Deflate : DecompressionMethods.GZip; _output.WriteLine("DecompressionMethods = {0}", decompressionMethods.ToString()); TestControl.WinHttpDecompressionSupport = false; var handler = new WinHttpHandler(); using (HttpResponseMessage response = SendRequestHelper.Send( handler, delegate { handler.AutomaticDecompression = decompressionMethods; TestServer.SetResponse(responseIsGZip ? DecompressionMethods.GZip : DecompressionMethods.Deflate, TestServer.ExpectedResponseBody); })) { await VerifyResponseContent( TestServer.CompressBytes(TestServer.ExpectedResponseBodyBytes, useGZip: responseIsGZip), response.Content, responseContentWasOriginallyCompressed : true, responseContentWasAutoDecompressed : false); } }
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); }