• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1398
  • Last Modified:

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

0
summer_soccer
Asked:
summer_soccer
  • 3
  • 3
  • 2
1 Solution
 
wht1986Commented:
I would think you would be better served by using the WebRequest / WebResponse classes

// Initialize the WebRequest.
WebRequest myRequest = WebRequest.Create("http://www.contoso.com");

// Return the response.
WebResponse myResponse = myRequest.GetResponse();

// Code to use the WebResponse goes here.

// Close the response to free resources.
myResponse.Close();

You can evaluate if it times out, returns invalid html, etc

see MS links
http://msdn.microsoft.com/en-us/library/system.net.webresponse.aspx
0
 
oobaylyCommented:
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

0
 
summer_soccerAuthor Commented:
Thanks. But how can know if a url can be visited or not?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
wht1986Commented:
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
0
 
oobaylyCommented:
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

0
 
summer_soccerAuthor Commented:
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?
0
 
oobaylyCommented:
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.
0
 
wht1986Commented:
Summer did you get things working?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now