C# 中的 Uri.CheckHostName(String) 方法

csharpserver side programmingprogramming更新于 2025/9/23 19:52:17

C# 中的 Uri.CheckHostName() 方法用于判断指定的主机名是否为有效的 DNS 名称。

语法

以下是语法 −

public static UriHostNameType CheckHostName (string host_name);

示例

现在让我们看一个实现 Uri.CheckHostName() 方法的示例 −

using System;
public class Demo {
   public static void Main(){
      string strURI = "http://localhost";
      Console.WriteLine("URI = "+strURI);
      UriHostNameType res = Uri.CheckHostName(strURI);
      Console.WriteLine("Host type = "+res);
   }
}

输出

这将产生以下输出 −

URI = http://localhost
Host type = Unknown

示例

现在让我们看另一个实现 Uri.CheckHostName() 方法的示例 −

using System;
public class Demo {
   public static void Main(){
      string strURI = "www.tutorialspoint.com";
      Console.WriteLine("URI = "+strURI);
      UriHostNameType res = Uri.CheckHostName(strURI);
      Console.WriteLine("Host type = "+res);
   }
}

输出

这将产生以下输出 −

URI = www.tutorialspoint.com
Host type = Dns

相关文章