如何关闭HttpWebRequest的抓超时后底层连接 [英] How to close underlying connections after catch httpwebrequest timeout

查看:354
本文介绍了如何关闭HttpWebRequest的抓超时后底层连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的asp.net应用程序发送的HttpWebRequest到远程REST服务器,并等待响应,我发现有很多相同的错误消息的是这样的:


  

System.Net.WebException:操作已超时。在
  System.Net.HttpWebRequest.GetResponse()


这是可能的,我捕获这个异常后直接关闭底层http连接?不然我真的没有这样做,因为我已经设置的存活假?

感谢。

其实另一个问题是,如果超时异常总是在 System.Net.HttpWebRequest.GetResponse()发生这是否意味着应用程序正在等待来自远程服务器的响应无法获得响应,直到超时。可能是什么可能的原因,网络连接不稳定?远程服务器不响应?其他可能的原因?

下面是code:

  System.Net.HttpWebResponse httpWebResponse = NULL;
的System.IO.Stream流= NULL;
XmlTextReader的XmlTextReader的= NULL;
尝试
{
    System.Net.HttpWebRequest HttpWebRequest的=(System.Net.HttpWebRequest)System.Net.WebRequest.Create(请求);
    httpWebRequest.ReadWriteTimeout = 10000;
    httpWebRequest.Timeout = 10000;
    httpWebRequest.KeepAlive = FALSE;
    httpWebRequest.Method =GET;
    httpWebResponse =(System.Net.HttpWebResponse)httpWebRequest.GetResponse();
    流= httpWebResponse.GetResponseStream();
    XmlTextReader的新= XmlTextReader的(流);
    xmlTextReader.Read();
    XMLDocument.load方法(XmlTextReader的);
    //文件处理code。
    // ...
}
抓住
{
    //捕获blcok错误句柄
}
最后
{
    如果(XmlTextReader的!= NULL)
        xmlTextReader.Close();
    如果(httpWebResponse!= NULL)
        httpWebResponse.Close();
    如果(流!= NULL)
        stream.Close();
}


解决方案

简单规则的拇指的是,如果它没有实现IDisposal那么它并不需要处置。

My asp.net application sending httpwebrequest to remote REST server and waiting for the response, and I found there are lots of same error message like this:

System.Net.WebException: The operation has timed-out. at System.Net.HttpWebRequest.GetResponse()

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的抓超时后底层连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆