Link to home
Start Free TrialLog in
Avatar of summer_soccer
summer_soccer

asked on

How to get url status from WebBrowser object in Visual C# Windows Form application?

I have some small Windows Form application that uses WebBrowser object in Visual C#. What I want to do is, given a list of urls, I want to automatically load these urls in the WebBrowser object one by one, then I want to know which urls can be visited, and which urls cannot be visited.

After I called webBrowser1.Navigate(one url), how can I know whether this url can be visited or not in the code?

Thanks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
 
namespace IEObj
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {   
 
                webBrowser1.Navigate("http://www.abcefd.com");
        }
 
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
 
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of wht1986
wht1986
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of oobayly
Second wht1986's comment.

The WebRequest is far more lightweight as it's only downloading the page, and none of the images, stylesheets etc.

I'd suggest one change though, use HEAD, rather than the default GET. This will mean you're only retrieving the headers, so it should be even quicker.

// Initialize the WebRequest.
WebRequest myRequest = WebRequest.Create("http://www.contoso.com");
myRequest.Method = "HEAD";
 
// Return the response.
WebResponse myResponse = myRequest.GetResponse();
 
// Code to use the WebResponse goes here.
 
// Close the response to free resources.
myResponse.Close();

Open in new window

Avatar of summer_soccer
summer_soccer

ASKER

Thanks. But how can know if a url can be visited or not?
Here's an post where the author polls multiple sites to see if they are up (visitable) with WebRequest/WebResponse objects

http://www.eggheadcafe.com/articles/20060120.asp
Well, if the page isn't visible, ie 404 Not Found, GetResponse() will throw an WebException, from which you can extract the actual status code returned.

This code should always give you a status code, either OK, or an error code
System.Net.WebRequest req = System.Net.WebRequest.Create("http://www.google.com/skdfjghasjklh");
req.Method = "HEAD";
 
System.Net.WebResponse resp;
System.Net.HttpStatusCode status;
try {
  // Get the Response & store the status
  resp = req.GetResponse();
  status = ((System.Net.HttpWebResponse)resp).StatusCode;
 
  // Always close
  resp.Close();
 
} catch (System.Net.WebException ex) {
  // Get the status from the error's response
  status = ((System.Net.HttpWebResponse)ex.Response).StatusCode;
 
}

Open in new window

thanks, but how about some other error, like DNS name cannot be resolved, how to handle this case? Do I need to do it in exception?
Can't remember off hand what type of exception a dns resolve error throws, but the easiest way to do it is to test conceivable errors by causing them to happen, and seeing what exception is thrown. Then just catch that type of exception.
Other possible exceptions could be an IOException, or a derivative if the network went down while the request was being made.
Summer did you get things working?