Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UrlEncodeUnicode and browser navigation errors

I want to redirect a request to some URL that may or may not contain non-ascii characters (e.g. german umlauts).

Doing this with the relevant part of the URL:

var url = HttpUtility.UrlEncodeUnicode("öäü.pdf"); // -> "%u00f6%u00e4%u00fc.pdf"

and then issuing the redirect:

Response.Redirect(url, ...);

will not produce the desired behaviour. It appears, the browser (IE, Opera as far as I have tested) doesn't honor this command when the URL to redirect to is Unicode-encoded. Ordinary UrlEncode'd paths work fine.

I have tried setting this in the Web.Config:

<configuration>
  <system.web>
    <globalization
      requestEncoding="utf-8"
      responseEncoding="utf-8"
    />
  </system.web>
</configuration>

That didn't change a thing.

Is there anything I can do, to get this to work?

like image 994
skarmats Avatar asked Nov 16 '25 07:11

skarmats


2 Answers

I am not sure with question, but could you try with this?

HttpUtility.UrlEncode("öäü.pdf")

or

HttpUtility.UrlEncode("öäü.pdf", Encoding.UTF8)

Sorry, If I understand your question wrong way.

like image 78
YOU Avatar answered Nov 18 '25 19:11

YOU


It produces strings that contain two %.. sequences for every double-byte character as opposed to a single %u.... sequence when using UrlEncodeUnicode(...). Any idea why this is?

Because UTF-8 uses multiple bytes to represent non-ASCII characters and each byte is URL-encoded separately. This is the standard way to encode Unicode in URI, as used by IRI and by default in all modern web browsers.

UrlEncodeUnicode and %u00f6, on the other hand, is totally spurious nonsense that should not be used for anything, and has been put there only to confuse you.

like image 43
bobince Avatar answered Nov 18 '25 20:11

bobince



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!