[英]Force To Read HttpWebResponse.GetResponseStream() When Content-Length Is Zero
As title said. 正如标题所说。
Sometime an HTTP response from remote server includes Content-Length header with zero value, but it still return HTTP body . 有时,来自远程服务器的HTTP响应包含具有零值的Content-Length标头,但仍返回HTTP body 。 The body can still be obtained with jQuery in browser, but using HttpWebResponse.GetResponseStream() can't read anything. 仍然可以在浏览器中使用jQuery获得该正文,但是使用HttpWebResponse.GetResponseStream()无法读取任何内容。 How do I force to read it? 我该如何强迫阅读? Thanks everyone. 感谢大家。
Here's an example of HTTP response which I met. 这是我遇到的HTTP响应示例。
HTTP/1.1 200 OK HTTP / 1.1 200 OK
Connection: keep-alive 连接:保持活动状态
Cache-Control: no-cache,no-store 缓存控制:无缓存,无存储
Content-Type: text/plain 内容类型:文本/纯文本
Content-Length: 0 内容长度:0
Date: Mon, 30 Sep 2013 09:34:21 GMT 日期:2013年9月30日星期一09:34:21 GMT
(HTTP Body) (HTTP正文)
Looking through the source code I could not find a way to force this behavior. 查看源代码,我找不到强制这种行为的方法。 Probably there's nothing you can do. 可能您无能为力。
Try disabling HTTP pipelining. 尝试禁用HTTP流水线。 Maybe HttpWebRequest does not rely on the content length in that case. 在这种情况下,也许HttpWebRequest不依赖于内容长度。 It is unlikely that it will help. 它不太可能会有所帮助。
Either use a different HTTP library, or send the request using TcpClient
. 使用其他HTTP库,或使用TcpClient
发送请求。 You can use Fiddler to copy a template HTTP request and use that as the basis for yours. 您可以使用Fiddler复制模板HTTP请求,并将其用作您的基础。 Should not be a lot of work. 不应该做很多工作。
You could also create a rewriting proxy that removes the header. 您还可以创建一个重写代理来删除标头。 That's probably a high-effort solution. 那可能是一个努力的解决方案。
First question - Where did you get the HTTP response? 第一个问题-您从哪里获得HTTP响应? Did you capture it with Fiddler or are you typing in what you think you're seeing by examining the HttpWebResponse in a debugger? 您是使用Fiddler捕获的,还是通过在调试器中检查HttpWebResponse来输入您认为的内容? I would suggest running this with Fiddler and examining the complete response headers if you didn't already. 我建议使用Fiddler来运行它,并检查完整的响应头(如果尚未安装的话)。
We need to establish first if the Content-Length is being returned with a value of zero or if some of the other code in your project is causing you to interpret it as zero or to set your copy of the content length to zero. 我们首先需要确定返回的Content-Length值为零,还是项目中的某些其他代码使您将其解释为零,或者将内容长度的副本设置为零。
I think one of two things is happening here: 我认为这里发生了两件事之一:
Harold 哈罗德
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.