【问题标题】:WebClient.DownloadString takes about 15 seconds when first called首次调用 WebClient.DownloadString 大约需要 15 秒
【发布时间】:2012-05-13 08:16:01
【问题描述】:
string url = "http://google.com/index.html";
WebClient client = new WebClient();
Stopwatch sw = new Stopwatch();
sw.Start();
string text = client.DownloadString(url);
sw.Stop();
Console.WriteLine(sw.Elapsed);

Stopwatch 表示DownloadString 方法在第一次调用时需要 13-15 秒,但重复调用需要相当长的时间。这是怎么发生的,我该如何解决?

【问题讨论】:

标签: c# webclient


【解决方案1】:

可能有几件事会导致第一次通话出现延迟,例如检测代理设置。尝试将代理设置为空:

client.Proxy = null;

【讨论】:

  • @user1306322 请务必阅读其他答案;延迟是有原因的。如果您将其设置为 null,您可能会使某些用户无法使用您的程序。当然,除非这只是一个内部工具。
【解决方案2】:

您的机器已配置为执行自动代理检测。

您可以在此处禁用它:

或者,您可以手动覆盖 WebClient 使用的代理; null 表示没有代理:

client.Proxy = null;

但是,在这种情况下,您应该让用户在您的应用程序中配置代理,因为某些用户在访问 Web 时必须使用代理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 2017-01-22
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多