Parallel HttpRequest. How?

Apr 2 2011 4:13 AM
Hello Experts,

I have the following Console app that makes a single request to a web page and returns its server status eg. 200, 404, etc..

How would I change it to:

Ask user for:

Url to request
How many parallel connections to use(concurrent users)
How long(seconds) to submit as many requests as it can

Show Total Fetches
Show Fetches per Second
Show Average Response Time (ms)

I imagine the best way to do it is to run multiple http fetches in parallel and run in a single process, so it doesn't bog down the client machine.

I really like C# but I'm still new to it. I've researched other articles about this but I don't fully understand it so any help would be greatly appreciated.

My Code:

  static void Main(string[] args)
  {
  try
  {
  HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://10.10.1.6/64k.html");
  webRequest.AllowAutoRedirect = false;
  HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
  //Returns "MovedPermanently", not 301 which is what I want.

  int i_goodResponse = (int)response.StatusCode;
  string s_goodResponse = response.StatusCode.ToString();
  Console.WriteLine("Normal Response: " + i_goodResponse + " " + s_goodResponse);
  Console.ReadLine();
  }
  catch (WebException we)
  {
  int i_badResponse = (int)((HttpWebResponse)we.Response).StatusCode;
  string s_badResponse = ((HttpWebResponse)we.Response).StatusCode.ToString();
  Console.WriteLine("Error Response: " + i_badResponse + " " + s_badResponse);
  Console.ReadLine();
  }
  }

Answers (7)