问题
this is my implementation of Webclient, supposedly, this download should be continuous, but for some reason, which debug don't even help, i got 1 success in the first run, then the rest are failed. Does anyone know why ?
for (int i = 1; i <= Count; i++)
{
using (WebClient wc = new WebClient())
{
wc.Headers["Accept-Encoding"] = "gzip";
wc.Headers["User-Agent"] = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
byte[] arr = wc.DownloadData(url);
if (arr.Length > 0)
Console.WriteLine(i.ToString() + ": SUCCESS");
else
Console.WriteLine(i.ToString() + ": FAILED");
}
}
回答1:
when i messed up with this code, it work, LOL! I dont' know what to say anymore...
using (WebClient client = new WebClient())
{
//manipulate request headers (optional)
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
//execute request and read response as string to console
using (StreamReader reader = new StreamReader(client.OpenRead(url)))
{
string s = reader.ReadToEnd();
//Console.WriteLine(s);
Console.WriteLine("Vote " + i.ToString() + ": SUCCESS");
i++;
}
}
// *** Establish the request
HttpWebRequest loHttp =
(HttpWebRequest)WebRequest.Create(url);
// *** Set properties
loHttp.Timeout = 10000; // 10 secs
loHttp.UserAgent = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
loHttp.Headers["Accept-Encoding"] = "gzip";
// *** Retrieve request info headers
HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();
Encoding enc = Encoding.GetEncoding(1252); // Windows default Code Page
StreamReader loResponseStream =
new StreamReader(loWebResponse.GetResponseStream(), enc);
string lcHtml = loResponseStream.ReadToEnd();
loWebResponse.Close();
loResponseStream.Close();
if (lcHtml.Length > 0)
{
Console.WriteLine("Vote " + i.ToString() + ": SUCCESS");
i++;
}
else
Console.WriteLine("Vote " + i.ToString() + ": FAILED");
}
Marked as community wiki, so that if anyone know why, please edit... :(
回答2:
Use a debugging proxy like Fiddler, and you'll be able to see the HTTP transactions.
来源:https://stackoverflow.com/questions/2123096/c-sharp-webclient-problem-with-looping-download