Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Get host name - port and path of website using “Uri Builder” class vb.net - C# .net.

Author: Maliha Atteeq
Download Source Code : 1042_application.zip

In this simple article you will learn that how we can get the host name, port and path of website using “Uri Builder” class in vb and C#.net.

 Uri Builder:

The Uri Builder class provides an easy way to modify the contents of an Uri instance without creating a new Uri instance for each modification. The Uri Builder properties provide read/write access to the read-only Uri properties so that they can be modified.

Host name:

host name is a name that is assigned to a host that uniquely identifies it on a network and thus allows it to be addressed without using its full IP address.

Set/Get the Host name:

In order to get or set the host name, Uri builder class provides us a property named as Host. This property returns the host name. The return type of this property is string.

To get the host name:

To demonstrate make a new window application. Drag one button on the form and write the following code:

C#:

private void get_host_Click(object sender, EventArgs e)

        {

            string host;

            host=obj.Host;

            MessageBox.Show("the host name is "+host);

        }

VB:

Private Sub get_host_Click(ByVal sender As Object, ByVal e As EventArgs)

    Dim host As String

    host = obj.Host

    MessageBox.Show("the host name is " & host)

End Sub

To set the host name:

To demonstrate make a new window application. Drag one button and one text box on the form and write the following code:

C#:

       private void btn_setHost_Click(object sender, EventArgs e)

        {

            string host;

            host = txt_setHost.Text;

            MessageBox.Show("the host name is set to " + host);

        }

VB:

Private Sub btn_setHost_Click(ByVal sender As Object, ByVal e As EventArgs)

    Dim host As String

    host = txt_setHost.Text

    MessageBox.Show("the host name is set to " & host)

End Sub

Port number:

The port number defines the protocol port for contacting the host referenced in the URI. If a port is not specified as part of the URI, the Port property returns the value of -1 to indicate that the default port value for the protocol scheme will be used to connect to the host.

Set/Get the Port number:

In order to get or set the port number, Uri builder class provides us a property named as Port. This property returns the port number which is used by your host. The return type of this property is integer.

To get the port number:

To demonstrate make a new window application. Drag one button on the form and write the following code:

C#:

private void btn_getPort_Click(object sender, EventArgs e)

        {

            int port;

            port=obj.Port;

            if (port == -1)

            {

                MessageBox.Show("the port is set to the default value");

            }

            else

            {

                MessageBox.Show("the port is " + Convert.ToString(port));

            }

        }

VB:

Private Sub btn_getPort_Click(ByVal sender As Object, ByVal e As EventArgs)

    Dim port As Integer

    port = obj.Port

    If port = -1 Then

        MessageBox.Show("the port is set to the default value")

    Else

        MessageBox.Show("the port is " & Convert.ToString(port))

    End If

End Sub

To set the port number:

To demonstrate make a new window application. Drag one button and one text box on the form and write the following code:

C#:

private void btn_setPort_Click(object sender, EventArgs e)

        {

            int port;

            port = Convert.ToInt32(txt_port.Text);

            if((port<-1)||(port>65535))

            {

                MessageBox.Show("the port number is out of range");

            }

            else

            {

                MessageBox.Show("the port is set to "+Convert.ToString(port));

            }

        }

VB:

Private Sub btn_setPort_Click(ByVal sender As Object, ByVal e As EventArgs)

    Dim port As Integer

    port = Convert.ToInt32(txt_port.Text)

    If (port < -1) OrElse (port > 65535) Then

        MessageBox.Show("the port number is out of range")

    Else

        MessageBox.Show("the port is set to " & Convert.ToString(port))

    End If

End Sub

Path:

The Path property contains the path information that the server uses to resolve requests for information. The path information does not include the scheme, host name, or query portion of the URI. The Path property always returns at least a slash (/).

Set/Get the Path:

In order to get or set the path, Uri builder class provides us a property named as Path. This property returns the path which is used by your host. The return type of this property is string.

To get the path:

To demonstrate make a new window application. Drag one button on the form and write the following code:

C#:

private void btn_getPath_Click(object sender, EventArgs e)

        {

            string path;

            path=obj.Path;

            MessageBox.Show("tha path is "+path);

        }

To set the path:

To demonstrate make a new window application. Drag one button and one text box on the form and write the following code:

C#:

private void btn_setPath_Click(object sender, EventArgs e)

        {

            string path;

            path = txt_path.Text;

            MessageBox.Show("the path is set to "+path);

        }

VB:

Private Sub btn_setPath_Click(ByVal sender As Object, ByVal e As EventArgs)

    Dim path As String

    path = txt_path.Text

    MessageBox.Show("the path is set to " & path)

End Sub

Now write the following code on FORM LOAD event:

C#:

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP APPLICATION";

        }

VB:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

   Me.Text = "DEVASP APPLICATION"

End Sub

This simple article tells how we can get the host name, port and path using “Uri Builder” class in vb and C#.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Send ping to web server - computer and get status using system.net vb.net – C#.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net