ApexLumia.Utils.UrlEncodeRelaxed C# (CSharp) Метод

UrlEncodeRelaxed() публичный статический Метод

URL encodes a string based on section 5.1 of the OAuth spec. Namely, percent encoding with [RFC3986], avoiding unreserved characters, upper-casing hexadecimal characters, and UTF-8 encoding for text value pairs.
The Uri.EscapeDataString method is supposed to take on RFC 3986 behavior if certain elements are present in a .config file. Even if this actually worked (which in my experiments it doesn't), we can't rely on every host actually having this configuration element present.
public static UrlEncodeRelaxed ( string value ) : string
value string The value to escape.
Результат string
        public static string UrlEncodeRelaxed(string value)
        {
            // Start with RFC 2396 escaping by calling the .NET method to do the work.
            // This MAY sometimes exhibit RFC 3986 behavior (according to the documentation).
            // If it does, the escaping we do that follows it will be a no-op since the
            // characters we search for to replace can't possibly exist in the string.
            if (value == "" || value == null) { return ""; }

            StringBuilder escaped = new StringBuilder(Uri.EscapeDataString(value));

            // Upgrade the escaping to RFC 3986, if necessary.
            for (int i = 0; i < UriRfc3986CharsToEscape.Length; i++)
            {
                string t = UriRfc3986CharsToEscape[i];
                escaped.Replace(t, UriRfc3968EscapedHex[i]);
            }

            // Return the fully-RFC3986-escaped string.
            return escaped.ToString();
        }

Usage Example

Пример #1
0
        /// <summary>
        /// Generate the OAuth HTTP Authorization header for twitter requests
        /// </summary>
        /// <param name="url">The URL the request will be made to.</param>
        /// <param name="method">The method of the request.</param>
        /// <param name="otherparams">Any other parameters specific to the request.</param>
        /// <returns></returns>
        private string generateAuthorizationHeader(string url, string method, Dictionary <string, string> otherparams)
        {
            var parameters = new Dictionary <string, string>();

            parameters.Add("oauth_version", "1.0");
            parameters.Add("oauth_consumer_key", _consumerkey);
            parameters.Add("oauth_nonce", Utils.uniqueAlphanumericString());
            parameters.Add("oauth_signature_method", "HMAC-SHA1");
            parameters.Add("oauth_timestamp", Utils.unix_timestamp().ToString());
            parameters.Add("oauth_token", _accesstoken);

            foreach (var item in otherparams)
            {
                parameters[item.Key] = item.Value;
            }

            parameters = parameters.OrderBy(x => x.Key).ToDictionary(v => v.Key, v => v.Value);


            string outputString = method + "&" + Utils.UrlEncodeRelaxed(url) + "&";

            foreach (var parameter in parameters)
            {
                outputString += Utils.UrlEncodeRelaxed(parameter.Key + "=" + parameter.Value + "&");
            }
            outputString = outputString.Substring(0, outputString.Length - 3);

            string signingKey = Utils.UrlEncodeRelaxed(_consumersecret) + "&" + Utils.UrlEncodeRelaxed(_accesssecret);

            byte[] hmacsha1  = Utils.hmacsha1ify(outputString, signingKey);
            string signature = Convert.ToBase64String(hmacsha1);

            string header = "OAuth ";

            header += "oauth_consumer_key=\"" + Utils.UrlEncodeRelaxed(parameters["oauth_consumer_key"]) + "\", ";
            header += "oauth_nonce=\"" + Utils.UrlEncodeRelaxed(parameters["oauth_nonce"]) + "\", ";
            header += "oauth_signature=\"" + Utils.UrlEncodeRelaxed(signature) + "\", ";
            header += "oauth_signature_method=\"HMAC-SHA1\", ";
            header += "oauth_timestamp=\"" + Utils.UrlEncodeRelaxed(parameters["oauth_timestamp"]) + "\", ";
            header += "oauth_token=\"" + Utils.UrlEncodeRelaxed(parameters["oauth_token"]) + "\", ";
            header += "oauth_version=\"1.0\"";

            return(header);
        }
All Usage Examples Of ApexLumia.Utils::UrlEncodeRelaxed