< An Intro to URI (URL & URN) | Main | Chap 6: System.Net Namespace >

 


 

Chapter 5 Part 2:

Developing with Uniform Resource Identifiers (URIs)

 

 

 

What do we have in this chapter 5 Part 2?

  1. C++ URIs Program Example

  2. C# URIs Program Example

  3. VB .NET URIs Program Example

 

 

C++ URIs Program Example

 

Create a new console application project. You can use the project and solution name, UriSamplesCP if you want.

 

C++ URIs Program Example - creating a new CLR console application project

 

Add the following functions and the main() codes.

 

// UriSamplesCP.cpp : main project file.

 

#include "stdafx.h"

 

using namespace System;

 

static void ConstructorSample()

{

    try

    {

        Console::WriteLine("A sample URI...");

        Uri^ uri = gcnew Uri("http://www.contoso.com/list.htm#new");

        Console::WriteLine(uri->ToString());

    }

    catch (UriFormatException^ uex)

    {

        Console::WriteLine(uex->ToString());

    }

}

 

static void CanonicalizeSample()

{

    try

    {

        // Note the raw URIs are all different

        Uri^ uriOne = gcnew Uri("http://www.contoso.com/Prod list.htm");

        Uri^ uriTwo = gcnew Uri("http://www.contoso.com:80/Prod%20list.htm");

        Uri^ uriThree = gcnew Uri("http://www.contoso.com/Prod%20list.htm");

 

        Console::WriteLine();

        Console::WriteLine("The given raw URIs...");

        Console::WriteLine(" http://www.contoso.com/Prod list.htm");

        Console::WriteLine(" http://www.contoso.com:80/Prod%20list.htm");

        Console::WriteLine(" http://www.contoso.com/Prod%20list.htm");

 

        Console::WriteLine();

        // The Canonical representation is the same for all three

        Console::WriteLine("Then the canonical representation...");

        Console::WriteLine(" uriOne = " + uriOne->ToString());

        Console::WriteLine(" uriTwo = " + uriTwo->ToString());

        Console::WriteLine(" uriThree = " + uriThree->ToString());

    }

    catch (UriFormatException^ uex)

    {

        Console::WriteLine(uex->ToString());

    }

}

 

static void ComparisonSample()

{

    try

    {

        // Note the raw URIs are different

        Uri^ uriOne = gcnew Uri("http://www.contoso.com/Prod list.htm");

        Uri^ uriTwo = gcnew Uri("http://www.contoso.com:80/Prod%20list.htm");

 

        Console::WriteLine();

        Console::WriteLine("The given raw URIs...");

        Console::WriteLine(" http://www.contoso.com/Prod list.htm");

        Console::WriteLine(" http://www.contoso.com:80/Prod%20list.htm");

 

        // Comparison is based on the canonical representation

        // so uriOne and uriTwo will be equal.

        Console::WriteLine();

        Console::WriteLine("Then, the URIs comparison...");

        Console::WriteLine(" uriOne.Equals(uriTwo) --> " + uriOne->Equals(uriTwo));

    }

    catch (UriFormatException^ uex)

    {

        Console::WriteLine(uex->ToString());

    }

}

 

static void UnknownSchemeSample()

{

    try

    {

        Console::WriteLine();

        Console::WriteLine("Unknown scheme general pattern...");

        Uri^ uriUnknown = gcnew Uri("unknown://authority/path?query");

        Console::WriteLine("Example: unknown://authority/path?query");

        Console::WriteLine(" scheme:" + uriUnknown->Scheme);

        Console::WriteLine(" authority:" + uriUnknown->Authority);

        Console::WriteLine(" path and query:" + uriUnknown->PathAndQuery);

        Console::WriteLine();

 

        Console::WriteLine("Unknown scheme that uses a custom pattern...");

        Uri^ uriUnknownCustom = gcnew Uri("unknown:path.authority.query");

        Console::WriteLine("Example: unknown:path.authority.query");

        Console::WriteLine(" scheme:" + uriUnknownCustom->Scheme);

        Console::WriteLine(" authority:" + uriUnknownCustom->Authority);

        Console::WriteLine(" path and query:" + uriUnknownCustom->PathAndQuery);

 

    }

    catch (UriFormatException^ uex)

    {

        Console::WriteLine(uex->ToString());

    }

}

 

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

int main(array<System::String ^> ^args)

{

    ConstructorSample();

    CanonicalizeSample();

    ComparisonSample();

    UnknownSchemeSample();

 

    return 0;

}

 

Build and run the project. The following is the sample output.

 

C++ URIs Program Example - a sample output with default argument values

 

C# URIs Program Example

 

Create a new console application project. You can use the project and solution name, UriSamplesCS if you want.

 

C# URIs Program Example - creating a new console application project

 

Add the following codes.

 

using System;

 

namespace UriSamplesCS

{

    /// <summary>

    /// Summary description for Class1.

    /// </summary>

    class Program

    {

        /// <summary>

        /// The main entry point for the application.

        /// </summary>

        [STAThread]

        static void Main(string[] args)

        {

            ConstructorSample();

            CanonicalizeSample();

            ComparisonSample();

            UnknownSchemeSample();

        }

 

        public static void ConstructorSample()

        {

            try

            {

                Console.WriteLine("A sample URI...");

                Uri uri = new Uri("http://www.contoso.com/list.htm#new");

                Console.WriteLine(uri.ToString());

            }

            catch (UriFormatException uex)

            {

                Console.WriteLine(uex.ToString());

            }

        }

 

        public static void CanonicalizeSample()

        {

            try

            {

                // Note the raw URIs are all different

                Uri uriOne = new Uri("http://www.contoso.com/Prod list.htm");

                Uri uriTwo = new Uri("http://www.contoso.com:80/Prod%20list.htm");

                Uri uriThree = new Uri("http://www.contoso.com/Prod%20list.htm");

 

                Console.WriteLine();

                Console.WriteLine("The raw URIs...");

                Console.WriteLine(" http://www.contoso.com/Prod list.htm");

                Console.WriteLine(" http://www.contoso.com:80/Prod%20list.htm");

                Console.WriteLine(" http://www.contoso.com/Prod%20list.htm");

 

                Console.WriteLine();

                // The Canonical representation is the same for all three

                Console.WriteLine("The canonical representation...");

                Console.WriteLine(" uriOne = " + uriOne.ToString());

                Console.WriteLine(" uriTwo = " + uriTwo.ToString());

                Console.WriteLine(" uriThree = " + uriThree.ToString());

            }

            catch (UriFormatException uex)

            {

                Console.WriteLine(uex.ToString());

            }

        }

 

        public static void ComparisonSample()

        {

            try

            {

                // Note the raw URIs are different

                Uri uriOne = new Uri("http://www.contoso.com/Prod list.htm");

                Uri uriTwo = new Uri("http://www.contoso.com:80/Prod%20list.htm");

 

                Console.WriteLine();

                Console.WriteLine("Raw URIs...");

                Console.WriteLine(" http://www.contoso.com/Prod list.htm");

                Console.WriteLine(" http://www.contoso.com:80/Prod%20list.htm");

 

                // Comparison is based on the canonical representation

                // so uriOne and uriTwo will be equal.

                Console.WriteLine();

                Console.WriteLine("URIs comparison...");

                Console.WriteLine(" uriOne.Equals(uriTwo) --> " + uriOne.Equals(uriTwo));

            }

            catch (UriFormatException uex)

            {

                Console.WriteLine(uex.ToString());

            }

        }

 

        public static void UnknownSchemeSample()

        {

            try

            {

                Console.WriteLine();

                Console.WriteLine("Unknown scheme general pattern");

                Uri uriUnknown = new Uri("unknown://authority/path?query");

                Console.WriteLine("Example: unknown://authority/path?query");

                Console.WriteLine(" scheme:" + uriUnknown.Scheme);

                Console.WriteLine(" authority:" + uriUnknown.Authority);

                Console.WriteLine(" path and query:" + uriUnknown.PathAndQuery);

 

                Console.WriteLine();

 

                Console.WriteLine("Unknown scheme that uses a custom pattern");

                Uri uriUnknownCustom = new Uri("unknown:path.authority.query");

                Console.WriteLine("Example: unknown:path.authority.query");

                Console.WriteLine(" scheme:" + uriUnknownCustom.Scheme);

                Console.WriteLine(" authority:" + uriUnknownCustom.Authority);

                Console.WriteLine(" path and query:" + uriUnknownCustom.PathAndQuery);

            }

            catch (UriFormatException uex)

            {

                Console.WriteLine(uex.ToString());

            }

        }

    }

}

 

Build and run the program. The following is a sample output.

 

C# URIs Program Example - a sample output with default argument values  

 

VB .NET URIs Program Example

 

Create a new console application project.

 

VB .NET URIs Program Example - creating a new console application project

 

Add the following codes.

 

Module Module1

 

    Sub Main()

        ConstructorSample()

        CanonicalizeSample()

        ComparisonSample()

        UnknownSchemeSample()

    End Sub

    Sub ConstructorSample()

        Try

            Dim uri As New Uri("http://www.contoso.com/list.htm#new")

            Console.WriteLine("URI sample = " + uri.ToString)

        Catch uex As UriFormatException

            Console.WriteLine(uex.ToString)

        End Try

    End Sub

 

    Sub CanonicalizeSample()

        Try

            ' Note the raw URIs are all different

            Dim uriOne As New Uri("http://www.contoso.com/Prod list.htm")

            Dim uriTwo As New Uri("http://www.contoso.com:80/Prod%20list.htm")

            Dim uriThree As New Uri("http://www.contoso.com/Prod%20list.htm")

 

            Console.WriteLine()

            Console.WriteLine("The raw URIs...")

            Console.WriteLine("http://www.contoso.com/Prod list.htm")

            Console.WriteLine("http://www.contoso.com:80/Prod%20list.htm")

            Console.WriteLine("http://www.contoso.com/Prod%20list.htm")

 

            ' The Canonical representation is the same for all three

            Console.WriteLine()

            Console.WriteLine("The Canonical representation of the previos raw URIs...")

            Console.WriteLine(" uriOne = " + uriOne.ToString())

            Console.WriteLine(" uriTwo = " + uriTwo.ToString())

            Console.WriteLine(" uriThree = " + uriThree.ToString())

        Catch uex As UriFormatException

            Console.WriteLine(uex.ToString)

        End Try

 

    End Sub

    Sub ComparisonSample()

        Try

            ' Note the raw URIs are different

            Dim uriOne As New Uri("http://www.contoso.com/Prod list.htm")

            Dim uriTwo As New Uri("http://www.contoso.com:80/Prod%20list.htm")

 

            Console.WriteLine()

            Console.WriteLine("Another raw URIs examples...")

            Console.WriteLine(" http://www.contoso.com/Prod list.htm")

            Console.WriteLine(" http://www.contoso.com:80/Prod%20list.htm")

 

            'Comparison is based on the canonical representation

            'so uriOne and uriTwo will be equal.

            Console.WriteLine()

            Console.WriteLine("The URIs comparison...")

            Console.Write("uriOne.Equals(uriTwo) ---> ")

            Console.WriteLine(uriOne.Equals(uriTwo))

 

        Catch uex As UriFormatException

            Console.WriteLine(uex.ToString)

        End Try

 

    End Sub

 

    Sub UnknownSchemeSample()

        Try

            Console.WriteLine("Unknown scheme that follows the general pattern")

            Dim uriUnknown As New Uri("unknown://authority/path?query")

 

            Console.WriteLine()

 

            Console.WriteLine("Example ---> unknown://authority/path?query")

            Console.WriteLine(" scheme:" + uriUnknown.Scheme)

            Console.WriteLine(" authority:" + uriUnknown.Authority)

            Console.WriteLine(" path and query:" + uriUnknown.PathAndQuery)

            Console.WriteLine()

 

            Console.WriteLine("Unknown scheme that uses a custom pattern")

            Dim uriUnknownCustom As New Uri("unknown:path.authority.query")

 

            Console.WriteLine("Example ---> unknown:path.authority.query")

            Console.WriteLine(" scheme:" + uriUnknownCustom.Scheme)

            Console.WriteLine(" authority:" + uriUnknownCustom.Authority)

            Console.WriteLine(" path and query:" + uriUnknownCustom.PathAndQuery)

 

        Catch ex As Exception

            Console.WriteLine(ex.ToString)

        End Try

    End Sub

End Module

 

Build and run the program. The following is a sample output.

 

VB .NET URIs Program Example - a sample output using default argument values

 

 

 


 

< An Intro to URI (URL & URN) | Main | Chap 6: System.Net Namespace >