Google Analytics İzleme

06 Haziran 2011

Encode Decode URL posted strings with unicode chars

Form field values are always encoded during form submit. 
A simple string like "text to submit" would be encoded like "text+to+submit" which if not decoded properly, can produce problems for viewing or debugging. 

The natural choice to decode seems to be HttpUtility.HtmlEncode. If you use HttpUtility.HtmlEncode, like the following piece of code demonstrate: 

string TestString = "This is a <Test String>."; 
string EncodedString = Server.HtmlEncode(TestString);

AND you would suppose it would yield "This+is+a+%3cTest+String%3e." But it reality, it yields "This is a &lt;Test String&gt;." 

Now, what's happening here is that the resulting string is safer for HTTP transfer but it's not good enough to be POSTed. This is where HttpUtility.UrlEncode comes to the rescue. A string encoded with UrlEncode can be safely POSTed to another page. 

string TestString = "This is a <Test String>."; 
string EncodedString = Server.URLEncode(TestString);

Resulted string = This+is+a+%3cTest+String%3e 

Kaynak:
http://www.beyondweblogs.com/post/Encode-Decode-URL-posted-strings-2b-unicode-chars.aspx


Hiç yorum yok: