繁体   English   中英

c#如何确保Webclient留有必要的下载时间

[英]c# How Do I make Sure Webclient allows necessary time for download

using (var client = new WebClient())
                {
                     html = client.DownloadString(some_string);
                     //do something
                     html = client.DownloadString(some_string1);
                     //do something
html = client.DownloadString(some_string2);
                     //do something
html = client.DownloadString(some_string3);
                     //do something
etc
                }

webclient不允许自己有足够的时间下载网页的整个源。 我如何强迫它等待直到完成下载?

DownloadString是一个阻塞调用,因此无需执行任何操作。 它将继续下载,直到收到字符串。

问题可能在于您不应该使用DownloadString。 您要检索什么? 您最终得到的是预期字符串的一半吗?

如MSDN文章所示,您可能应该使用它:

WebClient client = new WebClient ();

// Add a user agent header in case the 
// requested URI contains a query.

client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

Stream data = client.OpenRead (some_string);
StreamReader reader = new StreamReader (data);
string s = reader.ReadToEnd ();
Console.WriteLine (s);
data.Close ();
reader.Close ();

源在这里: http : //msdn.microsoft.com/zh-cn/library/system.net.webclient(VS.80).aspx

Web客户端不允许以编程方式访问超时。 您必须改为使用HttpRequest对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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