daily pastebin goal
76%
SHARE
TWEET

UriEncoder

tolikpunkoff Sep 11th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static class UriEncoder
  2.     {
  3.         /// <summary>
  4.         /// UrlEncodes a string without the requirement for System.Web
  5.         /// </summary>
  6.         /// <param name="String"></param>
  7.         /// <returns></returns>
  8.         // [Obsolete("Use System.Uri.EscapeDataString instead")]
  9.         public static string UrlEncode(string text)
  10.         {
  11.             // Sytem.Uri provides reliable parsing
  12.             return System.Uri.EscapeDataString(text);
  13.         }
  14.  
  15.         /// <summary>
  16.         /// UrlDecodes a string without requiring System.Web
  17.         /// </summary>
  18.         /// <param name="text">String to decode.</param>
  19.         /// <returns>decoded string</returns>
  20.         public static string UrlDecode(string text)
  21.         {
  22.             // pre-process for + sign space formatting since System.Uri doesn't handle it
  23.             // plus literals are encoded as %2b normally so this should be safe
  24.             text = text.Replace("+", " ");
  25.             return System.Uri.UnescapeDataString(text);
  26.         }
  27.  
  28.         /// <summary>
  29.         /// Retrieves a value by key from a UrlEncoded string.
  30.         /// </summary>
  31.         /// <param name="urlEncoded">UrlEncoded String</param>
  32.         /// <param name="key">Key to retrieve value for</param>
  33.         /// <returns>returns the value or "" if the key is not found or the value is blank</returns>
  34.         public static string GetUrlEncodedKey(string urlEncoded, string key)
  35.         {
  36.             urlEncoded = "&" + urlEncoded + "&";
  37.  
  38.             int Index = urlEncoded.IndexOf("&" + key + "=", StringComparison.OrdinalIgnoreCase);
  39.             if (Index < 0)
  40.                 return "";
  41.  
  42.             int lnStart = Index + 2 + key.Length;
  43.  
  44.             int Index2 = urlEncoded.IndexOf("&", lnStart);
  45.             if (Index2 < 0)
  46.                 return "";
  47.  
  48.             return UrlDecode(urlEncoded.Substring(lnStart, Index2 - lnStart));
  49.         }
  50.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top