timeout on http post when multiple posts in parallel

  • Thread starter Wilfried Mestdagh [MapPoint MVP]
  • Start date
W

Wilfried Mestdagh [MapPoint MVP]

Hello,

I do a HTTP POST in a thread with following code (only relevant copied here):

Cli cli = new Cli();
Thread cliThread = new Thread(new ThreadStart(cli.Run));
cliThread.Start();

the Cli class is like this:

public void Run()
{
HttpWebResponse webResponse;
try {
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);
req.Timeout = timeout; // set to 10 seconds
req.Method = "POST";
StreamWriter stOut = new StreamWriter(req.GetRequestStream(),
System.Text.Encoding.Default);
stOut.Write(xml);
stOut.Close();
webResponse = (HttpWebResponse)req.GetResponse();
statuscode = (int)webResponse.StatusCode;
StreamReader stIn = new StreamReader(webResponse.GetResponseStream());
string response = stIn.ReadToEnd();
stIn.Close();
} catch (WebException webEx) {

This works fine with one thread. But when I do several requests in parallel
then I get webexception "the operation timed out". I checked with a tool (a
kind of proxy that log all TCP traffic) and the server is giving immediatly a
200 and a page for "each" POST. So the data is received every time. But wy is
the timeout generated?

Can someone advice here?
 
W

Wilfried Mestdagh [MapPoint MVP]

Hi,

Thank you for your answer. The multiple threads are started with this code
called several times from within the main thread:
Cli cli = new Cli();
Thread cliThread = new Thread(new ThreadStart(cli.Run));
cliThread.Start();

So the "Run()" method of the Cli class is running in a separated thread
context. Question: do I still need the BeginGetRequestStream instead of
GetRequestStream ??? Because the code is already executed in his own thread.
Or am I missing something?

--
rgds, Wilfried [MapPoint MVP]
http://www.mestdagh.biz


Angel J. Hernández M. said:
Hi Wilfried,

In your previous example I just see one thread, but anyways if you want to
do it with multiple threads then you need to do it in an Asynchronous way.
Try implementing BeginGetRequestStream instead of GetRequestStream

http://msdn.microsoft.com/en-us/library/system.net.webrequest.begingetrequeststream.aspx


Regards,



--
Angel J. Hernández M
MCP,MCAD,MCSD,MCDBA
Microsoft MVP
http://msmvps.com/blogs/angelhernandez
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
http://www.customware.net
Technical Solutions Architect




"Wilfried Mestdagh [MapPoint MVP]"
 

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