C# WebClient 第一次运行缓慢 [英] C# WebClient acting slow the first time

查看:65
本文介绍了C# WebClient 第一次运行缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 WebClient 从网站下载字符串(只包含纯文本,没有其他内容),所以我使用 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?

推荐答案

WebClient 对象的 Proxy 属性设置为 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.

这篇关于C# WebClient 第一次运行缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆