jishi
jishi

Reputation: 24614

HttpUtility.UrlEncode standard encoding vs specified encoding?

What would the difference be between:

HttpUtility.UrlEncode("some string with é and β and stuff")
HttpUtility.UrlEncode("some string with é and β and stuff", Encoding.UTF8)
HttpUtility.UrlEncode( "some string with é and β and stuff", Encoding.Default )

result being:

some+string+with+%c3%a9+and+%ce%b2+and+stuff
some+string+with+%c3%a9+and+%ce%b2+and+stuff
some+string+with+%e9+and+%df+and+stuff

When testing, I get the same result for the first two, so can i safely assume that UTF8 is the default unless specified, or can that differ on different systems?

I have seene examples of unicode escape sequences that looks like this:

%u00e9 (é)

Fairly certain that paypal sends that in their IPN-requests. Why doesn't .NET encode like that?

Upvotes: 7

Views: 16969

Answers (2)

Saul Dolgin
Saul Dolgin

Reputation: 8804

Yes, you can safely assume that UTF8 is the default based on your examples above. Keeping in mind that the default encoding with .NET is determined by the underlying code page of the operating system.

The '%u00e9' example that you have seen from PayPal is actually a non-standard implementation for encoding Unicode characters. According to Wikipedia, this implementation has been rejected by the W3C.

Upvotes: 3

user151323
user151323

Reputation:

The source code for the method HttpUtility.UrlEncode Method (String) from the Reflector:

public static string UrlEncode(string str)
{
    if (str == null)
    {
        return null;
    }
    return UrlEncode(str, Encoding.UTF8);
}

To your question:

so can i safely assume that UTF8 is the default unless specified

Yes, you can.

Upvotes: 8

Related Questions