Url Parsing in Visual Basic .NET

Here’s the translation of the Go URL parsing example to Visual Basic .NET, formatted in Markdown suitable for Hugo:

Our URL parsing program demonstrates how to parse and extract different components from a URL in Visual Basic .NET.

Imports System
Imports System.Net

Module URLParsing
    Sub Main()
        ' We'll parse this example URL, which includes a
        ' scheme, authentication info, host, port, path,
        ' query params, and query fragment.
        Dim s As String = "postgres://user:pass@host.com:5432/path?k=v#f"

        ' Parse the URL and ensure there are no errors.
        Dim u As New Uri(s)

        ' Accessing the scheme is straightforward.
        Console.WriteLine(u.Scheme)

        ' UserInfo contains all authentication info; we need to parse it manually
        ' to get individual username and password values.
        Console.WriteLine(u.UserInfo)
        Dim userInfoParts() As String = u.UserInfo.Split(":")
        Console.WriteLine(userInfoParts(0))
        Console.WriteLine(userInfoParts(1))

        ' The Host contains both the hostname and the port,
        ' if present. Use Uri properties to extract them.
        Console.WriteLine(u.Host)
        Console.WriteLine(u.DnsSafeHost)
        Console.WriteLine(u.Port)

        ' Here we extract the path and the fragment after
        ' the #.
        Console.WriteLine(u.AbsolutePath)
        Console.WriteLine(u.Fragment)

        ' To get query params in a string of k=v format,
        ' use Query. You can also parse query params
        ' into a collection. The parsed query param collection is from
        ' strings to strings, so access directly if you want the value.
        Console.WriteLine(u.Query)
        Dim queryParams = HttpUtility.ParseQueryString(u.Query)
        Console.WriteLine(queryParams.ToString())
        Console.WriteLine(queryParams("k"))
    End Sub
End Module

To run the program, save it as URLParsing.vb and compile it using the Visual Basic .NET compiler. Then, execute the resulting executable.

$ vbc URLParsing.vb
$ URLParsing.exe
postgres
user:pass
user
pass
host.com:5432
host.com
5432
/path
#f
?k=v
k=v
v

This program demonstrates how to parse URLs in Visual Basic .NET. It uses the Uri class to parse the URL and extract various components such as scheme, user info, host, port, path, query parameters, and fragment.

Note that Visual Basic .NET’s Uri class handles most of the parsing automatically, but some parts (like separating username and password from user info) need to be done manually. The HttpUtility.ParseQueryString method is used to parse query parameters into a collection, which is similar to the map in the original example.

Remember to add Imports System.Web at the top of your file to use HttpUtility, and reference the System.Web assembly when compiling if necessary.