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

Changing an installed Windows service's log on account.

by Scosby Wednesday, August 26, 2009

Often, you will install a Windows service as part of your deployment. Usually, the service account (log on account) is well known (NT Authority account such as Local System, Network Service, ect) and you will install the service to run as this account, or some other specific account known at installation time. However, the need can arise to change this account as part of your application's configuration. You can accomplish this task by using the sc.exe command line utility.

sc.exe config <serviceName> obj= <domain\username> password= <password>

For example, suppose we have a service running on the local machine named "MyService" and we wish to change its log on account from local system to "acme\serviceAccount". The syntax is quite simple:

sc.exe config MyService obj= "acme\serviceAccount" password= P@ssw0rd

Now let's assume you wish to change the same service's log on account back to local system:

sc.exe config MyService obj= localsystem 

Here is a C# code snippet.

private void ChangeServiceAccount(String serviceName, String user, String password)
{
     System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo("sc.exe");

     startInfo.Arguments = String.Format("config {0} obj= {1} password= {2}", serviceName, user, password);

     startInfo.RedirectStandardOutput = true;

     startInfo.UseShellExecute = false;

     using (System.Diagnostics.Process p = new System.Diagnostics.Process())
     {
          p.StartInfo = startInfo;

          if (p.Start())
          {
               p.WaitForExit();
          }

          System.Diagnostics.Trace.WriteLine(p.StandardOutput.ReadToEnd());

          //Alternatively, you could display this in a message box
          //   MessageBox.Show(p.StandardOutput.ReadToEnd());
     }

Tags: ,

IT | Programming