更新时间:2023-02-15 11:02:31
问题是你解释的二进制数据作为文本,即使它不是 - 只要你开始把内容作为字符串而不是字节,你就有麻烦了。你没有给你的包装类的细节,但我假设你的内容
属性返回一个字符串 - 你将不能够使用。如果你的包装类不会让你得到来自网络响应的原始数据,你需要对其进行修改。
The problem is you're interpreting the binary data as text, even if it isn't - as soon as you start treating the content as a string instead of bytes, you're in trouble. You haven't given the details of your wrapper class, but I'm assuming your Content
property is returning a string - you won't be able to use that. If your wrapper class doesn't let you get at the raw data from the web response, you'll need to modify it.
如果您正在使用.NET 4 ,你可以使用新的CopyTo方法:
If you're using .NET 4, you can use the new CopyTo method:
using (Stream output = File.OpenWrite("file.dat"))
using (Stream input = http.Response.GetResponseStream())
{
input.CopyTo(output);
}
如果你不使用.NET 4中,你所要做的复制手动:
If you're not using .NET 4, you have to do the copying manually:
using (Stream output = File.OpenWrite("file.dat"))
using (Stream input = http.Response.GetResponseStream())
{
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}