Uri.IdnHost 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得符合 RFC 3490 規範的主機國際網域名稱,並依情況使用 Punycode。 這個字串在必要時解除逃脫後,仍可安全用於 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(範圍),則包含區域 ID(範圍)。
::1fe80::1%18 - 對於 IPv4 位址:回傳虛點小數符號(例如
192.168.1.1)。
IdnHost 是舊有 DnsSafeHost 屬性的首選替代方案,因為它的行為不依賴 app.config 設定,且設計上能產生適合 DNS 解析的值。 請注意,對於包含區域 ID 的 IPv6 結果,呼叫者可能仍需先剝除區域 ID,才能將該值傳給不接受該 ID 的 API。
如果你用跳脫字串來構造這個實例(例如), "http://[fe80::200:39ff:fe36:1a2d%254]/temp/example.htm"IdnHost 會回傳一個逃逸字串。 在使用 IdnHost 的逃脫字串做 DNS 解析前,應該先解除逃脫字串。 請注意,如果你用了一個無效的未跳脫字串來建構這個實例(例如:「http://[fe80::200:39ff:fe36:1a2d%4]/temp/example.htm」),那麼 IdnHost 會回傳一個未跳脫的字串。