13

我需要在 http post 请求中将大量数据发送到支持 gzip 编码请求的服务器。

从一个简单的开始

public async Task<string> DoPost(HttpContent content)
{
  HttpClient client = new HttpClient();
  HttpResponseMessage response = await client.PostAsync("http://myUri", content);

  response.EnsureSuccessStatusCode();
  return await response.Content.ReadAsStringAsync();
}

我刚刚添加了预压缩

public async Task<string> DoPost(HttpContent content, bool compress)
{
  if (compress) 
    content= await CompressAsync(content);

  return await DoPost(content);
}

private static async Task<StreamContent> CompressAsync(HttpContent content)
{
  MemoryStream ms = new MemoryStream();
  using (GZipStream gzipStream = new GZipStream(ms, CompressionMode.Compress, true))
  {
    await content.CopyToAsync(gzipStream);
    await gzipStream.FlushAsync();
  }

  ms.Position = 0;
  StreamContent compressedStreamContent = new StreamContent(ms);
  compressedStreamContent.Headers.ContentType = content.Headers.ContentType;
  compressedStreamContent.Headers.Add("Content-Encoding", "gzip");

  return compressedStreamContent;
}

它工作得很好,但压缩数据在发送请求之前完全加载到内存中。我希望能够在以流方式发送期间动态压缩数据。

为此,我尝试了以下代码:

private static async Task<HttpContent> CompressAsync2(HttpContent content)
{
  PushStreamContent pushStreamContent = new PushStreamContent(async (stream, content2, transport) =>
  {
    using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Compress, true))
    {
      try
      {
        await content.CopyToAsync(gzipStream);
        await gzipStream.FlushAsync();
      }
      catch (Exception exception)
      {
        throw;
      }
    }
  });
  pushStreamContent.Headers.ContentType = content.Headers.ContentType;
  pushStreamContent.Headers.Add("Content-Encoding", "gzip");

  return pushStreamContent;
}

但它永远不会超出 CopyToAsync(gzipStream)。FlushAsync 永远不会执行,也不会引发异常,Fiddler 也看不到任何帖子开始。

我的问题是:

  • 为什么 CompressAsync2 不起作用?
  • 如何在发送期间即时压缩而不在内存中加载压缩缓冲区?

任何帮助将不胜感激。

4

1 回答 1

17

尝试使用 WebAPIContrib 中的 CompressedContent 类https://github.com/WebApiContrib/WebAPIContrib/blob/master/src/WebApiContrib/Content/CompressedContent.cs

public async Task<string> DoPost(HttpContent content)
{
  HttpClient client = new HttpClient();
  HttpResponseMessage response = await client.PostAsync("http://myUri", 
                                 new CompressedContent(content,"gzip"));

  response.EnsureSuccessStatusCode();
  return await response.Content.ReadAsStringAsync();
}

PS这只会流式传输.net 4.5上的内容。.net 4 版本的 HttpWebRequest 总是缓冲发送的内容。

PPS 为每个请求创建一个新的 HttpClient 并不是使用 HttpClient 的最佳方式。这样做将强制为每个请求创建一个新的 TCP 连接。

于 2013-05-21T16:25:10.747 回答