In VB.NET programs, we often need to create and store URIs, or URLs, to remote resources like web servers. This can be done with the Uri and UriBuilder
Classes.
To test if a URL is valid, we can use the Uri.TryCreate
method. This tester-doer method returns True if the string
can be parsed as a URL.
One benefit to storing URLs in Uri objects is that we can access properties on the Uri. These properties can return important parts like the Host or the Scheme (like http).
Module Module1 Sub Main() Dim uri As Uri = new Uri("https://www.dotnetperls.com/") ' Print properties of Uri instance. Console.WriteLine("AbsolutePath = {0}", uri.AbsolutePath) Console.WriteLine("AbsoluteUri = {0}", uri.AbsoluteUri) Console.WriteLine("Authority = {0}", uri.Authority) Console.WriteLine("DnsSafeHost = {0}", uri.DnsSafeHost) Console.WriteLine("Fragment = {0}", uri.Fragment) Console.WriteLine("Host = {0}", uri.Host) Console.WriteLine("HostNameType = {0}", uri.HostNameType) Console.WriteLine("IsAbsoluteUri = {0}", uri.IsAbsoluteUri) Console.WriteLine("IsDefaultPort = {0}", uri.IsDefaultPort) Console.WriteLine("IsFile = {0}", uri.IsFile) Console.WriteLine("IsLoopback = {0}", uri.IsLoopback) Console.WriteLine("IsUnc = {0}", uri.IsUnc) Console.WriteLine("LocalPath = {0}", uri.LocalPath) Console.WriteLine("OriginalString = {0}", uri.OriginalString) Console.WriteLine("PathAndQuery = {0}", uri.PathAndQuery) Console.WriteLine("Port = {0}", uri.Port) Console.WriteLine("Query = {0}", uri.Query) Console.WriteLine("Scheme = {0}", uri.Scheme) Console.WriteLine("Segments = {0}", String.Join(",", uri.Segments)) Console.WriteLine("UserEscaped = {0}", uri.UserEscaped) Console.WriteLine("UserInfo = {0}", uri.UserInfo) End Sub End ModuleAbsolutePath = / AbsoluteUri = https://www.dotnetperls.com/ Authority = www.dotnetperls.com DnsSafeHost = www.dotnetperls.com Fragment = Host = www.dotnetperls.com HostNameType = Dns IsAbsoluteUri = True IsDefaultPort = True IsFile = False IsLoopback = False IsUnc = False LocalPath = / OriginalString = https://www.dotnetperls.com/ PathAndQuery = / Port = 443 Query = Scheme = https Segments = / UserEscaped = False UserInfo =
TryCreate
Is a String
a valid URL? We can determine this with Uri.TryCreate
, and as a bonus we can get the constructed Uri object as a return parameter.
Module Module1 Sub Main() ' Part 1: use Uri.TryCreate on valid url. Dim uri1 As Uri = Nothing If Uri.TryCreate("http://www.dotnetperls.com/", UriKind.Absolute, uri1) Console.WriteLine("1 = {0}", uri1) End If ' Part 2: handle an invalid Url with Uri.TryCreate. Dim uri2 As Uri = Nothing If Uri.TryCreate("http:dotnetperls-com", UriKind.Absolute, uri2) Console.WriteLine("2 = {0}", uri2) ' Not reached. End If End Sub End Module1 = http://www.dotnetperls.com/
UriBuilder
It is sometimes better to create a Uri object by using the UriBuilder
Class
. We can set parts like the Host, Path
, and Scheme to modify the returned URL.
UriBuilder
handles the syntax of the Uri scheme, such as the path separator and the scheme delimiter.Module Module1 Sub Main() ' Use UriBuilder constructor. Dim u1 As UriBuilder = New UriBuilder("http", "www.dotnetperls.com") Console.WriteLine($"UriBuilder: {u1}") ' Use UriBuilder properties. Dim u2 As UriBuilder = New UriBuilder() u2.Host = "www.dotnetperls.com" u2.Path = "uribuilder" u2.Scheme = "http" Console.WriteLine($"UriBuilder: {u2}") ' Convert to Uri. Dim uri As Uri = u2.Uri Console.WriteLine($" Uri: {uri}") End Sub End ModuleUriBuilder: http://www.dotnetperls.com/ UriBuilder: http://www.dotnetperls.com/uribuilder Uri: http://www.dotnetperls.com/uribuilder
For complex VB.NET programs, using Uri and UriBuilder
to manage URLs is a good idea as it can reduce possible syntax bugs in the resulting URLs. It can lead to more robust programs.