Very slow start of WebResponse TimeOut

I have a function in C # that retrieves Internet status by retrieving 64b XML from the router page

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; } 

using Mozilla Firefox 3.5 and the FireBug add-on, I guessed that it usually takes 30 ms to get the page, but on a very large 500 ms the limit that it still often reaches. How can I significantly improve performance?

Thank you in advance

+4
source share
1 answer

You do not close the web response. If you sent requests to the same server and did not close these answers, this is the problem.

Paste the response into the using statement:

 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; } 

(I made several other changes at the same time)

+9
source

All Articles