Skip to content

Commit 95a1139

Browse files
CopilotMihaZupan
andcommitted
Document Host, IdnHost, and DnsSafeHost differences with examples
Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
1 parent 6e6673e commit 95a1139

7 files changed

Lines changed: 239 additions & 9 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net8.0</TargetFramework>
5+
</PropertyGroup>
6+
</Project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net8.0</TargetFramework>
5+
</PropertyGroup>
6+
</Project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
open System
2+
3+
// <SnippetHostComparison>
4+
// Demonstrate differences between Host, IdnHost, and DnsSafeHost.
5+
6+
// Example 1: Regular hostname (ASCII).
7+
printfn "Example 1: Regular ASCII hostname"
8+
let uri1 = Uri("http://www.contoso.com:8080/path")
9+
printfn $" Host: {uri1.Host}" // www.contoso.com
10+
printfn $" IdnHost: {uri1.IdnHost}" // www.contoso.com
11+
printfn $" DnsSafeHost: {uri1.DnsSafeHost}" // www.contoso.com
12+
printfn ""
13+
14+
// Example 2: International domain name (non-ASCII).
15+
printfn "Example 2: International domain name"
16+
let uri2 = Uri("http://münchen.de/path")
17+
printfn $" Host: {uri2.Host}" // münchen.de (original)
18+
printfn $" IdnHost: {uri2.IdnHost}" // xn--mnchen-3ya.de (punycode)
19+
printfn $" DnsSafeHost: {uri2.DnsSafeHost}" // depends on configuration
20+
printfn ""
21+
22+
// Example 3: IPv6 address without zone ID.
23+
printfn "Example 3: IPv6 address without zone ID"
24+
let uri3 = Uri("http://[::1]:8080/path")
25+
printfn $" Host: {uri3.Host}" // [::1] (with brackets)
26+
printfn $" IdnHost: {uri3.IdnHost}" // ::1 (without brackets)
27+
printfn $" DnsSafeHost: {uri3.DnsSafeHost}" // ::1 (without brackets)
28+
printfn ""
29+
30+
// Example 4: IPv6 link-local address with zone ID.
31+
printfn "Example 4: IPv6 link-local address with zone ID"
32+
let uri4 = Uri("http://[fe80::1%10]:8080/path")
33+
printfn $" Host: {uri4.Host}" // [fe80::1] (with brackets, no zone ID)
34+
printfn $" IdnHost: {uri4.IdnHost}" // fe80::1%10 (without brackets, with zone ID)
35+
printfn $" DnsSafeHost: {uri4.DnsSafeHost}" // fe80::1%10 (without brackets, with zone ID)
36+
printfn ""
37+
38+
// Example 5: IPv4 address.
39+
printfn "Example 5: IPv4 address"
40+
let uri5 = Uri("http://192.168.1.1:8080/path")
41+
printfn $" Host: {uri5.Host}" // 192.168.1.1
42+
printfn $" IdnHost: {uri5.IdnHost}" // 192.168.1.1
43+
printfn $" DnsSafeHost: {uri5.DnsSafeHost}" // 192.168.1.1
44+
// </SnippetHostComparison>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net8.0</TargetFramework>
5+
</PropertyGroup>
6+
</Project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Imports System
2+
3+
Public Class UriHostComparison
4+
Public Shared Sub Main()
5+
' <SnippetHostComparison>
6+
' Demonstrate differences between Host, IdnHost, and DnsSafeHost.
7+
8+
' Example 1: Regular hostname (ASCII).
9+
Console.WriteLine("Example 1: Regular ASCII hostname")
10+
Dim uri1 As New Uri("http://www.contoso.com:8080/path")
11+
Console.WriteLine($" Host: {uri1.Host}") ' www.contoso.com
12+
Console.WriteLine($" IdnHost: {uri1.IdnHost}") ' www.contoso.com
13+
Console.WriteLine($" DnsSafeHost: {uri1.DnsSafeHost}") ' www.contoso.com
14+
Console.WriteLine()
15+
16+
' Example 2: International domain name (non-ASCII).
17+
Console.WriteLine("Example 2: International domain name")
18+
Dim uri2 As New Uri("http://münchen.de/path")
19+
Console.WriteLine($" Host: {uri2.Host}") ' münchen.de (original)
20+
Console.WriteLine($" IdnHost: {uri2.IdnHost}") ' xn--mnchen-3ya.de (punycode)
21+
Console.WriteLine($" DnsSafeHost: {uri2.DnsSafeHost}") ' depends on configuration
22+
Console.WriteLine()
23+
24+
' Example 3: IPv6 address without zone ID.
25+
Console.WriteLine("Example 3: IPv6 address without zone ID")
26+
Dim uri3 As New Uri("http://[::1]:8080/path")
27+
Console.WriteLine($" Host: {uri3.Host}") ' [::1] (with brackets)
28+
Console.WriteLine($" IdnHost: {uri3.IdnHost}") ' ::1 (without brackets)
29+
Console.WriteLine($" DnsSafeHost: {uri3.DnsSafeHost}") ' ::1 (without brackets)
30+
Console.WriteLine()
31+
32+
' Example 4: IPv6 link-local address with zone ID.
33+
Console.WriteLine("Example 4: IPv6 link-local address with zone ID")
34+
Dim uri4 As New Uri("http://[fe80::1%10]:8080/path")
35+
Console.WriteLine($" Host: {uri4.Host}") ' [fe80::1] (with brackets, no zone ID)
36+
Console.WriteLine($" IdnHost: {uri4.IdnHost}") ' fe80::1%10 (without brackets, with zone ID)
37+
Console.WriteLine($" DnsSafeHost: {uri4.DnsSafeHost}") ' fe80::1%10 (without brackets, with zone ID)
38+
Console.WriteLine()
39+
40+
' Example 5: IPv4 address.
41+
Console.WriteLine("Example 5: IPv4 address")
42+
Dim uri5 As New Uri("http://192.168.1.1:8080/path")
43+
Console.WriteLine($" Host: {uri5.Host}") ' 192.168.1.1
44+
Console.WriteLine($" IdnHost: {uri5.IdnHost}") ' 192.168.1.1
45+
Console.WriteLine($" DnsSafeHost: {uri5.DnsSafeHost}") ' 192.168.1.1
46+
' </SnippetHostComparison>
47+
End Sub
48+
End Class

0 commit comments

Comments
 (0)