Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# save a file from a HTTP Request

Tags:

c#

Im trying to download and save a file from a HttpWebResponse but im having problems saving the file (other than Text Files) properly.

I think its something to do with this part:

byte[] byteArray = Encoding.UTF8.GetBytes(http.Response.Content); MemoryStream stream = new MemoryStream(byteArray); 

Text Files work fine with the above code but when I try to save the Content to an Image file it gets corrupted. How do i write this 'string' data to an image file (and other binary files)

Forgot to mention, This is .NET CP 3.5 and I have a wrapper class around the HttpWebResponse class to add OAuth etc.

like image 321
dkarzon Avatar asked May 29 '10 08:05

dkarzon


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

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.

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

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);     } } 
like image 122
Jon Skeet Avatar answered Sep 24 '22 19:09

Jon Skeet


Use WebClient.DownloadFile. You can do it manually (something like this), but WebClient is the best bet for simple downloads.

like image 24
Matthew Flaschen Avatar answered Sep 23 '22 19:09

Matthew Flaschen