Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 非常慢的WebResponse触发超时_C#_Xml_Httpwebrequest_Httpwebresponse - Fatal编程技术网

C# 非常慢的WebResponse触发超时

C# 非常慢的WebResponse触发超时,c#,xml,httpwebrequest,httpwebresponse,C#,Xml,Httpwebrequest,Httpwebresponse,我在C#中有一个函数,它通过从路由器页面检索64b XML来获取Internet的状态 public bool isOn() { HttpWebRequest hwebRequest = (HttpWebRequest)WebRequest.Create("http://" + this.routerIp + "/top_conn.xml"); hwebRequest.Timeout = 500; HttpW

我在C#中有一个函数,它通过从路由器页面检索64b XML来获取Internet的状态

public bool isOn()
        {
            HttpWebRequest hwebRequest = (HttpWebRequest)WebRequest.Create("http://" + this.routerIp + "/top_conn.xml");
            hwebRequest.Timeout = 500;
            HttpWebResponse hWebResponse = (HttpWebResponse)hwebRequest.GetResponse();
            XmlTextReader oXmlReader = new XmlTextReader(hWebResponse.GetResponseStream());       
            string value;
            while (oXmlReader.Read())
            {
                value = oXmlReader.Value;
                if (value.Trim() != ""){
                    return !value.Substring(value.IndexOf("=") + 1, 1).Equals("0");
                }
            }
            return false;

        }
使用Mozilla Firefox 3.5和FireBug插件,我猜通常需要30毫秒来检索页面,但是在非常大的500毫秒限制下,它仍然经常到达页面。如何显著提高性能


提前感谢

您没有关闭web响应。如果您向同一服务器发出了请求,但没有关闭这些响应,这就是问题所在

使用语句将响应粘贴在
中:

public bool IsOn()
{
    HttpWebRequest request = (HttpWebRequest) WebRequest.Create
        ("http://" + this.routerIp + "/top_conn.xml");
    request.Timeout = 500;
    using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
    using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
    {      
        while (reader.Read())
        {
            string value = reader.Value;
            if (value.Trim() != "")
            {
                return value.Substring(value.IndexOf("=") + 1, 1) != "0";
            }
        }
    }
    return false;    
}
(我同时做了一些其他的改动…)