private static string GetDataResponseNormal(Flickr flickr, string baseUrl, Dictionary <string, string> parameters) { string method = flickr.CurrentService == SupportedService.Zooomr ? "GET" : "POST"; string data = String.Empty; foreach (var k in parameters) { data += k.Key + "=" + UtilityMethods.EscapeDataString(k.Value) + "&"; } if (method == "GET" && data.Length > 2000) { method = "POST"; } if (method == "GET") { return(DownloadData(method, baseUrl + "?" + data, null, null, null)); } else { return(DownloadData(method, baseUrl, data, PostContentType, null)); } }
private static void GetDataResponseNormalAsync(Flickr flickr, string baseUrl, Dictionary <string, string> parameters, Action <FlickrResult <string> > callback) { var method = flickr.CurrentService == SupportedService.Zooomr ? "GET" : "POST"; var data = String.Empty; foreach (var k in parameters) { data += k.Key + "=" + UtilityMethods.EscapeDataString(k.Value) + "&"; } if (method == "GET" && data.Length > 2000) { method = "POST"; } if (method == "GET") { DownloadDataAsync(method, baseUrl + "?" + data, null, null, null, callback); } else { DownloadDataAsync(method, baseUrl, data, PostContentType, null, callback); } }
/// <summary> /// Escapes a string for use with OAuth. /// </summary> /// <remarks>The only valid characters are Alphanumerics and "-", "_", "." and "~". Everything else is hex encoded.</remarks> /// <param name="text">The text to escape.</param> /// <returns>The escaped string.</returns> public static string EscapeOAuthString(string text) { string value = text; value = UtilityMethods.EscapeDataString(value).Replace("+", "%20"); // UrlEncode escapes with lowercase characters (e.g. %2f) but oAuth needs %2F value = Regex.Replace(value, "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper()); // these characters are not escaped by UrlEncode() but needed to be escaped value = value.Replace("(", "%28").Replace(")", "%29").Replace("$", "%24").Replace("!", "%21").Replace( "*", "%2A").Replace("'", "%27"); // these characters are escaped by UrlEncode() but will fail if unescaped! value = value.Replace("%7E", "~"); return(value); }
/// <summary> /// Calculates for form encoded POST data to be included in the body of an OAuth call. /// </summary> /// <remarks>This will include all non-OAuth parameters. The OAuth parameter will be included in the Authentication header.</remarks> /// <param name="parameters"></param> /// <returns></returns> public static string OAuthCalculatePostData(Dictionary <string, string> parameters) { string data = String.Empty; foreach (KeyValuePair <string, string> pair in parameters) { // Silverlight < 5 doesn't support modification of the Authorization header, so all data must be sent in post body. #if SILVERLIGHT data += pair.Key + "=" + UtilityMethods.EscapeOAuthString(pair.Value) + "&"; #else if (!pair.Key.StartsWith("oauth")) { data += pair.Key + "=" + UtilityMethods.EscapeDataString(pair.Value) + "&"; } #endif } return(data); }
/// <summary> /// Calculates the Flickr method cal URL based on the passed in parameters, and also generates the signature if required. /// </summary> /// <param name="parameters">A Dictionary containing a list of parameters to add to the method call.</param> /// <param name="includeSignature">Boolean use to decide whether to generate the api call signature as well.</param> /// <returns>The <see cref="Uri"/> for the method call.</returns> public string CalculateUri(Dictionary <string, string> parameters, bool includeSignature) { if (includeSignature) { string signature = CalculateAuthSignature(parameters); parameters.Add("api_sig", signature); } var url = new StringBuilder(); url.Append("?"); foreach (KeyValuePair <string, string> pair in parameters) { var escapedValue = UtilityMethods.EscapeDataString(pair.Value ?? ""); url.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "{0}={1}&", pair.Key, escapedValue); } return(BaseUri.AbsoluteUri + url.ToString()); }