1

I got a warning during building some legacy code:

'System.Web.HttpUtility.UrlEncodeUnicode(string)' is obsolete: '"This method produces non-standards-compliant output and has interoperability issues. The preferred alternative is UrlEncode(String)."'

Is it safe to replace HttpUtility.UrlEncodeUnicode(string) with HttpUtility.UrlEncode(string)? Do both methods produce identical results for any string?

If not, what is the preferred alternative?

Slaven Semper
  • 117
  • 1
  • 2
  • 8
  • 2
    If they are telling you that one of them produces invalid output and to use another I think it is safe to assume they don't generate **identical** output. – mjwills Sep 26 '19 at 05:37
  • Indeed, safe is relative... – TheGeneral Sep 26 '19 at 05:44
  • Maybe you need to write up some test cases – TheGeneral Sep 26 '19 at 05:45
  • 1
    They will not produce identical output. In particular, the Unicode method produces %uxxxx output which is a non-standard extension not valid for URLs per the RFC. See https://stackoverflow.com/questions/912811/ (a bit old, should be a good search base) for some details. – user2864740 Sep 26 '19 at 05:53

2 Answers2

2

Do both methods produce identical results for any string?

No, example:

HttpUtility.UrlEncodeUnicode("☺"); // %u263a
HttpUtility.UrlEncode("☺") // %e2%98%ba

Is it safe to replace HttpUtility.UrlEncodeUnicode(string) with HttpUtility.UrlEncode(string)?

Almost safe, it depends on the decoder, most decoder can handle the result from UrlEncode because it's standard, if your project uses a non-standard one, then it's not safe. (BTW HttpUtility.UrlDecode can handle both results.)

shingo
  • 18,436
  • 5
  • 23
  • 42
0

Based on Microsoft Reference Source (which has the source code of .NET references). The HttpUtility.UrlEncode(string) is an alternative of HttpUtility.UrlEncodeUnicode(string) as shown below :

[Obsolete("This method produces non-standards-compliant output and has interoperability issues. The preferred alternative is UrlEncode(String).")]
public static string UrlEncodeUnicode(string str) {..} // just removed the code in case of copy-rights. 

So, it's preferred to use UrlEncode(*) over UrlEncodeUnicode(*), you can check the reference source from here

iSR5
  • 3,274
  • 2
  • 14
  • 13