/// <summary> /// Join the name-value pairs into a string seperated with ampersands. /// Each name and value is first RFC 3986 encoded and values are separated /// from names with equal signs. /// </summary> /// <param name="values">The name value collection to encode and join</param> /// <returns>An RFC 3986 compliant string</returns> public static string EncodeAndJoin(NameValueCollection values) { if (values == null) { return(string.Empty); } StringBuilder enc = new StringBuilder(); bool first = true; foreach (string key in values.Keys) { string encKey = Rfc3986.Encode(key); foreach (string value in values.GetValues(key)) { if (!first) { enc.Append("&"); } else { first = false; } enc.Append(encKey).Append("=").Append(Rfc3986.Encode(value)); } } return(enc.ToString()); }
/** Construct a &-separated list of the given values, percentEncoded. */ public static String percentEncode(List <string> values) { StringBuilder p = new StringBuilder(); foreach (string v in values) { if (p.Length > 0) { p.Append("&"); } p.Append(Rfc3986.Encode(ToString(v))); } return(p.ToString()); }
/** * Construct a WWW-Authenticate or Authentication header value, containing * the given realm plus all the parameters whose names begin with "oauth_". */ public String getAuthorizationHeader(String realm) { StringBuilder into = new StringBuilder(AUTH_SCHEME); into.Append(" realm=\"").Append(Rfc3986.Encode(realm)).Append('"'); beforeGetParameter(); if (parameters != null) { foreach (var parameter in parameters) { String name = ToString(parameter.Key); if (name.StartsWith("oauth_")) { into.Append(", "); into.Append(Rfc3986.Encode(name)).Append("=\"") .Append( Rfc3986.Encode(ToString(parameter .Value))).Append('"'); } } } return(into.ToString()); }
/** * Write a form-urlencoded document into the given stream, containing the * given sequence of name/value pairs. */ public static void formEncode(List <Parameter> parameters, Stream into) { if (parameters != null) { bool first = true; byte[] towrite; foreach (Parameter parameter in parameters) { if (first) { first = false; } else { towrite = Encoding.UTF8.GetBytes("&"); into.Write(towrite, 0, towrite.Length); } towrite = Encoding.UTF8.GetBytes(Rfc3986.Encode(ToString(parameter.Key)) + "=" + Rfc3986.Encode(ToString(parameter.Value))); into.Write(towrite, 0, towrite.Length); } } }
public override String ToString() { return(Rfc3986.Encode(Key) + '=' + Rfc3986.Encode(Value)); }