2017-10-09 11 views
2

リクエストを送信したときと、要求を処理中であることをasp.netコアログに記録すると、私はかなり大きなリクエスト(〜1Mb)を送信しています。私はgzipを使ってaspにリクエストを圧縮することでこの時間を短縮できると思う。HttpClientを使用してasp.netコア2サイトへのリクエストを圧縮する最良の方法は何ですか?

以下は、私が圧縮せずにリクエストを行っているかなり単純な方法です。クライアント側でGzipリクエスト圧縮を実装する適切な方法は何ですか?そして、クライアント側で実装したら、サーバー側で何をする必要がありますか?

using (HttpResponseMessage response = client.PostAsync("Controller/Action", httpContent).Result) 
{ 
    if (response.StatusCode != System.Net.HttpStatusCode.OK) 
    { 

     throw new Exception(string.Format("Invalid responsecode for http request response {0}: {1}", response.StatusCode, response.ReasonPhrase)); 
    } 
} 

答えて

3

だから私はあまりにも多くの仕事を、それは、サーバー側で簡単なミドルウェアで動作するようになったとありませんクライアント側。私はWebAPIContrib githubプロジェクトのCompressedContent.csをRexのコメントで提案し、以下のようにリクエストしました。 OKでない場合はスロー例外が発生します.Plylyを使用してリクエストをリトライして待機ポリシーを適用しているためです。

クライアント側:

using (var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json")) 
using (var compressedContent = new CompressedContent(httpContent, "gzip")) 
using (HttpResponseMessage response = client.PostAsync("Controller/Action", compressedContent).Result) 
{ 
    if (response.StatusCode != System.Net.HttpStatusCode.OK) 
    { 
     throw new Exception(string.Format("Invalid responsecode for http request response {0}: {1}", response.StatusCode, response.ReasonPhrase)); 
    } 
} 

は、その後、私はGzipでストリームとリクエストボディストリームを包んミドルウェアの簡単な作品を作成し、サーバー側では、私はあなたがアプリの前にミドルウェアを配置する必要がありますかなり確信しています。 configure関数でUseMvc()を使用します(私はまだASPcore 1スタートアップスタイルを使用します)。

public class GzipRequestMiddleware 
{ 
    private readonly RequestDelegate next; 
    private const string ContentEncodingHeader = "Content-Encoding"; 
    private const string ContentEncodingGzip = "gzip"; 
    private const string ContentEncodingDeflate = "deflate"; 

    public GzipRequestMiddleware(RequestDelegate next) 
    { 
     this.next = next ?? throw new ArgumentNullException(nameof(next)); 
    } 


    public async Task Invoke(HttpContext context) 
    { 
     if (context.Request.Headers.Keys.Contains(ContentEncodingHeader) && (context.Request.Headers[ContentEncodingHeader] == ContentEncodingGzip || context.Request.Headers[ContentEncodingHeader] == ContentEncodingDeflate)) 
     { 
      var contentEncoding = context.Request.Headers[ContentEncodingHeader]; 
      var decompressor = contentEncoding == ContentEncodingGzip ? (Stream)new GZipStream(context.Request.Body, CompressionMode.Decompress, true) : (Stream)new DeflateStream(context.Request.Body, CompressionMode.Decompress, true); 
      context.Request.Body = decompressor; 
     } 
     await next(context); 
    } 
} 
1

var handler = new HttpClientHandler(); 
if (handler.SupportsAutomaticDecompression) 
{ 
    handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; 
} 

var client = new HttpClient(handler); 

MSDNのリファレンス以下のようにあなたが圧縮を有効にする必要があります。Using automatic decompression with HttpClient

+1

MSDNの参照リンクは –

+0

https://blogs.msdn.microsoft.com/dotnet/2013/06/06/portable-compression-and-httpclient-working-together/ – Rex

+0

たぶん私は完璧になります何かが欠けていますが、それは応答の圧縮解除のみを処理し、圧縮は要求しないようです。 – Theyouthis

関連する問題