I wrote an application like this two years ago to stress test a web
site. Here is my code (c#) -- it is also able to do a form post as
well as url get. It uses WebRequest and uses a Thread array (rather
than Thread pool).
I was able to launch hundreds of threads simultaneously to simulate a
lot of users on web site.
Another thing you could do is implement Tracing in your application, and
put some log entries before the call to the threaded method, and inside
the threaded method before and after the HttpRequest to see where its
getting "stuck".
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Threading;
using System.Web;
namespace crossBow1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
///
class Arrow
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
/* if (args.Length < 1) {
showusage();
} else {
if (args.Length < 2 ) {
getPage(args[0], "s1=food&s2=bart");
} else {
getPage(args[0], args[1]);
}
}
*/
// Declare an array of 5 threads to be worker threads.
Thread[] t = new Thread[1000];
for ( int i=0; i< 40 ; i++)
{
t
= new Thread(new ThreadStart(quiver));
t.Start();
}
// Console.WriteLine();
// Console.WriteLine("Press any key to continue...");
// Console.ReadLine();
return;
}
public static void quiver()
{
int n = 0;
// string nVs=nameValuePairs();
while(n < 5000)
{
//getPage("http://halppd.vcrm2.com/broadbaseema/www/submitsurvey.jsp",
/*"openfield=five&" +
"SurveyID=0eff76b72e300");*/
//nVs);
//hitPage("http://halagent.vcrm2.com/broadbaseema/ha3094/5dayflash.htm");
//hitPage("http://192.168.25.64/HA3094/5dayflash.htm");
hitPage("http://192.168.25.64/HA3094/blah.html");
n++;
}
}
public static void hitPage(string url)
{
// Initialize the WebRequest.
WebRequest req = WebRequest.Create(url);
req.Timeout = 30000;
try
{
WebResponse result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
byte[] read = new byte[512];
int bytes = ReceiveStream.Read(read, 0, 512);
string InnerHtml = "";
while (bytes > 0)
{
// Note:
// The following assumes that the response uses UTF-8 as encoding.
// If the content is sent in a ANSI codepage like 932 use something
like this:
// Encoding encode = System.Text.Encoding.GetEncoding("shift-jis");
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
InnerHtml = InnerHtml + encode.GetString(read, 0, bytes);
bytes = ReceiveStream.Read(read, 0, 512);
}
Console.WriteLine(DateTime.Now + " " + "Success");
}
catch (Exception e)
{
Console.WriteLine(DateTime.Now + " " + "Error retrieving page");
}
}
public static void getPage(String url, String payload)
{
WebResponse result = null;
//Console.WriteLine("payload is " + payload );
try
{
WebRequest req = WebRequest.Create(url);
req.Timeout = 10000;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = {'?', '=', '&'};
byte[] SomeBytes = null;
if (payload != null)
{
int i=0, j;
while(i<payload.Length)
{
j=payload.IndexOfAny(reserved, i);
if (j==-1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i,
payload.Length-i)));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, j-i)));
UrlEncoded.Append(payload.Substring(j,1));
i = j+1;
}
SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}
else
{
req.ContentLength = 0;
}
result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader sr = new StreamReader( ReceiveStream, encode );
Console.WriteLine("\r\nResponse stream received");
Char[] read = new Char[256];
int count = sr.Read( read, 0, 256 );
Console.WriteLine("HTML...\r\n");
while (count > 0)
{
String str = new String(read, 0, count);
// Console.Write(str);
count = sr.Read(read, 0, 256);
}
}
catch(Exception e)
{
Console.WriteLine( e.ToString());
Console.WriteLine("\r\nThe request URI could not be found or was
malformed");
}
finally
{
if ( result != null )
{
result.Close();
}
}
}
}
}