简体   繁体   English

当内容长度为零时,强制读取HttpWebResponse.GetResponseStream()

[英]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: 我认为这里发生了两件事之一:

  1. The response is not exactly as your indicated above and examining it with Fiddler will establish that quickly. 响应与上面的指示不完全相同,请与Fiddler进行检查,可以很快确定。 Most likely I would suspect that Content-Length is actually missing and Transfer-Encoding is present and set to chunked; 我很可能怀疑Content-Length实际上丢失了,并且存在Transfer-Encoding并将其设置为分块。 in that case the HttpWebResponse.ContentLength property will return -1 instead of 0 (zero). 在这种情况下,HttpWebResponse.ContentLength属性将返回-1而不是0(零)。
  2. The response is exactly as you indicated (particularly in that Content-Length is present and specified as zero), in which case your web server is just busted. 响应与您所指示的完全相同(特别是存在Content-Length并将其指定为零),在这种情况下,您的Web服务器就被破坏了。 I shouldn't be returning a body with Content-Length set to zero; 我不应该返回Content-Length设置为零的主体; it can return no Content-Length if it was HTTP 1.0, but it clearly says HTTP 1.1 above, so it should be returning Transfer-Encoding: chunked and no Content-Length or a non-zero Content-Length if not chunking and returning a body. 如果使用的是HTTP 1.0,则不会返回Content-Length,但是上面明确指出是HTTP 1.1,因此它应该返回Transfer-Encoding:分块,不返回Content-Length,如果不进行分块则不返回Content-Length,如果返回非零,则返回0身体。

Harold 哈罗德

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HttpWebResponse.GetResponseStream():什么时候传输响应体? - HttpWebResponse.GetResponseStream(): when is the response body transmitted? HttpWebResponse.GetResponseStream()(似乎是)失败,没有异常 - HttpWebResponse.GetResponseStream() (seems to be) failing with no exception 无法将HttpWebResponse.GetResponseStream()分配给Stream - Unable to assign HttpWebResponse.GetResponseStream() to Stream 使用HttpWebResponse :: GetResponseStream方法时接收不完整的数据 - Receiving incomplete data when using HttpWebResponse::GetResponseStream method WebResponse Content-Length返回-1 - WebResponse Content-Length Returns -1 为什么 Stream.CanRead 在 HttpWebResponse object 上第二次调用 GetResponseStream() 时返回 false - Why does Stream.CanRead return false when calling GetResponseStream() second time on HttpWebResponse object 从具有不同类型的HttpWebResponse GetResponseStream反序列化XML - Deserializing XML from HttpWebResponse GetResponseStream with different types 寻找一种方法重写HttpWebResponse的GetResponseStream方法 - Looking for a way to override the GetResponseStream Method of HttpWebResponse 如何设置HttpWebResponse的内容长度限制 - How to Set Limit on Content Length for HttpWebResponse httpHandler Content-Length是否太短? - httpHandler Content-Length too short?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM