A long time to load the first connection in C # .NET.

I am creating a program that connects to a website and loads the XML from it. Then it displays the information to the user.

The problem I encountered is when I first open the program and start loading XML information, which takes a lot of time. When I load another page from the site with the program open, it takes about half a second to load. I was wondering if there is a way to avoid this.

I am currently using HttpWebRequest to load a stream and StreamReader to read it. Then I look through and parse the XML using XLINQ.

+6
c # streamreader
source share
3 answers

Try setting the proxy server explicitly. If you do not have a proxy server, the HttpRequest will spend time looking for one. After he (or did not find) found it, he will use this 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 .config:

 <system.net> <defaultProxy enabled="false" useDefaultCredentials="false" > <proxy/> <bypasslist/> <module/> </defaultProxy> </system.net> 
+14
source share

The first delay can be caused by a combination of the following:

  • Time to resolve DNS server records
  • Proxy server autoconfig script load time, compile and execute it to determine effective proxy
  • Network timeout from your application to the proxy server (if your environment has a proxy server)
  • network timeout from the proxy server to the actual target server.
  • Server latency for serving XML document. If it needs to go through the representation of an object in memory and generate an XML document, which may take some time. In addition, if it is using methods such as XML serialization to generate the document, then depending on how the serializer is configured, the first to call serialization / deserialization always takes a lot of time, due to the fact that an intermediate assembly is required to create and compile.
  • Parsing XML on the client side may take time, esp, if the XML structure of the document is very complex.
  • If XLinq (such as XMLSerializer) generates a temporary assembly for XML parsing and querying, then the first query will take longer than the subsequent ones.

To figure out which part takes time, paste some time 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 is for the different components between the first query and subsequent queries.

If you find that the difference is in the download, and not in the request, then you can continue the study by receiving a network sniff using Wireshark .

Hope this helps.

+1
source share

You will probably have to do some more research to figure out which part of the request takes longer on the first pass. My first instinct says that it takes longer to query the DNS to obtain the IP address for the domain name you specified, because it does not cache on first run. It could also be a web server at the other end, which should run multiple startup scripts on the first request. You mentioned that the first request takes a lot of time, but you do not say how long. Is this causing a big problem, so it takes so long to complete the first request, or is it just annoying?

0
source share

All Articles