This simple code takes the lines from a multiline textbox, downloads each url page as a string, and another function is called that parses information from that string. client.DownloadString(url) just hangs on the download attempt of the second url. I can't get any feedback on why. One time it actually went through them all. I shouldn't need to use the async version of this method. Why dos it work on the first url but not the second? It doesn't matter what the urls are, it almost always hangs on the second url.
string[] lines = tbUrls.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < lines.Count(); i++)
{
try
{
WebClient client = new WebClient();
string url = lines[i];
string downloadString = client.DownloadString(url);
findNewListings(downloadString, url);
}
catch (Exception exce)
{
MessageBox.Show("Error downlaoding page: \n\n" + exce.Message);
}
}
I am editing your code, try this:
WebClient client = new WebClient();
string url = lines[i];
try
{
string downloadString = client.DownloadString(url);
client.Dispose(); //dispose the object because you don't need it anynmore
findNewListings(downloadString, url);
}
catch (Exception exce)
{
MessageBox.Show("Error downlaoding page: \n\n" + exce.Message);
}
If an object is no longer in use, its better to Dispose
it.