简体   繁体   中英

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();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM