3

I have read the following 2 articles and have tried implementing the same.

My code is this and the time out occurs here

HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = false;
wr.CookieContainer = cookieJar;

wr.Proxy = null;
wr.ServicePoint.ConnectionLeaseTimeout = 5000;
Stream rs = wr.GetRequestStream(); // -> Time out error occurs here

Article I read

My code using that as a sample
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = false;
wr.CookieContainer = cookieJar;

wr.Timeout = 5000;
wr.Proxy = null;

wr.ServicePoint.ConnectionLeaseTimeout = 5000;
wr.ServicePoint.MaxIdleTime = 5000;

Stream rs = wr.GetRequestStream(); //-->Time out error

Any clues would be helpful. At times I can get 1 request through and all other request fail or everything fails. Am posting to a HTTPS site. No issues when running with Fiddler

UPDATE 1: I tried following zbugs ideas but that resulted in same issue. the first request goes through the subsequent ones fail. I am closing all response streams as well as calling abort on my request object.

Community
  • 1
  • 1
user721264
  • 477
  • 2
  • 8
  • 22

3 Answers3

5

You need to set this.

const int maxIdleTimeout = 5000;
ServicePointManager.MaxServicePointIdleTime = maxIdleTimeout;

If you have more than one client making the request at any given time,

const int maxConnections = 100; //or more/less
ServicePointManager.DefaultConnectionLimit = maxConnections;

http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.maxservicepointidletime.aspx

Suma
  • 33,181
  • 16
  • 123
  • 191
zbugs
  • 591
  • 2
  • 10
  • would i have to do something like this? wr.ServicePoint.ConnectionLeaseTimeout = 5000; wr.ServicePoint.MaxIdleTime = 5000; ServicePointManager.MaxServicePointIdleTime = 5000; Stream rs = wr.GetRequestStream(); as that resulted in same time out exception – user721264 May 18 '12 at 20:56
  • no. ServicePointManager.MaxServicePointIdleTime etc.. and you do have to dispose the wr object and the stream after you are done. – zbugs May 18 '12 at 21:02
  • Yes i have the close and dispose method as well as wr.Abort() – user721264 May 18 '12 at 21:18
  • all you need to do is try that before making the entering that method or creating the wr object. Set the ServicePointManager values. – zbugs May 18 '12 at 21:51
1

You should release all resources that implement IDisposable. Not releasing them can cause the kind of issue you're experimenting.

Here you have an example that is not exaclty your code but it will help you to undertand what I mean

var request = WebRequest.Create(url);
request.ContentType = "application/json; charset=utf-8";

using (var response = request.GetResponse())
{
    using (var sr = new StreamReader(response.GetResponseStream()))
    {
    ...
    }
}
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
0

On one of my request call, I had failed to call

Request.Abort()

That seems to have solved my issue along with suggestion from zbugs

user721264
  • 477
  • 2
  • 8
  • 22