且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#检查远程服务器

更新时间:2023-02-02 17:28:14

您可以的

您可以下载从它的默认页面

您可以做一个HEAD要求

如果这是你的网络上的本地IIS6服务器,并且您有一些管理的细节,你可以的连接到IIS 使用一些的DirectoryEntry code

一些在136615也会有所帮助,特别是接受的答案,讨论插座

有关的打印服务器(或,具体,打印机),在code被K斯科特这里可能的帮助。它的乐趣code与反正:-)这code提到dns.resolve,这是过时,取而代之的的 Dns.GetHostEntry

我很担心的想法: - )

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 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();

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 :-)