当我向多个客户端发送短信时,它会给出错误操作超时并且错误发生在HttpWebResponse上
我试过了 myReq.Timeout = 50000; myReq.ReadWriteTimeout = 50000;
但在第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();
答案 0 :(得分:15)
这可能是问题所在:
HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
WebResponse
实现IDisposable
,因此您应该使用using
语句(以及您从流中创建的StreamReader
)。如果您打开WebResponse
,它将占用从连接池到该主机的连接,并且您最终可以通过这种方式进行超时。固定代码如下所示:
string responseString;
using (var response = myReq.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream())
{
responseString = reader.ReadToEnd();
}
}
即使抛出异常,这也会关闭流和响应,因此您将始终清理资源(在这种情况下,将连接释放回池中)。