问题描述
我的asp.net应用程序发送的HttpWebRequest到远程REST服务器,并等待响应,我发现有很多相同的错误消息的是这样的:
Is that possible that after I catch this exception and close the underlying http connection directly? or I don't really have to do so since I already set keepalive to false?
Thanks.
Actually another questions is if the timeout exception always happened at System.Net.HttpWebRequest.GetResponse(),
does that mean application is waiting for the response from remote server and could not get response until time out. what could be the possible reason, network connection not stable? remote server not response? any other possible reasons?
Here is the code:
System.Net.HttpWebResponse httpWebResponse = null;
System.IO.Stream stream = null;
XmlTextReader xmlTextReader = null;
try
{
System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(request);
httpWebRequest.ReadWriteTimeout = 10000;
httpWebRequest.Timeout = 10000;
httpWebRequest.KeepAlive = false;
httpWebRequest.Method = "GET";
httpWebResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
stream = httpWebResponse.GetResponseStream();
xmlTextReader = new XmlTextReader(stream);
xmlTextReader.Read();
xmlDocument.Load(xmlTextReader);
//Document processing code.
//...
}
catch
{
//Catch blcok with error handle
}
finally
{
if (xmlTextReader != null)
xmlTextReader.Close();
if (httpWebResponse != null)
httpWebResponse.Close();
if (stream != null)
stream.Close();
}
The simple rule-of-thumb is that if it doesn't implement IDisposal then it doesn't need disposing of.
这篇关于如何关闭HttpWebRequest的抓超时后底层连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!