Skip to content

Instantly share code, notes, and snippets.

@agocs
Last active December 16, 2015 14:08
Show Gist options
  • Save agocs/5446168 to your computer and use it in GitHub Desktop.
Save agocs/5446168 to your computer and use it in GitHub Desktop.
How to URL Encode a string in C#
private string urlencode(string input_url)
{
StringBuilder b = new StringBuilder(100);
foreach (char c in input_url)
{
b.Append(replace(c));
}
return b.ToString();
}
private string replace(char c)
{
switch (c)
{
case '~':
return "%7E";
case '!':
return "%21";
case '@':
return "%40";
case '#':
return "%23";
case '$':
return "%24";
case '%':
return "%25";
case '^':
return "%5E";
case '&':
return "%26";
case '*':
return "%2A";
case '(':
return "%28";
case ')':
return "%29";
case '{':
return "%7B";
case '}':
return "%7D";
case '[':
return "%5B";
case ']':
return "%5D";
case '=':
return "%3D";
case ':':
return "%3A";
case '/':
return "/";
case ',':
return "%2C";
case ';':
return "%3B";
case '?':
return "%3F";
case '+':
return "%2B";
case '\'':
return "%27";
case '"':
return "%22";
case '\\':
return "%5C";
default:
return c.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment