Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

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.

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

// 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(); 
like image 448
jms Avatar asked Jun 14 '09 09:06

jms


People also ask

How do you save data in binary?

If saved in binary as just a copy of the float 's bits, it will take four characters (four bytes, or 32 bits) on a typical 32-bit system. The exact number of bits stored by a call such as: FILE *my_file = fopen("pi. bin", "wb"); float x = 3.1415; fwrite(&x, sizeof x, 1, my_file);

How do I write a BIN file?

To write to a binary fileUse the WriteAllBytes method, supplying the file path and name and the bytes to be written. This example appends the data array CustomerData to the file named CollectedData. dat .

Which methods are used to write and read into from a binary file?

The BinaryReader and BinaryWriter classes are used for reading from and writing to a binary file.


2 Answers

Minimalist answer:

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

Or in PowerShell (suggested in an anonymous edit):

[System.Net.WebClient]::WebClient $client = New-Object System.Net.WebClient $client.DownloadFile($URL, $Filename) 
like image 141
Marc Gravell Avatar answered Sep 18 '22 16:09

Marc Gravell


Just don't use any StreamReader or TextWriter. Save into a file with a raw 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() 
like image 20
Matthew Flaschen Avatar answered Sep 19 '22 16:09

Matthew Flaschen