Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

HTTP GET with .NET WebClient

0.00/5 (No votes)
2 Mar 2009 1  
Methods for performing HTTP GETs in C# using WebClient and StreamReader.

Introduction

One of the joys of developing with .NET is, a significant amount of the ground work which we previously had to code ourselves is now part of the framework. In this article, I show methods for performing HTTP GETs in C# using the WebClient and the StreamReader. I'll use these methods in future articles.

First, let's introduce Stream and StreamReader, which can both be found in the System.IO namespace. The StreamReader implements a TextReader to read UTF-8 characters by default from a stream (the source), which makes it ideal for reading from a URI. Take note that StreamReader is different from Stream, which reads bytes.

For sending data to and from a URI, .NET provides the WebClient class which can be found in the System.Net namespace. Several methods are available to enable us to send and receive files and data, both synchronously and asynchronously. The method we are interested in here is OpenRead(URI), which returns data from the URI as a Stream.

Let's code

The basic code to read from our URI can be achieved in three lines. We create our WebClient instance, create a Stream from the WebClient, and then read into a StreamReader until the end of the file, like so:

using System.IO;
using System.Net;

String URI = "http://somesite.com/somepage.html";

WebClient webClient = new WebClient();
Stream stream = webClient.OpenRead(URI);
String request = reader.ReadToEnd();

After we have run over this code, the contents of somepage.html will be in the request string variable. This is all great, but we are presuming that the request here is faultless.. i.e., no exceptions are thrown. With exception handling being so easy in .NET, there's no excuse not to make benefit of it... although from experience, it seems not everyone is of the same opinion...

Let's wrap our Stream requests into a try-catch loop. We can catch a WebException to clearly identify what has gone wrong, and deal with it nicely.

try
{
    WebClient webClient = new WebClient();
    Stream stream = webClient.OpenRead(URI);
    String request = reader.ReadToEnd();
}
catch (WebException ex)
{
    if (ex.Response is HttpWebResponse)
    {
        switch (((HttpWebResponse)ex.Response).StatusCode)
        {
            case HttpStatusCode.NotFound:
                response = null;
                break;

            default:
                throw ex;
        }
    }
}

We can further optimize the code by the wrapping using(...) around the WebClient/Stream, but that's beyond the scope of this article.

Authentication

If you have a URI which requires authentication, you can add a NetworkCredential to the WebClient reference before you call the OpenRead method, like so:

WebClient webClient = new WebClient();
webClient.Credentials = new NetworkCredential(username, password);
Stream stream = webClient.OpenRead(URI);

A real world example of using the above would be retrieving a list of your latest Tweets from Twitter. You need to pass your username and password to be able to get to the feed. The example download uses this as a demonstration, so you will need to add your own Twitter username and password.

History

No changes.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here