gpt4 book ai didi

c# - HttpWebResponse 剥离换行符

转载 作者:太空宇宙 更新时间:2023-11-03 16:34:48 26 4
gpt4 key购买 nike

我正在使用以下代码来读取响应:

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%)。任何提示为什么会这样?谢谢!

最佳答案

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

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

关于c# - HttpWebResponse 剥离换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9474000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com