如何从 URL 获取数据并将其保存到 C#.NET 中的二进制文件中,而不会出现编码混乱?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/992498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:06:02  来源:igfitidea点击:

How to GET data from an URL and save it into a file in binary in C#.NET without the encoding mess?

c#.nethttpencodingbinary

提问by jms

In C#.NET, I want to fetch data from an URL and save it to a file in binary.

在 C#.NET 中,我想从 URL 获取数据并将其保存到二进制文件中。

Using HttpWebRequest/Streamreader to read into a string and saving using StreamWriter works fine with ASCII, but non-ASCII characters get mangled because the Systems thinks it has to worry about Encodings, encode to Unicode or from or whatever.

使用 HttpWebRequest/Streamreader 读入字符串并使用 StreamWriter 保存在 ASCII 中工作正常,但非 ASCII 字符会被损坏,因为系统认为它必须担心编码、编码为 Unicode 或来自或其他。

What is the easiest way to GET data from an URL and saving it to a file, binary, as-is?

从 URL 获取数据并将其按原样保存到文件、二进制文件的最简单方法是什么?

// This code works, but for ASCII only
String url = "url...";
HttpWebRequest  request  = (HttpWebRequest)
WebRequest.Create(url);

// execute the request
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();

// we will read data via the response stream
Stream ReceiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader( ReceiveStream );
string contents = readStream.ReadToEnd();

string filename = @"...";

// create a writer and open the file
TextWriter tw = new StreamWriter(filename);
tw.Write(contents.Substring(5));
tw.Close();

采纳答案by Marc Gravell

Minimalist answer:

极简回答:

using (WebClient client = new WebClient()) {
    client.DownloadFile(url, filePath);
}

Or in PowerShell (suggested in an anonymous edit):

或者在 PowerShell 中(建议匿名编辑):

[System.Net.WebClient]::WebClient
$client = New-Object System.Net.WebClient
$client.DownloadFile($URL, $Filename)

回答by Matthew Flaschen

Just don't use any StreamReader or TextWriter. Save into a file with a raw FileStream.

只是不要使用任何 StreamReader 或 TextWriter。使用原始 FileStream 保存到文件中。

String url = ...;
HttpWebRequest  request  = (HttpWebRequest) WebRequest.Create(url);

// execute the request
HttpWebResponse response = (HttpWebResponse) request.GetResponse();

// we will read data via the response stream
Stream ReceiveStream = response.GetResponseStream();

string filename = ...;

byte[] buffer = new byte[1024];
FileStream outFile = new FileStream(filename, FileMode.Create);

int bytesRead;
while((bytesRead = ReceiveStream.Read(buffer, 0, buffer.Length)) != 0)
    outFile.Write(buffer, 0, bytesRead);

// Or using statement instead
outFile.Close()

回答by Aviatrix

This is what I use:

这是我使用的:

sUrl = "http://your.com/xml.file.xml";
rssReader = new XmlTextReader(sUrl.ToString());
rssDoc = new XmlDocument();

WebRequest wrGETURL;
wrGETURL = WebRequest.Create(sUrl);

Stream objStream;
objStream = wrGETURL.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader(objStream, Encoding.UTF8);
WebResponse wr = wrGETURL.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
XmlDocument content2 = new XmlDocument();

content2.LoadXml(content);
content2.Save("direct.xml");