为什么HttpWebResponse会丢失数据?

时间:2016-10-25 12:20:50

标签: c# stream httpwebresponse

在另一个问题中,当通过GetResponseStream()从HttpWebResponse读取时,人们得到的数据不完整。

从嵌入式设备读取数据时我也遇到了这个问题,该设备应该向我发送1000个输入的配置,所有32个字节的头部和64个字节* 1000都会产生64032个字节的数据。

直接读取响应流只能为我提供前61个半输入的数据,从那里只有零。

版本a)无效:

int headerSize = 32;
int inputSize = 64;
byte[] buffer = new byte[(inputSize*1000) + headerSize];

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

using (Stream stream = response.GetResponseStream())
{
    if (stream != null)
    {
        stream.Seek(0, SeekOrigin.Begin);
        stream.Read(buffer, 0, buffer.Length);
    }
}

response.Close();
return buffer;

为了使问题可视化,我分别为每个输入配置打印了64个字节。它基本上由40个ascii字符和几个表示布尔值和整数值的字节组成。

版本A)输出:

1/1000 | 46656E7374657220576F686E656E2020202020202020202020202020202020202020202020202020000000000F0EB0AA00008100000001800000100090010020
2/1000 | 42574D20576F686E656E202020202020202020202020202020202020202020202020202020202020000000000F0EB0AA00008100000001800000100091010080
…
61/1000 | 53656E736F72203631202020202020202020202020202020202020202020202020202020202020200000000000000000000010003300000000001000C3010000
62/1000 | 53656E736F7220363220202020202020202020202020202020202020202020200000000000000000000000000000000000000000000000000000000000000000
63/1000 | 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
…
999/1000 | 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1000/1000 | 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

当我将ResponseStream复制到新的MemoryStream时,我可以完全读取所有1000个输入而没有任何损坏的字节。

版本B)完美运作:

(另见https://stackoverflow.com/a/22354617/6290907解决了我在第一种情况下的问题)

int headerSize = 32;
int inputSize = 64;
byte[] buffer = new byte[(inputSize*1000) + headerSize];

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

using (Stream stream = response.GetResponseStream())
{
    if (stream != null)
    {
        MemoryStream memStream = new MemoryStream();
        stream.CopyTo(memStream);
        memStream.Flush();
        stream.Close();

        memStream.Seek(0, SeekOrigin.Begin);
        memStream.Read(buffer, 0, buffer.Length);

        memStream.Close();
    }
}

response.Close();
return buffer;

版本B)输出

1/1000 | 46656E7374657220576F686E656E2020202020202020202020202020202020202020202020202020000000000F0EB0AA00008100000001800000100090010020
2/1000 | 42574D20576F686E656E202020202020202020202020202020202020202020202020202020202020000000000F0EB0AA00008100000001800000100091010080
…
61/1000 | 53656E736F72203631202020202020202020202020202020202020202020202020202020202020200000000000000000000010003300000000001000C3010000
62/1000 | 53656E736F72203632202020202020202020202020202020202020202020202020202020202020200000000000000000000010003300000000001000C3010000
63/1000 | 53656E736F72203633202020202020202020202020202020202020202020202020202020202020200000000000000000000010003300000000001000C3010000
…
999/1000 | 53656E736F7220393939202020202020202020202020202020202020202020202020202020202020000000000000000000001000DA030000000010006A050000
1000/1000 | 53656E736F7220313030302020202020202020202020202020202020202020202020202020202020000000000000000000001000DB030000000010006B050000

从技术角度来看:为什么HttpWebResponse在直接访问时会丢失数据? 我不想让它工作,但我想理解为什么版本a失败而版本b成功,而两者都依赖于相同的数据源(response.GetResponseStream())。 在这种情况下,幕后发生了什么?

感谢您的努力!

1 个答案:

答案 0 :(得分:2)

检查int返回的Stream.Read,如docs所述:

  

如果这么多字节,这可能小于请求的字节数   当前不可用,如果流的末尾有,则为零(0)   已经到达。

我愿意打赌在第一次通话中只返回部分流。

如果你反复调用Stream.Read,你最终会获得所有字节。 http流的加载速度比代码运行速度慢 - 在致电Read之前没有时间完成。

通过将CopyToMemoryStream一起使用,调用将阻塞,直到读取整个流。在StreamReader中进行换行,然后调用ReadToEnd将获得相同的成功结果。