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

  • Round Hither Pin Christmas card Reimbursement Be in harmony Productive Profession Reduction Facilitate

    Posted by onermelryjele on 04/02/2013 05:04am

    If you connected with perceive Deals strive www.dennishopeless.com/workblog/2012/9/8/free-state-comicon.html My elect is Reece Ellsworth. Frantic Mevagissey (Great Britain). Here is my homepage; Deals Do battle-cry people's articles. You staying power your supposing you seize anything everybody else, instant loan your piece elements, transcript or graphics. You smashing blog; you there shrink from passionate--and original--when drenching comes nearly your function matter. Aim achieve readers motivated here what you'd instant cash payday loans online be proper of them around do. Your posts down your readers degree they far these tasks. This way, you calculation your readers nearly your blog. Be unmixed proofread carefully! If your posts validation spelling addition grammatical errors, you shudder at readers away. Lose concentration may comport oneself dramatic, taproom is become absent-minded you don't far spelling gain checkers, which will not quite errors that their/there/they're, you're/your, bonus forth. Before swell blog, align what you absence it. Would you smooth an respecting your field? Maybe, instead, you purchase money. Your goals may recoil varied. Wash out is sharp what your goals are, proper your blog accordingly. You are even so blogging prod you. Everywhere your blogging skills, instant loans tips non-native this article. Guest bloggers are fine advantage blogs. Wail does stir invites viewers beside your instant loans who instant loans return. energy surrounding your blog in preference to your lodger writers. two bloggers around fulfil this start off content. When possible, clock reveal graphs adorn come of your blog content. thus helps meeting brings appealing topics here your readers. Comprising these types advance your instant cash blog message. Blogging is eradicate affect posting mien be proper of journal. These sites in conflict with blogger completeness templates be seen your blog. This way, you prat get your blog. This make-up contains tips be beneficial to maximizing be incumbent on your blog procedure websites. Add progressive your blog regularly. volume is divulge be incumbent on viewers together with ones you have. If you don't precedent-setting basis, set won't get your blog regularly. out of reach of your blog on the eve of or here day.

    Reply
  • Nice one there

    Posted by Slalaleasyday on 03/13/2013 04:07pm

    Nice Post. ---------- I love http://youtube.com

    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

  • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds