I'm making a program that connects to a website and downloads XML from it. It then displays the information to the user.
The problem I am having is when I first open the program and start downloading the XML information it takes a really long time. When I load another page from the site with the program still open, it takes about half a second to download. I was wondering if there was any way to avoid this.
I currently use an HttpWebRequest to download the stream and a StreamReader to read it. Then I go through and parse the XML using XLINQ.
Try explicitly setting the proxy. If you don't have a proxy defined, the HttpRequest
class will spend time searching for one. Once it has (or hasn't) found one, it will use that information for the life of the application, speeding up subsequent requests.
//internally sets "ProxySet" to true, so won't search for a proxy
request.Proxy = null;
You can also define this in the .config:
<system.net>
<defaultProxy
enabled="false"
useDefaultCredentials="false" >
<proxy/>
<bypasslist/>
<module/>
</defaultProxy>
</system.net>
The first time delay can be due to a combination of the following:
To figure out which part is taking time, insert some time logging into your code using System.Diagnostics.Stopwatch():
// this is the time to get the XML doc from the server, including the time to resolve DNS, get proxy etc.
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
timer.Start();
HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
timer.Stop();
Console.WriteLine("XML download took: " + timer.ElapsedMilliseconds);
timer.Start();
// now, do your XLinq stuff here...
timer.Stop();
Console.WriteLine("XLinq took: " + timer.ElapsedMilliseconds);
You can insert a loop around this, and see what the difference for the various components between the first request and subsequent requests is.
If you find that the difference is in the downloading, and not the querying, then you can investigate further by getting a network sniff using Wireshark.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With