user2181428
user2181428

Reputation: 43

System.Net.WebException: The operation has timed out on HttpWebResponse

when i send sms to multiple clients it gives an error the operation time out and error is on HttpWebResponse

i have tried myReq.Timeout = 50000; myReq.ReadWriteTimeout = 50000;

but giving same error error on line 150
Line 148: myReq.Timeout = 50000;
Line 149: myReq.ReadWriteTimeout = 50000;
Line 150: HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
Line 151: System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream());
Line 152: string responseString = respStreamReader.ReadToEnd();

Upvotes: 4

Views: 15296

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500953

This may well be the problem:

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

WebResponse implements IDisposable, so you should use a using statement for it (and for the StreamReader you create from the stream). If you leave a WebResponse open, it will take up a connection from the connection pool to that host, and you can end up with timeouts this way. The fixed code would look like this:

string responseString;
using (var response = myReq.GetResponse())
{
    using (var reader = new StreamReader(response.GetResponseStream())
    {
        responseString = reader.ReadToEnd();
    }
}

This will close the stream and the response even if an exception is thrown, so you'll always clean up the resources (in this case releasing the connection back to the pool) promptly.

Upvotes: 19

Related Questions