Monday, 10 May 2010

How to convert UTF-8 to urlencoded string

As you know, System.Web.HttpUtility.UrlEncode can easily convert UTF-8 string into the format of %xx%xx so it can be used in a URL string. Now imagine you are not working on a ASP.NET project and you don’t want to add reference of System.Web.dll in your project, then how can you do the same thing? Below is one solution I found:

   1: private static string GetEncodedString(string username)
   2: {
   3:     StringBuilder sb = new StringBuilder();
   4:     byte[] btyes = System.Text.Encoding.Default.GetBytes(username);
   5:     for (int i = 0; i < btyes.Length; i++)
   6:     {
   7:         sb.Append(@"%" + Convert.ToString(btyes[i], 16));
   8:     }
   9:  
  10:     return sb.ToString();
  11: }

Now if you pass in chinese string 你好 it will turn out %c4%e3%ba%c3, which is exactly what I want. The reason I create this method is because I am trying to use HttpWebRequest component to directly post data to a web page and I need a way to submit UTF-8 string.

BTW, the only difference between my method and UrlEncode is my method will convert all characters in the passed in string to %xx format even it is ASCII but UrlEncode won’t do that.

No comments: