Thursday, May 19, 2011

HttpUtility Class Methods in Asp.Net

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 < and " is replaced with ".

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: “

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

Label1.Text = "" 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("");

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¶m2=val2¶m3=val3¶m4=val4¶m5=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");

No comments:

Post a Comment