C#Web客户端的演技减缓第一次演技、客户端、Web

2023-09-04 00:18:59 作者:用辣条欺负你

我使用的是Web客户端下载从网站的字符串(其中只包含纯文本,没有别的),所以我用了DownloadString方式:

I am using a WebClient to download a string from a website (which just contains plain text, nothing else), so I use the DownloadString method:

WebClient wc = new WebClient();
string str = wc.DownloadString("http://blah");

它工作正常,但问题在于,它第一次下载的字符串时,它需要很长的时间,比如5秒。之后,它的工作速度快。为什么会出现这种情况?如何解决这个问题?

It works fine, but the problem is that the first time it downloads the string it takes a long time, like 5 seconds. After that it works fast. Why does this happen and how can overcome this problem?

推荐答案

设置的代理属性的 Web客户端对象为null应该消除,你看到的延迟。另外,如果你已经配置你的系统使用代理服务器可以检索到 WebRequest.GetSystemWebProxy 。第二种方法应消除在这两种情况下的延迟。

Setting the Proxy property of your WebClient object to null should eliminate the delays you're seeing. Alternatively if you've configured your system to use a proxy it can be retrieved with WebRequest.GetSystemWebProxy. The second method should eliminate the delay in either case.