语言

Uri.IdnHost 属性

定义

根据需要使用 Punycode 获取主机符合 RFC 3490 的国际域名。 如有必要,此字符串在未转义后可以安全地用于 DNS 解析。

public:
 property System::String ^ IdnHost { System::String ^ get(); };
public string IdnHost { get; }
member this.IdnHost : string
Public ReadOnly Property IdnHost As String

属性值

根据 IDN 标准使用 Punycode 格式化的主机名。

例外

此实例表示相对 URI,此属性仅对绝对 URI 有效。

示例

以下示例演示了 DNS、IDN、IPv4 和 IPv6 输入的方式HostIdnHost和差异:

// Demonstrate differences between Host, IdnHost, and DnsSafeHost.

// Example 1: Regular hostname (ASCII).
Console.WriteLine("Example 1: Regular ASCII hostname");
Uri uri1 = new Uri("http://www.contoso.com:8080/path");
Console.WriteLine($"  Host:        {uri1.Host}");        // www.contoso.com
Console.WriteLine($"  IdnHost:     {uri1.IdnHost}");     // www.contoso.com
Console.WriteLine($"  DnsSafeHost: {uri1.DnsSafeHost}"); // www.contoso.com
Console.WriteLine();

// Example 2: International domain name (non-ASCII).
Console.WriteLine("Example 2: International domain name");
Uri uri2 = new Uri("http://münchen.de/path");
Console.WriteLine($"  Host:        {uri2.Host}");        // münchen.de (original)
Console.WriteLine($"  IdnHost:     {uri2.IdnHost}");     // xn--mnchen-3ya.de (punycode)
Console.WriteLine($"  DnsSafeHost: {uri2.DnsSafeHost}"); // münchen.de or xn--mnchen-3ya.de, depending on configuration.
Console.WriteLine();

// Example 3: International domain name already in punycode (encoded) form.
Console.WriteLine("Example 3: Already-encoded international domain name");
Uri uri2Encoded = new Uri("http://xn--mnchen-3ya.de/path");
Console.WriteLine($"  Host:        {uri2Encoded.Host}");        // xn--mnchen-3ya.de (as provided)
Console.WriteLine($"  IdnHost:     {uri2Encoded.IdnHost}");     // xn--mnchen-3ya.de (already punycode)
Console.WriteLine($"  DnsSafeHost: {uri2Encoded.DnsSafeHost}"); // xn--mnchen-3ya.de
Console.WriteLine();

// Example 4: IPv6 address without zone ID.
Console.WriteLine("Example 4: IPv6 address without zone ID");
Uri uri3 = new Uri("http://[::1]:8080/path");
Console.WriteLine($"  Host:        {uri3.Host}");        // [::1] (with brackets)
Console.WriteLine($"  IdnHost:     {uri3.IdnHost}");     // ::1 (without brackets)
Console.WriteLine($"  DnsSafeHost: {uri3.DnsSafeHost}"); // ::1 (without brackets)
Console.WriteLine();

// Example 5: IPv6 link-local address with zone ID.
Console.WriteLine("Example 5: IPv6 link-local address with zone ID");
Uri uri4 = new Uri("http://[fe80::1%10]:8080/path");
Console.WriteLine($"  Host:        {uri4.Host}");        // [fe80::1] (with brackets, no zone ID)
Console.WriteLine($"  IdnHost:     {uri4.IdnHost}");     // fe80::1%10 (without brackets, with zone ID)
Console.WriteLine($"  DnsSafeHost: {uri4.DnsSafeHost}"); // fe80::1%10 (without brackets, with zone ID)
Console.WriteLine();

// Example 6: IPv4 address.
Console.WriteLine("Example 6: IPv4 address");
Uri uri5 = new Uri("http://192.168.1.1:8080/path");
Console.WriteLine($"  Host:        {uri5.Host}");        // 192.168.1.1
Console.WriteLine($"  IdnHost:     {uri5.IdnHost}");     // 192.168.1.1
Console.WriteLine($"  DnsSafeHost: {uri5.DnsSafeHost}"); // 192.168.1.1

注解

为使用需要 Punycode 格式的域名的较低级别网络协议提供此属性。 如果代码不需要该特定格式,请使用 Host 主机名。

此属性返回的值取决于主机类型:

  • 对于 DNS 主机名:对于有效的国际域名,非 ASCII 字符是经过 punycode 编码的(例如, münchen.de 变为 xn--mnchen-3ya.de)。 无效的 IDN 的主机名可能会返回 as-is 并包含非 ASCII 字符。
  • 对于 IPv6 地址:返回没有周围括号的地址,如果指定了区域 ID(例如, ::1 或 fe80::1%18)。
  • 对于 IPv4 地址:返回点符号十进制表示法(例如, 192.168.1.1)。

IdnHost 是旧 DnsSafeHost 属性的首选替代方法,因为它的行为不依赖于 app.config 设置,并且旨在生成适合 DNS 解析的值。 请注意,对于包含区域 ID 的 IPv6 结果,调用方在将值传递给不接受它的 API 之前,仍可能需要去除区域 ID。

如果使用转义字符串构造此实例(例如), "http://[fe80::200:39ff:fe36:1a2d%254]/temp/example.htm"则 IdnHost 返回转义字符串。 在使用该字符串进行 DNS 解析之前,应取消从 IdnHost 返回的任何转义字符串。 请注意,如果使用无效的未转义字符串构造此实例(例如“http://[fe80::200:39ff:fe36:1a2d%4]/temp/example.htm”),则 IdnHost 将返回未转义字符串。

适用于