gauntalus

I'm trying to write an app that uses WebRequest to fetch information from a web service, but the first request that I make is very very slow. Simply fetching the first site usually takes rougly ~25 seconds. Most requests after the first one take < 1 second. Any idea why this might be happening Here's a sample snippet and its output.

Code Snippet

using System;

using System.Net;

using System.IO;

using System.Diagnostics;

namespace WebTest

{

class Program

{

static void Main(string[] args)

{

DateTime start = DateTime.Now;

WebRequest request = WebRequest.Create("http://www.microsoft.com");

HttpWebResponse response = (HttpWebResponse) request.GetResponse();

StreamReader reader = new StreamReader(response.GetResponseStream());

Debug.WriteLine(reader.ReadLine());

Debug.WriteLine(DateTime.Now - start);

}

}

}

Running this yields the following output for me.

<html>

00:00:24.3792990

The same request in IE takes about 2 seconds to load. Changing the URL doesn't help.



Re: .NET Framework Networking and Communication First call to read WebRequest.Create() response VERY slow, subsequent calls fast

Jörg Jooss - MSFT

You could hook up Fiddler and see where all that time is being spent.

Are you using a proxied connection





Re: .NET Framework Networking and Communication First call to read WebRequest.Create() response VERY slow, subsequent calls fast

SVetlana Garber

I am experiencing the same problem...





Re: .NET Framework Networking and Communication First call to read WebRequest.Create() response VERY slow, subsequent calls fast

gauntalus

Hi Jorg,

I hooked up Fiddler, but I don't know what else its going to be able to tell me. Basically it just says that the request took 24s... Here are the "Performance Statistics":

Code Snippet

Request Count: 1
Bytes Sent: 67
Bytes Received: 9,917

ACTUAL PERFORMANCE
--------------
Requests started at: 19:51:24:2133
Responses completed at: 19:51:48:7010
Total Sequence time: 00:00:24.4876905

RESPONSE CODES
--------------
HTTP/200: 1

RESPONSE BYTES (by Content-Type)
--------------
text/html: 9,461
~headers: 456

ESTIMATED WORLDWIDE PERFORMANCE
--------------
The following are VERY rough estimates of download times when hitting servers based in WA, USA.


US West Coast (Modem - 6KB/sec)
---------------
Round trip cost: 0.10s
Elapsed Time: 1.10s


Japan / Northern Europe (Modem)
---------------
Round trip cost: 0.15s
Elapsed Time: 1.15s


China (Modem)
---------------
Round trip cost: 0.45s
Elapsed Time: 1.45s


US West Coast (DSL - 30KB/sec)
---------------
Round trip cost: 0.10s
Elapsed Time: 0.10s


Japan / Northern Europe (DSL)
---------------
Round trip cost: 0.15s
Elapsed Time: 0.15s


China (DSL)
---------------
Round trip cost: 0.45s
Elapsed Time: 0.45s


================
Learn more about HTTP performance at http://www.fiddlertool.com/redir/ id=httpperf

I am not using a proxied connection. And like I said, the same request in IE takes about 2 seconds.

Where can I go from here





Re: .NET Framework Networking and Communication First call to read WebRequest.Create() response VERY slow, subsequent calls fast

Jörg Jooss - MSFT

Some questions:

  • There is really no downstream proxy "Automatically Detect Settings" in Internet Options is not checked
  • Are subsequent requests after the first coming from the same application, or do you simply launch your app again with another URL parameter

BTW, you can enable System.Net tracing by adding this to your App.config:

<configuration>

<system.diagnostics>

<trace autoflush="true" />

<switches>

<add name="System.Net" value="Verbose"/>

</switches>

</system.diagnostics>

</configuration>

This will dump all the informaton to any Debug console (e.g. Visual Studio's Output "window"). With a 20+ second lag, you should be able to actually see at what point the processing lags.





Re: .NET Framework Networking and Communication First call to read WebRequest.Create() response VERY slow, subsequent calls fast

DanT2007

All:

We are seeing the same exact thing here on our application. It takes 25 seconds for queries to be returned. These queries normally take .0001 seconds to return. This slows our app down to a screaching halt for the duration of the queries. It is the same queries, usually in sets of 3, or 5 each taking ~ 25 seconds.

Auto close is turned off in case anyone thinks this will correct it.

Has anyone isolated this cause yet






Re: .NET Framework Networking and Communication First call to read WebRequest.Create() response VERY slow, subsequent calls fast

Jörg Jooss - MSFT

I'm not sure what you mean by "Auto Close".

Same question here: Can you see at what point your applications lags when enabling tracing

(As a quick remedy, you may want to make this an async operation if that makes sense from a UX perspective in order to avoid a blocking operation of 25 seconds.)