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.