Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Encodes an URL
- /// </summary>
- /// <param name="str">Input URL</param>
- /// <returns>Returns an encoded URL</returns>
- public static string Encode(string str)
- {
- StringBuilder b = new StringBuilder();
- foreach (Char c in str)
- {
- int decValue = (int)c;
- //Exclude Unreserved Characters (RFC 3986)
- if ((decValue >= 48 && decValue <= 57) || // 0 - 9
- (decValue >= 65 && decValue <= 90) || // A - Z
- (decValue >= 97 && decValue <= 122) || // a - z
- decValue == 45 || // -
- decValue == 95 || // _
- decValue == 46 || // .
- decValue == 126) // ~
- {
- b.Append(c);
- }
- else
- {
- b.Append("%" + decValue.ToString("X"));
- }
- }
- return b.ToString();
- }
Advertisement
Add Comment
Please, Sign In to add comment