3

In C# console application , i need to capture the out "Internet cannot display the webpage", then i can put my check. If internet browser opens the url means internet connection is there, else connection down. I am using below code

    WebClient client = new WebClient();
        string value = client.DownloadString("http://foo.com");
        Console.WriteLine(value.Length);
        Console.WriteLine(value);
        Console.WriteLine("Press any key to continue");

        Console.ReadKey(true);

Please se the method to capture the output

Sohail
  • 780
  • 3
  • 14
  • 27

2 Answers2

2

You have to get the status code from web client.

Based on the status code you can identify the state of the current connection.

Please refer the following code.

How to get status code from webclient?

Community
  • 1
  • 1
Jomy John
  • 6,308
  • 4
  • 28
  • 32
0
WebClient client = new WebClient();
string value;

try
{
    value = client.DownloadString("http://foo.com");
}
catch (WebException ex)
{
    value = ex.Message;
}

Console.WriteLine(value.Length);
Console.WriteLine(value);
Console.WriteLine("Press any key to continue");
Console.ReadKey(true);
Paul Fleming
  • 24,238
  • 8
  • 76
  • 113
  • Note that you can get more information about the failed request from the exception. See [documentation](http://msdn.microsoft.com/en-us/library/system.net.webexception.aspx) for more details. – Paul Fleming Oct 07 '12 at 12:41
  • I am trying to calculate the length , so length for Internet explorer cannot diplay the page is always 66 so i will using check for this. thank you – Sohail Oct 07 '12 at 13:10