Tomas Tomas - 24 days ago 8
C# Question

Url Unicode characters encoding

How to encode URLs containing Unicode? I would like to pass it to a command line utility and I need to encode it first.

Example:

http://zh.wikipedia.org/wiki/白雜訊


becomes
http://zh.wikipedia.org/wiki/%E7%99%BD%E9%9B%9C%E8%A8%8A
.

Answer

You can use the HttpUtility.UrlPathEncode method in the System.Web assembly (requires the full .NET Framework 4 profile):

var encoded = HttpUtility.UrlPathEncode("http://zh.wikipedia.org/wiki/白雜訊");