Downloading Files with the WebRequest and WebResponse Classes
A few years ago, I was being tasked with writing a console application that would download and save from the Web a file name that was passed to it via the command line. The application took me about four hours to write and involved a lot of Sockets programming and data conversion. With .NET, the WebRequest and WebResponse classes now reduce this task to about five minutes!
Here's a simple function that you can easily plug into your C# application; it will download a specified server file and save it to a local file. Instead of describing the function and the presenting it, the function contains comments that document exactly what is going on.
// Remember to add the following using statements to your code // using System.Net; // using System.IO; public static int DownloadFile(String remoteFilename, String localFilename) { // Function will return the number of bytes processed // to the caller. Initialize to 0 here. int bytesProcessed = 0; // Assign values to these objects here so that they can // be referenced in the finally block Stream remoteStream = null; Stream localStream = null; WebResponse response = null; // Use a try/catch/finally block as both the WebRequest and Stream // classes throw exceptions upon error try { // Create a request for the specified remote file name WebRequest request = WebRequest.Create(remoteFilename); if (request != null) { // Send the request to the server and retrieve the // WebResponse object response = request.GetResponse(); if (response != null) { // Once the WebResponse object has been retrieved, // get the stream object associated with the response's data remoteStream = response.GetResponseStream(); // Create the local file localStream = File.Create(localFilename); // Allocate a 1k buffer byte[] buffer = new byte[1024]; int bytesRead; // Simple do/while loop to read from stream until // no bytes are returned do { // Read data (up to 1k) from the stream bytesRead = remoteStream.Read (buffer, 0, buffer.Length); // Write the data to the local file localStream.Write (buffer, 0, bytesRead); // Increment total bytes processed bytesProcessed += bytesRead; } while (bytesRead > 0); } } } catch(Exception e) { Console.WriteLine(e.Message); } finally { // Close the response and streams objects here // to make sure they're closed even if an exception // is thrown at some point if (response != null) response.Close(); if (remoteStream != null) remoteStream.Close(); if (localStream != null) localStream.Close(); } // Return total bytes processed to caller. return bytesProcessed; }
Finally, here's an example of using the DownloadFile function.
int read = DownloadFile("http://www.mysite.com/problem1.jpg",
"d:\\test.jpg");
Console.WriteLine("{0} bytes written", read);

Comments
raw file download?
Posted by ktanriover on 08/11/2007 03:53amVB.Net syntax note
Posted by Fragiletruce on 02/01/2007 03:52pmVery clear, very helpful. I had to spend some time working out the syntax for VB.Net so here's the code: Dim webRequest as HTTPWebRequest = webRequest.Create(docurl) webRequest.Method = "GET" webRequest.ContentType = "application/x-www-form-urlencoded" webRequest.CookieContainer = cookies Dim bytesProcessed As Integer = 0 Dim remoteStream As Stream Dim localStream As Stream Dim response As WebResponse response = webRequest.GetResponse() If Not response Is Nothing Then remoteStream = response.GetResponseStream() localStream = File.Create(targetfile) 'Declare buffer as byte array Dim myBuffer As Byte() 'Byte array initialization ReDim myBuffer(1024) Dim bytesRead As Integer bytesRead = remoteStream.Read(myBuffer, 0, 1024) Do While (bytesRead > 0) localStream.Write(myBuffer, 0, bytesRead) bytesProcessed += bytesRead bytesRead = remoteStream.Read(myBuffer, 0, 1024) Loop localStream.Close() End IfReplyProblem
Posted by mlheese on 12/14/2005 11:03amCode doesn't work. The file is only copied to the Server's drive and never actually makes it across the network to the Client. I created a C# Web Application and tried the code but when I ran the webpage from another PC, the file only gets copied to the Server's drive and never makes it across to the Client PC. Am I doing something wrong?
ReplyLogin + Password
Posted by netrom on 08/02/2005 05:04amWhat if the page you want to download, requires a Login and Password?? How to send those Param, this is the file I wish to download http://www.ip2location.com/download.asp?filename=IPCountry-FULL.zip but this requires Login + Password, and now I only download the site "download.asp"
ReplyEvents
Posted by coderforrent.com on 05/11/2004 04:08pmDoes the WebRequest object have any events that can show the progress of the download?
ReplyCheck if file already in IE cache
Posted by fregate on 04/21/2004 01:14amHow in .NET (without Win32 API) check that file exists in IE cache before downloading file.
ReplyHow about using this function, URLDownloadToFile?
Posted by megaxoom on 04/16/2004 03:55pmI have been using this function, URLDownloadToFile, to download a file from internet. Why goes through so much work, when this function is available? To use: char* url = "http://www.mysite.com/problem1.jpg"; char* output = "d:\\test.jpg"; URLDownloadToFile(NULL, url, output, 0, NULL); Library files: header file : Urlmon.h library file : Urlmon.lib
-
-
ReplyWhy go through so much work? Here's a reason:
Posted by Fragiletruce on 02/01/2007 04:01pmOne reason for using the url webrequest is so you can download from a protected site. Login and setup a cookie container. Attach the cookies to subsequent requests (ie for file downloads). You can't do that with URL download.
ReplyThis is a .NET series
Posted by Tom Archer on 04/16/2004 05:07pmWhile I'm aware of the native APIs for accomplishing this task, my articles are specifically focused on tips and techniques involving the .NET BCL. To that extent, this particular article gives a practical example of using two very useful Internet classes - WebRequest and WebResponse.
ReplyAccess denied error
Posted by GangWU on 04/15/2004 12:55pmWhen I use this piece of code, it caught an error: catch(Exception e) { Console.WriteLine(e.Message); } says: Access to the path \"d:\\test.jpg\" is denied. When it passes the second string I saw a "@" in front of "d:\\test.jpg". How can I do with this? Thanks.-
ReplyWhat is D: on your system?
Posted by Tom Archer on 04/16/2004 02:21amIt sounds like you either don't have rights to the D:/ or more likely, it is a CD-ROM drive. Remember that is an example. On my system, D: is a hard disk. You will need to change the remote file and possibly the local file to suit your particular needs.
Reply