[ACCEPTED]-C# Check Remote Server-.net

Accepted answer
Score: 26

You could ping it

You could download the default page from it

You could 10 do a HEAD request

If it's a local IIS6 server 9 on your network, and you have some admin 8 details, you could connect to IIS using some DirectoryEntry 7 code

Some of the answers on 136615 might help too, specifically 6 the accepted answer that talks about sockets

For 5 the print servers (or, specifically, the 4 printers), the code by K Scott here might help. It's 3 fun code to play with anyway :-) That code 2 mentions dns.resolve, which is obsoleted 1 and replaced by Dns.GetHostEntry

I'm about out of ideas :-)

Score: 8

If you just want to see whether a given 6 server is online, then a simple ping should 5 do the job in most cases.

PingReply pingReply;
using (var ping = new Ping())
    pingReply = ping.Send("http://www.stackoverflow.com/");
var available = pingReply.Status == IPStatus.Success;

Using this method 4 you're not abusing the HTTP server in any 3 way, too.

Otherwise (if you want to check 2 whether a connection is possible on a specific 1 port), that basically looks fine.

Score: 4

just to add to Dan's answer... I just had 6 to implement this and here is a nice little 5 code snippet that should help out those 4 that make it here from Google.

Imports System.Net
Private Function URLExists(pURL As String) As Boolean

    Try
        'Creating the HttpWebRequest
        Dim request As HttpWebRequest = TryCast(WebRequest.Create(pURL), HttpWebRequest)
        'Setting the Request method HEAD, you can also use GET too.
        request.Method = "HEAD"
        'Getting the Web Response.
        Dim response As HttpWebResponse = TryCast(request.GetResponse(), HttpWebResponse)
        'Returns TURE if the Status code == 200
        Return (response.StatusCode = HttpStatusCode.OK)
    Catch
        'Any exception will returns false.
        Return False
    End Try
End Function

sorry it is 3 VB but that is what I had in front of me. I 2 will leave it as an exercise for the reader 1 to convert this to C# as needed.

Score: 2

If you be using a previous version of .NET 3 Framework, like me, the version 2, you'll 2 have no Ping and no System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable(). Then, you can use 1 HttpWebRequest to check a host disponibility:

  public static bool AccessToWebService()
  {
      string host = "http://192.168.99.41";
      try
      {
          HttpWebRequest request = (HttpWebRequest) WebRequest.Create(host);
          request.Method = "HEAD";
          HttpWebResponse response = (HttpWebResponse) request.GetResponse();
          return response.StatusCode == HttpStatusCode.OK;
      }
      catch (Exception)
      {
          return false;
      }
  }
Score: 1

I'm guessing you want to check to see if 6 a website is available. You could just use 5 a System.Net.WebRequest and check the result.

Update: Based on your 4 comment, if you've got a few servers (and 3 services) to monitor, then maybe it'd be 2 a better idea to use a package such as Nagios, HostMonitor or 1 IPSentry instead of rolling your own.

More Related questions