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


About the Author

Tom Archer - MSFT

I am a Program Manager and Content Strategist for the Microsoft MSDN Online team managing the Windows Vista and Visual C++ developer centers. Before being employed at Microsoft, I was awarded MVP status for the Visual C++ product. A 20+ year veteran of programming with various languages - C++, C, Assembler, RPG III/400, PL/I, etc. - I've also written many technical books (Inside C#, Extending MFC Applications with the .NET Framework, Visual C++.NET Bible, etc.) and 100+ online articles.

Comments

  • you should use FileStream in place of Stream

    Posted by Rajneesh on 02/17/2016 03:54am

    I think you should use FileStream in place of Stream in local stream, because stream works so slow.

    Reply
  • Check download progress

    Posted by Wendy on 12/03/2014 10:06pm

    How can I show the progress of files download in a progress bar and how many percent left in this case?

    Reply
  • raw file download?

    Posted by ktanriover on 08/11/2007 03:53am

    Thanks for the code. Anyway, I'd like to download the raw file (not rendered, let's say an aspx file on the server). I think "webrequest" automatically sets the remote file to be rendered by the server, somehow like we see it via browser. Any suggestions?

    Reply
  • VB.Net syntax note

    Posted by Fragiletruce on 02/01/2007 03:52pm

    Very 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 If

    Reply
  • Problem

    Posted by mlheese on 12/14/2005 11:03am

    Code 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?

    Reply
  • Login + Password

    Posted by netrom on 08/02/2005 05:04am

    What 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"

    Reply
  • Events

    Posted by coderforrent.com on 05/11/2004 04:08pm

    Does the WebRequest object have any events that can show the progress of the download?

    Reply
  • Check if file already in IE cache

    Posted by fregate on 04/21/2004 01:14am

    How in .NET (without Win32 API) check that file exists in IE cache before downloading file.

    Reply
  • How about using this function, URLDownloadToFile?

    Posted by megaxoom on 04/16/2004 03:55pm

    I 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

    • Why go through so much work? Here's a reason:

      Posted by Fragiletruce on 02/01/2007 04:01pm

      One 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.

      Reply
    • This is a .NET series

      Posted by Tom Archer on 04/16/2004 05:07pm

      While 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.

      Reply
    Reply
  • Access denied error

    Posted by GangWU on 04/15/2004 12:55pm

    When 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.

    • What is D: on your system?

      Posted by Tom Archer on 04/16/2004 02:21am

      It 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
    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Enterprise cloud adoption has evolved rapidly from fringe curiosity to the mainstream. As enterprises increasingly move mission-critical workloads to the cloud, it's important to track best practices to ensure a seamless migration process. While CIOs are becoming increasingly mature and pragmatic in their approach to cloud, surprises and challenges still need to be addressed. Read this eBook to learn the key best practices for cloud deployment success, the importance of SLAs in choosing a cloud provider, and …

  • Are you weighing the benefits of cloud storage versus on-premises storage? If so, the right answer might be to use both — a mix of on-premises and public cloud services with data mobility between the two platforms. Hybrid has piqued interest as more organizations look to the public cloud to augment their on-premises data management. Analyst firm IDC predicts that by 2018, 85% of enterprises will operate in a multi-cloud environment. Check out this easy-to-absorb infographic to explore five reasons to …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date