Downloading Files with the WebRequest and WebResponse Classes


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

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
    // 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
          // 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)
    // 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",
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.


  • you should use FileStream in place of Stream

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

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

  • Check download progress

    Posted by Wendy on 12/04/2014 06:06am

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

  • raw file download?

    Posted by ktanriover on 08/11/2007 10: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?

  • VB.Net syntax note

    Posted by Fragiletruce on 02/01/2007 11: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)
    End If

    • thanks

      Posted by ehab on 09/23/2017 06:43am

      worked ,THANKS FOR YOUR HELP

  • Problem

    Posted by mlheese on 12/14/2005 07:03pm

    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?

  • Login + Password

    Posted by netrom on 08/02/2005 12:04pm

    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"

  • Events

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

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

  • Check if file already in IE cache

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

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

  • How about using this function, URLDownloadToFile?

    Posted by megaxoom on 04/16/2004 10: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/02/2007 12:01am

      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.

    • This is a .NET series

      Posted by Tom Archer on 04/17/2004 12:07am

      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.

  • Access denied error

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

    When I use this piece of code, it caught an error:
      catch(Exception e)
    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?

    • What is D: on your system?

      Posted by Tom Archer on 04/16/2004 09: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.

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

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