C# Check Remote Server

后端 未结 5 1844
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 06:18

Can anyone advise what the best way to check (using .NET 3.5) if a remote server is available?

I was thinking of using the following code but would like to know if

相关标签:
5条回答
  • 2020-12-15 06:57

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

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

    0 讨论(0)
  • 2020-12-15 07:01

    You could ping it

    You could download the default page from it

    You could do a HEAD request

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

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

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

    I'm about out of ideas :-)

    0 讨论(0)
  • 2020-12-15 07:01

    If you just want to see whether a given server is online, then a simple ping should 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 you're not abusing the HTTP server in any way, too.

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

    0 讨论(0)
  • 2020-12-15 07:03

    If you be using a previous version of .NET Framework, like me, the version 2, you'll have no Ping and no System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable(). Then, you can use 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;
          }
      }
    
    0 讨论(0)
  • 2020-12-15 07:17

    just to add to Dan's answer... I just had to implement this and here is a nice little code snippet that should help out those 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 VB but that is what I had in front of me. I will leave it as an exercise for the reader to convert this to C# as needed.

    0 讨论(0)
提交回复
热议问题