HttpWebRequest的contentlength无效 [英] HttpWebRequest invalid contentlength
问题描述
我需要获取内容长度,以便告诉我的应用程序缓冲区的结束位置.问题是即使响应中出现了Content-Length标头,httpwebresponse.ContentLength仍返回-1.
I need to get content length in order to tell my app where the buffer ends. The problem is that httpwebresponse.ContentLength returns -1 even though Content-Length header is presented in response.
然后,尽管我要阅读实际的标头以找出长度.我正在测试的页面返回的Content-Length是1646.一个HTTP嗅探器声称我收到了1900个字节,因此我认为区别在于标头长度.然后我从响应中复制了整个身体,并将其粘贴到在线strlen网站中,身体大小实际上是1850!
Then I though I'm going to read the actual header to find out the length. The Content-Length returned by the page I'm testing on is 1646. An HTTP sniffer claims that I received 1900 bytes, so I assume the difference are the header length. Then I copied the whole body from response and pasted it into online strlen site and the body size is actually 1850!!
这怎么可能?为什么响应返回无效的content-length,为什么httpwebrequest.ContentLength返回-1?在收到响应本身之前,如何计算实际的响应长度?
How is this possible? Why does response return invalid content-length and why does httpwebrequest.ContentLength returns -1? How can I calculate the actual response length before receiving the response itself?
这是我用来获取响应的代码:
This is the code I'm using to get the response:
using (System.IO.Stream responseStream = hwresponse.GetResponseStream())
{
using (MemoryStream memoryStream = new MemoryStream())
{
int count = 0;
do
{
count = responseStream.Read(buffer, 0, buffer.Length);
TCP_R.SendBytes(buffer);
} while (count != 0);
}
}
byte[] PACKET_END_IDENTIFIER = { 0x8, 0x01, 0x8, 0x1, 0x8 };
TCP_R.SendBytes(PACKET_END_IDENTIFIER);
TCP_R.Close();
我有一个接受请求的代理服务器应用程序,将其发送到另一个应用程序(我的客户端),客户端执行该请求,并使用TCP_R类返回结果.服务器从客户端获得响应后,会将响应返回给浏览器.
I have a proxy server application that takes a request, sends it to another application (my client) client executes the request and using TCP_R class returns the result. When server gets response from client, it returns response back to browser.
每次执行请求时,我都会获得所有数据+多余的垃圾,这是一个示例:
Each time I do a request, I get all the data + extra garbage, here's an example:
<tag1><tag2><tag3> ag3>
ag3>是垃圾数据,就像缓冲区的结尾被切断并再次添加一样.评估客户端以有效响应进行响应,将垃圾数据添加到onDataRecieve事件中..有什么提示吗?谢谢!
ag3> is the garbage data, it's like the ending of buffer is cut off and added again. It apprears that the client responds with a valid response, the garbage data is added onDataRecieve event.. any tips? thanks!
推荐答案
-1不是ContentLength
属性的无效值.我假设您的意思是 response 的ContentLength
属性是-1 ...问 request 长度是什么意思.即使这样,它也是完全有效的:
-1 isn't an invalid value of the ContentLength
property. I assume you mean the ContentLength
property of the response is -1... asking the request what the length is would be non-sensical. Even so, it's perfectly valid:
ContentLength属性包含随响应返回的Content-Length标头的值.如果未在响应中设置Content-Length标头,则ContentLength设置为值-1.
The ContentLength property contains the value of the Content-Length header returned with the response. If the Content-Length header is not set in the response, ContentLength is set to the value -1.
如果主体长度为1850,则表明它正在使用 分块传输编码 .但这对您应该是透明的-只需不断从响应流中读取内容直到结束为止.如果您使用的是.NET 4,那就太容易了-只需创建一个MemoryStream
并使用Stream.CopyTo
即可将数据复制到该MemoryStream
.
If the body length is 1850, that suggests it's using chunked transfer encoding. But that should be transparent to you - just keep reading from the response stream until the end. If you're using .NET 4, it's dead easy - just create a MemoryStream
and use Stream.CopyTo
to copy the data to that MemoryStream
.
这篇关于HttpWebRequest的contentlength无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!