2

I'm trying to make an app that check internet connection (and integrity of an special site) using httpwebrequest. I found this code after researching but can't complete it and don't know if it is true:

 WebRequest myRequest = WebRequest.Create("http://www.bing.com");
            myRequest.Timeout = 5000;
            WebResponse response = myRequest.GetResponse();

            if(response == ???)
            {
                response.Close();
                return true;
            }
            else{
                response.Close();
                return false;
            }

What should I add?

Mohammad H
  • 785
  • 2
  • 10
  • 25

3 Answers3

2

Take a look at documentation:

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void Main ()
        {
            // Create a request for the URL. 
            WebRequest request = WebRequest.Create ("http://www.bing.com");
            // If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.
            WebResponse response = request.GetResponse ();
            // Display the status.
            Console.WriteLine (((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream ();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader (dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd ();
            // Display the content.
            Console.WriteLine (responseFromServer);
            // Clean up the streams and the response.
            reader.Close ();
            response.Close ();
        }
    }
}

[EDITED]

Sure you can convert your responce to HttpWebResponse and ask StatusCode like this:

if ( ((HttpWebResponse)response).StatusCode == HttpStatusCode.OK )
{
    response.Close();
    return true;
}
else
{
    response.Close();
    return false;
}
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
  • I have seen this doc but dont know what to put insted of ??? in my code. take a look at 4th line in my code – Mohammad H Nov 18 '14 at 08:53
2

Try it this way:

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.bing.com");
myRequest.Timeout = 5000;
HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();

if(response.StatusCode == HttpStatusCode.OK)
{
  response.Close();
  return true;
}
else{
  response.Close();
  return false;
}
wam090
  • 2,823
  • 8
  • 33
  • 36
1

If your ISP has not blocked ping traffic, you can also use the .net Ping Class

public static bool IsConnectedToInternet()
{
    string host = "www.google.com";
    bool result = false;
    Ping p = new Ping();
    try
    {
        PingReply reply = p.Send(host, 5000);
        if (reply.Status == IPStatus.Success)
            return true;
    }
    catch { }
    return result;
}
Piyush
  • 830
  • 8
  • 19