超出了HttpClient缓冲区大小限制

Mil*_*nec 16 .net rest dotnet-httpclient

我正在使用我的客户端获取有关存储在我的Swift对象存储中的某个文件的一些信息,这些文件可以通过REST Api访问.在Swift中,HEAD方法和指向对象的url返回HTML响应(包含NO CONTENT)标头中包含的元数据(哈希,时间戳等).

当文件大小<2GB时,我的代码可以正常工作.我得到了HttpResponseMessage,我能够解析它所需的数据,但是当我要求文件> 2GB时,我得到异常:"无法向缓冲区写入比配置的最大缓冲区大小更多的字节:2147483647".

我明白,HttpClient属性MaxResponseContentBufferSize不能设置为> 2GB的值,但我不想得到它的内容.这是一些错误还是有更好的方法来解决这个问题?

public HttpResponseMessage FileCheckResponse(string objectName)
   {
        //create url which will be appended to HttpClient (m_client)
        string requestUrl = RequestUrlBuilder(m_containerName, objectName);
        //create request message with Head method
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Head, requestUrl);
        //exception thrown here... 
        HttpResponseMessage response = m_client.SendAsync(request).Result;            

        return response;
    }
Run Code Online (Sandbox Code Playgroud)

尝试使用Dev HTTP Client(Chrome扩展程序)执行相同操作时,我没有问题.似乎Content-Length标题使它变得不可行.以下是Dev HTTP Client的输出:

Content-Length: 3900762112
Accept-Ranges: bytes
Last-Modified: Fri, 06 Sep 2013 16:24:30 GMT
Etag: da4392bdb5c90edf31c14d008570fb95
X-Timestamp: 1378484670.87557
Content-Type: application/octet-stream
Date: Tue, 10 Sep 2013 13:25:27 GMT
Connection: keep-alive
Run Code Online (Sandbox Code Playgroud)

任何想法我都会很高兴!谢谢

首先 - 感谢达雷尔Mirrel,谁在几秒钟内解决了我整整一天的问题:)我只需要通过增加HttpCompletitionOption其中获得响应编辑一行代码:

HttpResponseMessage response = m_client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).Result;
Run Code Online (Sandbox Code Playgroud)

选项ResponseHeaderRead告诉客户尽快做完手术的头被读取withnout读消息的内容.

Dar*_*ler 28

使用SendAsync重载,允许您指定HttpCompletionOptions.有了这个,您可以告诉HttpClient不要为响应内容创建缓冲区.

  • 哦,我的$ {god}神= Darrel Miller。非常感谢!!我不敢相信这会如此简单。我整天都在寻找解决方案:) (2认同)
  • 这对我有用:`await _client.GetAsync(uri,HttpCompletionOption.ResponseHeadersRead);` (2认同)