語言

Uri.Host 屬性

定義

取得這個實例的主機元件。

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

屬性值

主機名稱。 這通常是伺服器的 DNS 主機名稱或 IP 位址。

例外狀況

此實例代表一個相對 URI,且此性質僅適用於絕對 URI。

範例

以下範例將伺服器的主機名稱www.contoso.com()寫入主控台。

Uri baseUri = new Uri("http://www.contoso.com:8080/");
Uri myUri = new Uri(baseUri, "shownew.htm?date=today");

Console.WriteLine(myUri.Host);
let baseUri = Uri "http://www.contoso.com:8080/"
let myUri = Uri(baseUri, "shownew.htm?date=today")

printfn $"{myUri.Host}"
Dim baseUri As New Uri("http://www.contoso.com:8080/")
Dim myUri As New Uri(baseUri,"shownew.htm?date=today")
       
Console.WriteLine(myUri.Host)

以下範例展示了 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

備註

與該 Authority 屬性不同,此屬性值不包含埠號。

此屬性回傳的值取決於主機類型:

  • 對於 DNS 主機名稱 (包括國際網域名稱):回傳逃逸輸入。 非 ASCII 字元會回傳 as-is,且不會被編碼為 punycode。
  • 對於 IPv6 位址:回傳以方括號包圍的位址(例如 [::1]),但即使原始 URI 中指定區域 ID(範圍),也不會包含。
  • 對於 IPv4 位址:回傳虛點小數符號(例如 192.168.1.1)。

對於 DNS 解析或其他較低階的網路情境,請使用 IdnHost 代替,該格式會回傳一個以 punycode 編碼的形式表示有效的國際網域名稱,並包含 IPv6 位址的區域 ID(無括號)。 當你希望主機呈現在 URI 中的樣貌(例如用於顯示或比較)時使用 Host 。

適用於