Skip to main content
 首页 » 编程设计

c#之为什么 HttpWebResponse.ContentLength 突然返回 -1

2024年02月13日39cloudgamer

我们这里有一个特殊的问题。 我们在客户端有一个代码,可以使用 HTTPWebResponse/HttpWebRequest 从 Web 服务器下载一些文件 我们正在使用 HttpWebResponse.ContentLength 来获取文件的大小... 所有这些天它都工作正常但突然开始给 -1 然而,当我们绕过服务器上的负载平衡器时,它就可以工作了…… 任何人都可以就为什么会发生这种情况给出一个公平的想法

请您参考如下方法:

来自 MSDN 关于 HttpWebResponse.ContentLength property 的文档:

If the Content-Length header is not set in the response, ContentLength is set to the value -1.

简单地说——服务器没有发送 Content-Length header 。