Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Using the URI Class

Shows you how to parse a URI using the URI class and how to form an absolute URI from a base URI and a relative URI.

On Saturday, June 26th 2004 at 02:25 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.2 with 19 votes)
Contextual Ads
More C# Resources
Advertisement

Parsing a URI


Create a new Windows Application project. Add two textBoxes and a button. The first textBox, name it txtURI. The second, name it txtResult and set the multiline property to True. Name the button btnParse. Do the rest of adjustments so that the form looks like the one below.



First let's not forget to add a reference to the System.Net namespace.





using System.Net;



Double click btnParse to get to the btnParse_Click() event. Here we shall use the following code:





txtResult.Clear();

Uri uri = new Uri(txtURI.Text);

txtResult.AppendText("Absolute URI: " + uri.AbsoluteUri + "\r\n");

txtResult.AppendText("Absolute Path: " + uri.AbsolutePath + "\r\n");

txtResult.AppendText("Local path: " + uri.LocalPath + "\r\n");

txtResult.AppendText("Scheme: " + uri.Scheme + "\r\n");

txtResult.AppendText("Authority: " + uri.Authority + "\r\n");

txtResult.AppendText("Host: " + uri.Host + "\r\n");

txtResult.AppendText("Port: " + uri.Port + "\r\n");

txtResult.AppendText("Fragment: " + uri.Fragment + "\r\n");

txtResult.AppendText("Query: " + uri.Query + "\r\n");



First we clear txtResult from any prior parsing results.

In the second line we create a new Uri using the URL in the textBox. Next comes a sequence of uri properties:

AbsoluteUri property returns the complete URI. If you want you can call this the complete URI.

AbsolutePath returns the absolute path used by the server.

Local Path returns the local path of the file.

Scheme gets the scheme of the current URI (ex.: file, http, ftp...).

Authority returns a string composed from the host name / IP address and port number of the server.

Host property returns a string with the host name / IP address of the server.

Port gets the port of the server.

Fragment returns the text followed by a marker (#) in an URI.

Query gives you the query information from the URI (ex.: ?id=5).



Here is an example result:







There's another useful thing you can do using the Uri class:




Forming an absolute URI from a base URI and a relative URI


You can continue this on the same form. Add three textBoxes and a button. Name the first one txtBase, the second txtRelative and the third txtAbsolute. The name of the button will be btnForm. Now double click it. In the btnForm_Click() event use the following code:





Uri baseUri = new Uri(txtBase.Text);

Uri absoluteUri = new Uri(baseUri, txtRelative.Text);

txtAbsolute.Text = absoluteUri.ToString();



We create a new Uri named baseUri and then another named absoluteUri that is composed from the baseUri and the relative path contained in txtRelative.

Here's an example result:



Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Ashok Mohanty(India) on Saturday, June 25th 2005 at 04:15 AM

Hi,
The code given by u is not working.Plz send me the details.How to run the code step by step..Waiting for u'r mail
regards
Ashok

by Dan on Thursday, June 8th 2006 at 02:59 PM

Any hints on how to deal with multiple querystrings? Is there a simple way to separate them out?


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources