Skip to content

Instantly share code, notes, and snippets.

@abfo
Created September 30, 2018 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abfo/f820f0fd869828ea27eccb4d738c6397 to your computer and use it in GitHub Desktop.
Save abfo/f820f0fd869828ea27eccb4d738c6397 to your computer and use it in GitHub Desktop.
public static string OAuthUrlEncode(string s)
{
if (string.IsNullOrEmpty(s))
{
return string.Empty;
}
else
{
StringBuilder sb = new StringBuilder(s.Length);
for (int i = 0; i < s.Length; i++)
{
if (NoEncodeChars.IndexOf(s[i]) == -1)
{
// character needs encoding
byte[] characterBytes = Encoding.UTF8.GetBytes(s[i].ToString());
for (int b = 0; b < characterBytes.Length; b++)
{
sb.AppendFormat(CultureInfo.InvariantCulture,
"%{0:X2}",
characterBytes[b]);
}
}
else
{
// character is allowed
sb.Append(s[i]);
}
}
return sb.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment