9

Hy, I am using HttpWebRequest in 10 concurent threads to download a list of images. I sorted the images after the hostName so each of this threads are getting an image from the same Hostname.

HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
myReq.ServicePoint.ConnectionLimit = 10;
myReq.Timeout = 2000;
myReq.KeepAlive = true;

HttpWebResponse myResp = (HttpWebResponse )myReq.GetResponse();

After the program is running for a while I keep getting timeout exception. My thoughts are that I get exception because maybe the Host server has some limitation regarding the concurent connections from the same user.

So how is a connection reused in .net ? In my program each thread is creating a new connection to a hostname, or is reusing the existing one because of the KeepAlive property ??

2
  • 2
    What was the result of "In my program each thread is creating a new connection to a hostname, or is reusing the existing one because of the KeepAlive property ??"
    – merrick
    May 14, 2012 at 16:09
  • For the servers that supported http 1.1 protocol, it was reusing the connection. It was also important to request images from the same domain in the same time, so that I don't open more then 10 connections too often. .Net has a way of doing that automatically even if you are finishing one request.
    – Dorin
    May 15, 2012 at 8:34

1 Answer 1

4

It seems that the problem was some servers that were using http/1.0. HttpWebRequest is using 100 Continue behaviour but the server doesn't support it.

So i changed the property System.Net.ServicePointManager.Expect100Continue to false and then everything worked fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.