I created the first method, which gets and displays all the links in a web page in a listView when a button is pressed. I found the second method on a site that supposedly checks to see if a link is valid. I need help on incorporating it into my program. I would like that after it checks the link, that it will tell me if it is valid or not beside the link in my listView box. I'm still pretty new to C# and programming overall. I have read several things and tried changing somethings around, but no luck. Can somebody please give me some pointers on this and tell me if this will work on what I'm doing?
private void toolStripButtonLinks_Click(object sender, EventArgs e)
{
foreach (HtmlElement link in webBrowser1.Document.Links)
listViewLinks.Items.Add(link.GetAttribute("HREF").ToString());
}
private Boolean TestUrl(String url)
{
WebRequest req = null;
WebResponse resp = null;
try
{ // Create a request and get a response. If no exceptions were thrown,
// The link is good.
req = (WebRequest)WebRequest.Create(url);
resp = req.GetResponse();
return true;
}
catch (Exception e)
{
// An exception was thrown indicating a bad link. Check e for error.
return false;
}
finally
{
// Close the response object.
if (resp != null) resp.Close();
}
}