CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
HttpUtility Class Methods in Asp.Net

By Suresh Kumar Goudampally
Posted On Apr 05, 2011
Article Rating:
Be first to rate
this article.
No of Comments: 0
Category: ASP.Net
Print this article.

HttpUtility Class Methods in Asp.Net

Introduction

HttpUtility is the class library provided by the Asp.Net consists of set of methods which are useful in developing web applications. The utility mainly provides methods for encoding and decoding urls useful for processing Web Requests. In this article I will be explaining in detail about each method by quoting some real time scenarios where actually these methods are useful.

Here is the list of the HttpUtility methods, I will be explaining in this article.

1.      UrlEncode/UrlDecode

2.      HtmlEncode/HtmlDecode

3.      HtmlAttributeEncode

4.      ParseQueryString

5.      UrlPathEncode

 

1.UrlEncode/UrlDecode Method

             Purpose

The main purpose of these two methods is to encode and decode the urls respectively. We need to encode the urls because some of the characters are not safe sending those across browser. Some characters are being misunderstood by the browser and that leads to data mismatch on the receiving end. Characters such as a question mark (?), ampersand (&), slash mark (/), and spaces might be truncated or corrupted by some browsers.

For instance user requires sending the data in the url querystrings in the following format

Eg:

string Url = "http://localhost:9618/WebForm1.aspx?" + "names=suresh#satheesh#vijay#";          

On the receiving end if the developer tries to read the url querystring values in the following way

string QueryStringValues = Request.QueryString["names"];

the developers  reads it as “suresh” which is not the expected result

The workaround for this we need to encode the url and read the querystring values to get the expected result.

UrlEncode

Eg:

string Url = "http://localhost:9618/WebForm1.aspx?" + HttpUtility.UrlEncode("names=suresh#satheesh#vijay");

Response.Redirect(Url);

When we use the UrlEncode for the above url, the new url looks in the following way:

http://localhost:9618/WebForm1.aspx?names=suresh%23satheesh%23vijay

When the user tries to fetch the querystring values on the receiving end, the expected result is “suresh#satheesh#vijay

Eg:

string QueryStringValues = Request.QueryString["names"];       

Response.Write(QueryStringValues);

           

UrlDecode

This method is used to convert the encoded url back into a decoded string.

If you want to decode the entire url on the receiving end, just decode in the following way.

Eg:

string Url = HttpUtility.UrlDecode(Request.RawUrl);

The expected result would be "/WebForm1.aspx?names=suresh#satheesh#vijay” ;

We can also decode only the querystring values on the receiving end in the following way,

Eg:

string QueryStringValues = Request.QueryString["names"];

QueryStringValues = HttpUtility.UrlDecode(QueryStringValues); 

 

2.    HtmlEnCode/HtmlDecode Method

HtmlEncode -Purpose

The main purpose of HtmlEncode is to convert a HTML-encoded string for reliable HTTP transmission from the Web server to a client. It mainly converts the characters that are not allowed in Html like “<”,”>”, spaces and punctuation marks.  For example, < is replaced with &lt; and " is replaced with &quot;.

HtmlDecode converts back to the original string which is html encoded.

For instance user has a requirement where he wants to display the data in the following way

Eg: “<Suresh Kumar>” 

If the developer uses the following code, it is misinterpreted on the browser receiving end 

Label1.Text = "<Suresh Kumar>"  and nothing is visible on the browser.

To achive this we need use the HtmlEncode for displaying the correct data.

Eg:

Label1.Text = HttpUtility.HtmlEncode("<Suresh Kumar>"); 

Also Htmlencoding is used to avoid Cross-site scripting.

 

3.    HtmlAttributeEncode Method

Purpose

This method is used to encode only certain characters like Quotation marks (“), ampersands (&), and left   angle brackets. Performance wise, this method is faster than the HtmlEncode.

Eg:

Response.Write(HttpUtility.HtmlAttributeEncode(TextBox3.Text));

 

4.ParseQueryString Method

Purpose

This method is used to retrieve the querystring values of an url in the NameValueCollection format.  This helps developers for traversing easily to read the querystring values when there are multiple querystring values in the url. The NameValueCollection in .Net framework comes with namespace System.Collections.Specialized.

Let’s say user has an url with querystring values on the receiving end then ParseQueryString is used for retrieving the values.

Eg:

string Url = "http://localhost:3873/WebForm1.aspx?" + "param1=val1&param2=val2&param3=val3&param4=val4&param5=val5";

Response.Redirect(Url);

On the receiving end or code behind we can read the querystring values using the ParseQueryString in the following way.

Uri Uri = new Uri(Request.Url.AbsoluteUri);

NameValueCollection nvColl = HttpUtility.ParseQueryString(Uri.Query);

string param1value = nvColl["param1"];

On the receiving end the Request.QueryString also retrieves the NameValueCollection format of querystring values. The difference between HttpUtility.ParseQueryString and Request.QueryString is the former requires the complete url as input where as the later doesn’t require.

So HttpUtility.ParseQueryString can be used not only on the receiving end but on any code behind file when developer requires to split and play with the url to read the querystring values.

 




5.UrlPathEncode Method

Purpose

UrlPathEncode is similar to UrlEncode method which is used to encode the url. The only difference between two is UrlEncode replaces space with “+” and UrlPathEncode replaces space with “%20”.

 

The other important point is using UrlPathEncode we can only encode portion of the url i.e strings. If we try to encode entire url it won’t get encoded.

For instance if we try to encode in the following way, the url is not encoded

Eg: 

string strUrl = "http://localhost:9618/WebForm1.aspx?names=suresh satheesh vidjay";

strUrl = HttpUtility.UrlPathEncode(strUrl);

 

 We can use UrlPathEncode in the following way to encode only portion of querystring

 Eg:

string strUrl = "http://localhost:9618/WebForm1.aspx?names" + HttpUtility.UrlPathEncode("suresh satheesh vijay");

 

Conclusion

In this article, I have explained the importance of HttpUtility methods provided in the .Net Framework. Also explained the purpose of each methods and how they are used in the real time scenarios.

Happy Coding!!

                                  

 

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments