C# 检查远程服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1043342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Check Remote Server
提问by Kane
Can anyone advise what the best way to check (using .NET 3.5) if a remote server is available?
如果远程服务器可用,任何人都可以建议检查(使用.NET 3.5)的最佳方法是什么?
I was thinking of using the following code but would like to know if a better way exists if the community has another option.
我正在考虑使用以下代码,但想知道如果社区有其他选择是否存在更好的方法。
TcpClient client = new TcpClient("MyServer", 80);
if (!client.Connected)
{
throw new Exception("Unable to connect to MyServer on Port 80");
}
client.Close();
采纳答案by Dan F
You could pingit
你可以ping通它
You could download the default pagefrom it
您可以从中下载默认页面
You could do a HEADrequest
你可以做一个HEAD请求
If it's a local IIS6 server on your network, and you have some admin details, you could connect to IISusing some DirectoryEntry code
如果它是您网络上的本地 IIS6 服务器,并且您有一些管理详细信息,则可以使用一些 DirectoryEntry 代码连接到 IIS
Some of the answers on 136615might help too, specifically the accepted answer that talks about sockets
136615上的一些答案可能也有帮助,特别是关于套接字的公认答案
For the print servers (or, specifically, the printers), the code by K Scott heremight help. It's fun code to play with anyway :-) That code mentions dns.resolve, which is obsoleted and replaced by Dns.GetHostEntry
对于打印服务器(或,具体,打印机),用K斯科特的代码在这里可能会有帮助。无论如何,这是一段有趣的代码:-) 这段代码提到了 dns.resolve,它已经过时并被Dns.GetHostEntry取代
I'm about out of ideas :-)
我没有想法了:-)
回答by Noldorin
If you just want to see whether a given server is online, then a simple ping should do the job in most cases.
如果您只想查看给定的服务器是否在线,那么在大多数情况下,一个简单的 ping 就可以完成这项工作。
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.
使用这种方法,您也不会以任何方式滥用 HTTP 服务器。
Otherwise (if you want to check whether a connection is possible on a specific port), that basically looks fine.
否则(如果您想检查特定端口上是否可以连接),那基本上看起来不错。
回答by Kev
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.
我猜你想检查一个网站是否可用。您可以只使用 aSystem.Net.WebRequest
并检查结果。
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, HostMonitoror IPSentryinstead of rolling your own.
更新:根据您的评论,如果您有几台服务器(和服务)需要监控,那么使用Nagios、HostMonitor或IPSentry 之类的软件包而不是自己动手可能是一个更好的主意。
回答by LucasS
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.
抱歉,它是 VB,但这就是我面前的东西。我将把它留作练习,让读者根据需要将其转换为 C#。
回答by Marcelo Camargo
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:
如果您使用的是以前版本的 .NET Framework,就像我一样,版本 2,您将没有 Ping 和System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()
. 然后,您可以使用HttpWebRequest
检查主机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;
}
}