3

this may be a silly question, but it trips me up every time.

HttpUtility has the methods HtmlDecode and UrlDecode. Do these two methods decode anything (Html/Http related) I might find? When do I have to use them, and which one am I supposed to use?

Just now I hit an error. This is my error log:

Payment receiver was not payment@mysite.com. (it was payment%40mysite.com).

But, I wrapped the email address here in HttpUtility.HtmlDecode before using it. It turns out I have to use .UrlDecode instead, but this email address didn't come from a URL so this wasn't obvious to me.

Can someone clarify this?

Oliver
  • 11,297
  • 18
  • 71
  • 121
  • Does this content comes from GET request? (AJAX, etc. you do not see it URL, but it is still GET request). – Rolice Jan 18 '12 at 15:25

4 Answers4

3

See What is meant by htmlencode and urlencode?

It's the reverse of your case, but essentially you need to use UrlEncode/Decode anytime you are using an address of sorts (urls and yes, email addresses). HtmlEncode/Decode is for code that typically a browser would render (html/xml tags).

Community
  • 1
  • 1
PinnyM
  • 35,165
  • 3
  • 73
  • 81
2

This same encoding is also used in Form POST requests as well.

My guess is something read it 'naked' without decoding it.

Html Encoding/Decoding is only used to escape strings that contain characters that would otherwise be interpreted as html control characters. The process turns the characters into html entities and back again.

Url Encoding is to get around the fact that many characters are not allowed in Uris; or because they too could be misinterpreted. Thus the percent encoding is used.

Percent encoding is also used in the body of http requests.

In both cases, of course, it's also a way of expressing a specific character code in a request/response independent of character sets; but equally, interpreting what is meant by a particular code can also be dependent on knowing a particular character set. Generally you don't worry about that - but it can be important (especially in the HTML case).

Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
  • Is this kind of encoding only used in URLs and POST requests, or elsewhere too? – Oliver Jan 18 '12 at 15:23
  • well it could be used anywhere - but it's most commonly used in URLs, URIs and URNs. Equally, other protocols that piggy-back on HTTP may well, of course, utilise percent encoding. – Andras Zoltan Jan 18 '12 at 15:29
2

When you see %xx this means percent encoding has occured - this is a URL encoding scheme, so you need to use UrlEncode / UrlDecode.

The HtmlEncode and HtmlDecode methods are for encoding and decoding elements for HTML display - so things like & get encoded to & and > to >.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
2

URLEncode converts characters that aren't allowed in a URL into character equivalents which are parsable as a URL. In your example @ became %40. URLDecode reverses this.

HTMLEncode is similar to URLEncode, but the target environment is text NESTED inside of HTML. This helps the browser from interpereting your content as HTML, but when rendered it should look like the decoded version. HTMLDecode reverses this.