|
| 1 | +using System; |
| 2 | + |
| 3 | +public class UriHostComparison |
| 4 | +{ |
| 5 | + public static void Main() |
| 6 | + { |
| 7 | + // <SnippetHostComparison> |
| 8 | + // Demonstrate differences between Host, IdnHost, and DnsSafeHost. |
| 9 | + |
| 10 | + // Example 1: Regular hostname (ASCII). |
| 11 | + Console.WriteLine("Example 1: Regular ASCII hostname"); |
| 12 | + Uri uri1 = new Uri("http://www.contoso.com:8080/path"); |
| 13 | + Console.WriteLine($" Host: {uri1.Host}"); // www.contoso.com |
| 14 | + Console.WriteLine($" IdnHost: {uri1.IdnHost}"); // www.contoso.com |
| 15 | + Console.WriteLine($" DnsSafeHost: {uri1.DnsSafeHost}"); // www.contoso.com |
| 16 | + Console.WriteLine(); |
| 17 | + |
| 18 | + // Example 2: International domain name (non-ASCII). |
| 19 | + Console.WriteLine("Example 2: International domain name"); |
| 20 | + Uri uri2 = new Uri("http://münchen.de/path"); |
| 21 | + Console.WriteLine($" Host: {uri2.Host}"); // münchen.de (original) |
| 22 | + Console.WriteLine($" IdnHost: {uri2.IdnHost}"); // xn--mnchen-3ya.de (punycode) |
| 23 | + Console.WriteLine($" DnsSafeHost: {uri2.DnsSafeHost}"); // depends on configuration |
| 24 | + Console.WriteLine(); |
| 25 | + |
| 26 | + // Example 3: IPv6 address without zone ID. |
| 27 | + Console.WriteLine("Example 3: IPv6 address without zone ID"); |
| 28 | + Uri uri3 = new Uri("http://[::1]:8080/path"); |
| 29 | + Console.WriteLine($" Host: {uri3.Host}"); // [::1] (with brackets) |
| 30 | + Console.WriteLine($" IdnHost: {uri3.IdnHost}"); // ::1 (without brackets) |
| 31 | + Console.WriteLine($" DnsSafeHost: {uri3.DnsSafeHost}"); // ::1 (without brackets) |
| 32 | + Console.WriteLine(); |
| 33 | + |
| 34 | + // Example 4: IPv6 link-local address with zone ID. |
| 35 | + Console.WriteLine("Example 4: IPv6 link-local address with zone ID"); |
| 36 | + Uri uri4 = new Uri("http://[fe80::1%10]:8080/path"); |
| 37 | + Console.WriteLine($" Host: {uri4.Host}"); // [fe80::1] (with brackets, no zone ID) |
| 38 | + Console.WriteLine($" IdnHost: {uri4.IdnHost}"); // fe80::1%10 (without brackets, with zone ID) |
| 39 | + Console.WriteLine($" DnsSafeHost: {uri4.DnsSafeHost}"); // fe80::1%10 (without brackets, with zone ID) |
| 40 | + Console.WriteLine(); |
| 41 | + |
| 42 | + // Example 5: IPv4 address. |
| 43 | + Console.WriteLine("Example 5: IPv4 address"); |
| 44 | + Uri uri5 = new Uri("http://192.168.1.1:8080/path"); |
| 45 | + Console.WriteLine($" Host: {uri5.Host}"); // 192.168.1.1 |
| 46 | + Console.WriteLine($" IdnHost: {uri5.IdnHost}"); // 192.168.1.1 |
| 47 | + Console.WriteLine($" DnsSafeHost: {uri5.DnsSafeHost}"); // 192.168.1.1 |
| 48 | + // </SnippetHostComparison> |
| 49 | + } |
| 50 | +} |
0 commit comments