简体   繁体   中英

WebClient query takes too long to complete, why?

This is my C# code.

WebClient client = new WebClient();

while (true) {
   html = client.DownloadString("http://google.com");
   Console.WriteLine(string.Format("\tSize: {0}", html.Length));
}

It takes around 9 seconds to get the first result. Then it takes around 3 seconds each.

When I do it with Java, it takes less than 1 second.

Why do you think it's so slow with C#? How can I improve it?

I noticed that the first Request always takes long using WebClient or WebRequest... I do the same request with a Socket-Http class I build and it takes no time.

But after the 1st request it should go faster, good.

Found a fix for the problem

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.

Set request.Proxy = null;

You may have a problem with your network (as Mark comments).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM