簡體   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