Hitting a web page programmatically

by Scosby Tuesday, January 19, 2010

Let's say you have a friend who is trying to win a popularity contest on a local web site. Furthermore, let's say that after you go to the site and vote you notice there is no login required and you see a bunch of crap in the query string for links as you hover over them. Let's go a step further and say you decide to write a program that will hit the website and perform the vote automatically. So, let's look at the classes in the .NET Framework that will help you do this simple example.

You will need to make a HttpWebRequest. This class allows you to work directly with a web server via HTTP and get a response back from it. To get a HttpWebResponse, we create a request and then get a response back. Quite simple, really.

Let's look at some code now.

HttpWebResponse response = null; 

try
{   
     HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(site);

     req.UserAgent = @"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0)";
    

     response = (HttpWebResponse)req.GetResponse();
    

     String
message = "Http response code " + ((Int32)response.StatusCode).ToString() + " :: " + Enum.GetName(typeof(HttpStatusCode), response.StatusCode);
    

     this
.UpdateTextBox(message);
}
finally
{   
     response.Close();

}

Let's break a couple of lines down and look at them in more depth. The first line we'll look at creates our web request:

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(http://www.webserver.com/View.aspx?ID=134684&R=10);

Here, the static Create method on the HttpWebRequest class returns as an instance of the abstract base class WebRequest, so we must cast it back to a HttpWebRequest. We pass in the full URL to the Create method, this includes any query string parameters that you need.

In the next line we'll look at you'll see how we actually hit the server:

response = (HttpWebResponse)req.GetResponse();

Here, the GetResponse method on the HttpWebRequest class returns the response from the web server. Note, similar to the HttpWebRequest.Create method we need to cast the WebResponse return value to a HttpWebResponse. We can then use the StatusCode property to determine the results of our operation. I store this information in a variable named "message", which is then passed off to a contrived example method to perform further processing in this example.

The last step you should take is to release the the connection for reuse by other requests, you accomplish this by calling the HttpWebResponse.Close method.

This is a simple, abeit trivial, example of how you can work with web servers in code and help your friends win contests. This could easily be extended and plugged into a windows service, with a config file for the site and various other options...maybe even a winforms configuration application. Sounds like a good homework project to me ;)

-Scosby

Tags: , ,

Technology | Programming

blog comments powered by Disqus