C# Check Remote Server

后端 未结 5 1856
没有蜡笔的小新
没有蜡笔的小新 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 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;
          }
      }
    

提交回复
热议问题