开发者

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 does not allow itself enough time to download开发者_如何学C the entire source of the webpage. how do i force it to wait until it finishes the donwload?


DownloadString is a blocking call, so there is no need to do anything. It will continue to download until the string is received.

The problem probably lies in the fact that you shouldn't be using DownloadString. What are you trying to retrieve? Do you end up with half of your expected string?

You should probably use it as the MSDN article shows:

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 ();

The source is here: http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.80).aspx


The web client doesn't allow programmatic access to Timeout. You'll have to use a HttpRequest object instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜