Web request in a c# app

T

ThunderMusic

Hi,
What I want to do is send Http requests for a stress test application. The
url I query will send me a 404, 400, maybe others or redirect me to a
different url. I need to do that asynchronously. I mean, I could possibily
send request using 10 threads and sending 10 requests per thread on each
time the thread loops. I doubt the Microsoft WebBrowser control will do the
trick here.

So, to summarise what I need to do : Send a query (go to a URL that will be
an ASP.NET custom handler), receive the query return (400, 404, another
URL), process the new URL to know if it's expected.

Does someone know a place where I can find code to do this? As for now, I
only found code that uses the MS WebBrowser control. Does anyone know which
classes I should use for this? I can look in msdn doc if needed afterward,
but I need something to start with.

Thanks a lot

ThunderMusic
 
M

Marc Gravell

WebBrowser is for interactive usage (it is pretty much IE, as it is a
wrapper around SHDOCVW); perhaps you should look as WebClient, which is
a wrapper around (IIRC) HttpWebRequest and HttpWebResponse:

using (WebClient wc = new WebClient())
{
string page200 =
wc.DownloadString("http://www.google.com");
// expect this to throw
string page404 =
wc.DownloadString("http://www.google.com/doesntexist");
}

Note: HttpWebResponse provides more access to headers
Note: Pages sometimes distinguish by user-agent; you may need to spoof
the UA (header) to give realistic results
Note: another option - re-use existing code like WAS or CURL...

Marc
 
M

Marc Gravell

I just noticed your async requirement; personally, I would do this sync
(local to each thread), but on parallel threads (perhaps ThreadPool,
perhaps not) - keeps it simple for debugging; HttpWebRequest etc does
sport async options, but I believe this just runs sync on the pool
anyway.

You might also want to watch for saturation; I don't know how parallel
these classes are happy to go... you could always drop a few layers,
but that is getting grungy.

Marc
 
H

Henning Krause [MVP - Exchange]

Hello,
I just noticed your async requirement; personally, I would do this sync
(local to each thread), but on parallel threads (perhaps ThreadPool,
perhaps not) - keeps it simple for debugging; HttpWebRequest etc does
sport async options, but I believe this just runs sync on the pool
anyway.

actually, the HttpWebRequest sync methods run always async (using 2
threadpool threads per request), blocking the currrent thread until the
request completes...

Best regards,
Henning Krause
 
L

Laurent Bugnion [MVP]

Hi,
Hello,


actually, the HttpWebRequest sync methods run always async (using 2
threadpool threads per request), blocking the currrent thread until the
request completes...

Why is it blocking if it's asynchronous? I don't get it.

Laurent
 
H

Henning Krause [MVP - Exchange]

Hello,

Marc was talking about the synchronous methods.

The synchronous methods of the HttpWebRequest perform the actual request on
threadpool threads and block the calling thread until the request has
finished.

Best regards,
Henning Krause

Laurent Bugnion said:
Hi,
Hello,


actually, the HttpWebRequest sync methods run always async (using 2
threadpool threads per request), blocking the currrent thread until the
request completes...

Why is it blocking if it's asynchronous? I don't get it.

Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 
L

Laurent Bugnion [MVP]

Hi,
Hello,

Marc was talking about the synchronous methods.

The synchronous methods of the HttpWebRequest perform the actual request
on threadpool threads and block the calling thread until the request has
finished.

Best regards,
Henning Krause

Sounds better. I was confused by your statement "the HttpWebRequest sync
methods run always async". Now I understand that the "async" you mention
is not regarding the request itself, but regarding the threading model.

Thanks for clarifying.

Greetings,
Laurent
 
T

ThunderMusic

I doubt it will suit my needs because I need to get data dynamicly from a
database for building the URLs and doing the stress test and doing an export
job to csv or anything else before starting the actual job would be way out
of the specifications I received...

but thanks a lot, it could have been the way to go... ;) I'll keep the link
anyway, it could be useful in the future... ;)

ThunderMusic
 
N

Nick Malik [Microsoft]

Out of the specifications?

Sorry to be impertinent. I don't work in your environment, and I don't know
your boss, but if someone handed me a free tool that COMPLETELY REPLACES the
work I'm doing, I'm not sure I'd pass it off so easily. Not only does WAST
perform stress testing, but there are also more robust (read: not-free)
tools that will do this, all of them cheaper than having you write code.

Tell your boss you can save him thousands of dollars (in your billable time)
and show him the list of products on the market to do stress testing against
web sites. Include the Free one (WAST). You will be a HERO for saving
money and time.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top