Click here to Skip to main content
Click here to Skip to main content

HTTP GET with .NET WebClient

, 2 Mar 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

n05robsta
Software Developer (Senior)
United Kingdom United Kingdom
I'm a ASP.NET developer based in Brighton, UK.

Comments and Discussions

 
QuestionServerProtocolViolation PinmemberkPlusPlusA15-Aug-14 22:33 
Questionyou have not defined reader in your code. Pinmemberrandom425-Jan-13 0:39 
QuestionHttp get PinmemberAbirami-8721-Jan-13 20:46 
AnswerRe: Http get Pinmemberrandom425-Jan-13 0:52 
QuestionMy working code 2 Pinmemberbrentminder28-Nov-12 4:56 
AnswerRe: My working code 2 Pinmemberrandom425-Jan-13 0:51 
QuestionEndofStream blocking PinmemberEldon Zacek14-Aug-12 11:41 
QuestionMy working code PinmemberHerbert Yu6-Feb-12 11:15 
QuestionCompile error in code above PinmemberMark J2-Jan-12 21:08 
GeneralWebclient Not working ... Pinmemberabhibhalani5-Sep-10 23:24 
Generalhhtps does not work PinmemberMember 75098514-Mar-10 22:26 
GeneralHTTP Get.. Pinmembersbarclay18-Jan-10 7:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 2 Mar 2009
Article Copyright 2009 by n05robsta
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid