简体   繁体   English

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

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

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();

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)

Just don't use any StreamReader or TextWriter. 只是不要使用任何StreamReader或TextWriter。 Save into a file with a raw FileStream. 使用原始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()

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM