Webclient downloaddata()下载速度非常慢 [英] Webclient downloaddata() downloads very slowly
问题描述
WebClient web = new WebClient();
var byt = web.downloaddat(path)
This code download data very slowly even application got stuck or hang.`enter code here.
我尝试过:
我设置proxy = null;
,但第一次不起作用,第二次有效。
What I have tried:
I set proxy = null ;
but for first time it not work and second time it works.
推荐答案
在这里,尝试使用
Here, try using
WebProxy proxy = WebProxy.GetDefaultProxy()
client.Proxy = proxy;
或者你第一次可以覆盖超时功能并为webclient设置超时,在超时时再次调用你的功能下载数据这将解决你第一次无法工作的问题。
Or for the first time you can override the timeout function and set timeout for webclient, on timeout call your function again to download data this will solve your issue where it is not working for the first time.
public class WebClientWithTimeout:WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest wr = base.GetWebRequest(address);
wr.Timeout = 5000; // timeout in milliseconds (ms)
return wr;
}
}
可以查看此链接了解更多详情: C#使用WebClient将URL下载到超时的字符串或文件 [ ^ ]
下载速度是数据量,连接速度以及本地和远程主机速度的函数。您可以尝试异步下载,请参阅 WebClient.DownloadData方法(字符串)(系统。网络) [ ^ ]。
Download speed is a function of the amount of data, the speed of your connection, and the speed of the local and remot host. you could try asynchronous downloading, see WebClient.DownloadData Method (String) (System.Net)[^].
如果您的应用程序挂起,您可能正在从主(GUI)线程调用DownloadData
:
If your application hangs you are probably callingDownloadData
from your main (GUI) thread:
此方法在下载资源时阻止。要下载资源并在等待服务器响应时继续执行,请使用 DownloadDataAsync [ ^ ]方法。
This method blocks while downloading the resource. To download a resource and continue executing while waiting for the server's response, use one of the DownloadDataAsync[^] methods.
这篇关于Webclient downloaddata()下载速度非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!