Saturday, June 4, 2011

Reading the contents of a web page is easy in C#

Reading the contents of a web page is easy in C# with the System.Net.WebClient class:


using System.Net;
using System.Windows.Forms;

string url = "http://www.devtopics.com";
string result = null;

try
{
WebClient client = new WebClient();
result = client.DownloadString( url );
}
catch (Exception ex)
{
// handle error
MessageBox.Show( ex.Message );
}The web page is read into the ‘result’ string. Note the URL you pass to the DownloadString method must have the http:// prefix, otherwise it will throw a WebException.

A more complicated but also more flexible solution is to use the System.Net.HttpWebRequest class, which enables you to interact directly with servers using HTTP:




using System.Net;
using System.IO;
using System.Windows.Forms;

string result = null;
string url = "http://www.devtopics.com";
WebResponse response = null;
StreamReader reader = null;

try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create( url );
request.Method = "GET";
response = request.GetResponse();
reader = new StreamReader( response.GetResponseStream(), Encoding.UTF8 );
result = reader.ReadToEnd();
}
catch (Exception ex)
{
// handle error
MessageBox.Show( ex.Message );
}
finally
{
if (reader != null)
reader.Close();
if (response != null)
response.Close();
}

No comments:

Post a Comment