1

I want to check website response like as website is responding properly like it giving some error like 404 or 505 or its Internet Explorer not able to find web page Please give some different or easy answer i already searched in Google..

I'am using Below solution but while using for-loop for more then one link at a time its not working can any one please help me.

    webBrowser1.Url = new Uri( "SomeRandomUrl");
    ((SHDocVw.WebBrowser)webBrowser1.ActiveXInstance).NavigateError += new
 SHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(Form1_NavigateError);

    void Form1_NavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel)
    {
        int currentStatusCode = (int)StatusCode;</code>
    }
Saghir A. Khatri
  • 3,429
  • 6
  • 45
  • 76
Ashwani
  • 81
  • 14
  • Use `async/await` if you need to handle events in the loop: http://stackoverflow.com/a/21950328/1768303 – noseratio Feb 25 '14 at 11:00

1 Answers1

0

Try this:

private void GetErrorCode(string url)
{
    try
    {
        string htmlString = new WebClient().DownloadString(url);
        Console.WriteLine(htmlString);
    }
    catch (WebException webExp)
    {
        Console.WriteLine("You got a {0} error!", (int)((HttpWebResponse) webExp.Response).StatusCode);
    }

    Console.ReadKey();
}

Call as follows:

GetErrorCode("http://www.gooogle.com/GiveMeA404ErrorPagePlease");
David
  • 4,665
  • 4
  • 34
  • 60
  • This page: http://stackoverflow.com/questions/4510212/how-i-can-get-web-pages-content-and-save-it-into-the-string-variable suggests a variation to `WebClient.DownloadString`, but I don't know if it will matter or not for what you are doing. – David Feb 25 '14 at 05:27
  • PhatWrat Thanks for reply but all answer u mentioned time consuming that's why i choose web browser control to check its little fast then others please suggest if u hav answer about web browser control. – Ashwani Feb 25 '14 at 06:09