HttpWebResponse剥离换行符

时间:2012-02-27 23:24:41

标签: c# httpwebresponse strip

我正在使用以下代码来阅读回复:

using (Stream MyResponseStream = hwresponse.GetResponseStream())
{
        byte[] MyBuffer = new byte[4096];
        int BytesRead;

        while (0 < (BytesRead = MyResponseStream.Read(MyBuffer, 0, MyBuffer.Length)))
        {  
            ByteArrayToFile("request.txt", MyBuffer, BytesRead);
        }
}

这是写入文件的功能:

public void ByteArrayToFile(string _FileName, byte[] _ByteArray, int BytesRead)
{
        System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Append, System.IO.FileAccess.Write);
        _FileStream.Write(_ByteArray, 0, BytesRead);
        _FileStream.Close();
}

如果我使用webclient,我会获得新行,所有内容都被正确解析。当我使用HttpWebResponse时,新行字符被剥离(不是全部,而是80%)。任何暗示为什么会发生这种情况?谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码将整个响应写入文件:

using (StreamReader MyResponseStream = new StreamReader(hwresponse.GetResponseStream()))
{
    using (StreamWriter _FileStream = new StreamWriter("request.txt", true))
    {
        _FileStream.Write(MyResponseStream.ReadToEnd());
    }
}