Url Encode 3
using System; using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Net; using System.Text; using System.Text.RegularExpressions; using System.Web; using System.Xml; using System.Xml.Serialization; using System.Collections.Generic; using System.Drawing; public static class Utility { const string UnreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~"; /// <summary> /// This is a different Url Encode implementation since the default .NET one outputs the percent encoding in lower case. /// While this is not a problem with the percent encoding spec, it is used in upper case throughout OAuth /// </summary> /// <param name="value">The value to Url encode</param> /// <returns>Returns a Url encoded string</returns> public static string UrlEncode(string value) { var result = new StringBuilder(); foreach (char symbol in value) { if (UnreservedChars.IndexOf(symbol) != -1) { result.Append(symbol); } else { //some symbols produce > 2 char values so the system urlencoder must be used to get the correct data if (String.Format("{0:X2}", (int)symbol).Length > 3) { // ReSharper disable PossibleNullReferenceException result.Append(HttpUtility.UrlEncode(value.Substring(value.IndexOf(symbol), 1)).ToUpper()); // ReSharper restore PossibleNullReferenceException } else { result.Append('%' + String.Format("{0:X2}", (int)symbol)); } } } return result.ToString(); } }