Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class UriEncoder
- {
- /// <summary>
- /// UrlEncodes a string without the requirement for System.Web
- /// </summary>
- /// <param name="String"></param>
- /// <returns></returns>
- // [Obsolete("Use System.Uri.EscapeDataString instead")]
- public static string UrlEncode(string text)
- {
- // Sytem.Uri provides reliable parsing
- return System.Uri.EscapeDataString(text);
- }
- /// <summary>
- /// UrlDecodes a string without requiring System.Web
- /// </summary>
- /// <param name="text">String to decode.</param>
- /// <returns>decoded string</returns>
- public static string UrlDecode(string text)
- {
- // pre-process for + sign space formatting since System.Uri doesn't handle it
- // plus literals are encoded as %2b normally so this should be safe
- text = text.Replace("+", " ");
- return System.Uri.UnescapeDataString(text);
- }
- /// <summary>
- /// Retrieves a value by key from a UrlEncoded string.
- /// </summary>
- /// <param name="urlEncoded">UrlEncoded String</param>
- /// <param name="key">Key to retrieve value for</param>
- /// <returns>returns the value or "" if the key is not found or the value is blank</returns>
- public static string GetUrlEncodedKey(string urlEncoded, string key)
- {
- urlEncoded = "&" + urlEncoded + "&";
- int Index = urlEncoded.IndexOf("&" + key + "=", StringComparison.OrdinalIgnoreCase);
- if (Index < 0)
- return "";
- int lnStart = Index + 2 + key.Length;
- int Index2 = urlEncoded.IndexOf("&", lnStart);
- if (Index2 < 0)
- return "";
- return UrlDecode(urlEncoded.Substring(lnStart, Index2 - lnStart));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement