简体   繁体   中英

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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