Url Encode 2
using System; using System.IO; using System.Collections; using System.Text; using System.Text.RegularExpressions; using System.Xml.Serialization; namespace FlickrNet { /// <summary> /// Internal class providing certain utility functions to other classes. /// </summary> internal sealed class Utils { private Utils() { } #if !WindowsCE internal static string UrlEncode(string oldString) { if (oldString == null) return null; string a = System.Web.HttpUtility.UrlEncode(oldString); a = a.Replace("&", "%26"); a = a.Replace("=", "%3D"); a = a.Replace(" ", "%20"); return a; } #else internal static string UrlEncode(string oldString) { if (oldString == null) return String.Empty; StringBuilder sb = new StringBuilder(oldString.Length * 2); Regex reg = new Regex("[a-zA-Z0-9$-_.+!*'(),]"); foreach (char c in oldString) { if (reg.IsMatch(c.ToString())) { sb.Append(c); } else { sb.Append(ToHex(c)); } } return sb.ToString(); } private static string ToHex(char c) { return ((int)c).ToString("X"); } #endif } }