/// <summary> /// Makes a POST request to the specified <paramref name="url"/>. /// </summary> /// <param name="url">The URL of the request.</param> /// <param name="queryString">The query string of the request.</param> /// <param name="postData">The POST data of the request.</param> /// <returns>An instance of <see cref="IHttpResponse"/> representing the response.</returns> public virtual IHttpResponse Post(string url, IHttpQueryString queryString, IHttpPostData postData) { if (string.IsNullOrWhiteSpace(url)) { throw new ArgumentNullException(nameof(url)); } return(DoHttpRequest(HttpMethod.Post, url, queryString, postData)); }
/// <summary> /// Sets the <see cref="IHttpRequest.PostData"/> property of <paramref name="request"/>. /// </summary> /// <typeparam name="T">The type of the request - eg. <see cref="HttpRequest"/>.</typeparam> /// <param name="request">The request.</param> /// <param name="postData">The new POST data of the request.</param> /// <returns>The specified <paramref name="request"/> as an instance of <typeparamref name="T"/>.</returns> public static T SetPostData <T>(this T request, IHttpPostData postData) where T : IHttpRequest { if (request != null) { request.PostData = postData; } return(request); }
/// <summary> /// Generates the the string of parameters used for making the signature. /// </summary> /// <param name="queryString">Values representing the query string.</param> /// <param name="body">Values representing the POST body.</param> /// <returns>The generated parameter string.</returns> public virtual string GenerateParameterString(IHttpQueryString queryString, IHttpPostData body) { // The parameters must be alphabetically sorted SortedDictionary <string, string> sorted = new SortedDictionary <string, string>(); // Add parameters from the query string if (queryString != null) { foreach (string key in queryString.Keys) { //if (key.StartsWith("oauth_")) continue; sorted.Add(Uri.EscapeDataString(key), Uri.EscapeDataString(queryString[key])); } } // Add parameters from the POST data if (body != null) { foreach (string key in body.Keys) { //if (key.StartsWith("oauth_")) continue; sorted.Add(Uri.EscapeDataString(key), Uri.EscapeDataString(body[key])); } } // Add OAuth values if (!String.IsNullOrEmpty(Callback)) { sorted.Add("oauth_callback", Uri.EscapeDataString(Callback)); } sorted.Add("oauth_consumer_key", Uri.EscapeDataString(ConsumerKey)); sorted.Add("oauth_nonce", Uri.EscapeDataString(Nonce)); sorted.Add("oauth_signature_method", "HMAC-SHA1"); sorted.Add("oauth_timestamp", Uri.EscapeDataString(Timestamp)); if (!String.IsNullOrEmpty(Token)) { sorted.Add("oauth_token", Uri.EscapeDataString(Token)); } sorted.Add("oauth_version", Uri.EscapeDataString(Version)); // Merge all parameters return(sorted.Aggregate("", (current, pair) => current + ("&" + pair.Key + "=" + pair.Value)).Substring(1)); }
/// <summary> /// Generate the signature. /// </summary> /// <param name="method">The method for the HTTP request.</param> /// <param name="url">The URL of the request.</param> /// <param name="queryString">The query string.</param> /// <param name="body">The POST data.</param> /// <returns>Returns the generated signature.</returns> public virtual string GenerateSignature(SocialHttpMethod method, string url, IHttpQueryString queryString, IHttpPostData body) { HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(GenerateSignatureKey())); return Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(GenerateSignatureValue(method, url, queryString, body)))); }
/// <summary> /// Makes a POST request to the specified <paramref name="url"/>. /// </summary> /// <param name="url">The base URL of the request (no query string).</param> /// <param name="postData">The POST data.</param> /// <returns>An instance of <see cref="IHttpResponse"/> representing the raw response.</returns> public virtual IHttpResponse Post(string url, IHttpPostData postData) { return(DoHttpRequest(HttpMethod.Post, url, null, postData)); }
/// <summary> /// Makes a HTTP request to the underlying API based on the specified parameters. /// </summary> /// <param name="method">The HTTP method of the request.</param> /// <param name="url">The base URL of the request (no query string).</param> /// <param name="queryString">The query string.</param> /// <param name="postData">The POST data.</param> /// <returns>Returns an instance of <see cref="SocialHttpResponse"/> representing the raw response.</returns> public virtual SocialHttpResponse DoHttpRequest(SocialHttpMethod method, string url, IHttpQueryString queryString, IHttpPostData postData) { // Some input validation if (String.IsNullOrWhiteSpace(url)) throw new ArgumentNullException("url"); if (queryString == null) queryString = new SocialHttpQueryString(); if (postData == null) postData = new SocialHttpPostData(); // Initialize the request SocialHttpRequest request = new SocialHttpRequest { Method = method, Url = url, QueryString = queryString, PostData = postData }; PrepareHttpRequest(request); // Make the call to the URL return request.GetResponse(); }
/// <summary> /// Makes a HTTP request to the underlying API based on the specified parameters. /// </summary> /// <param name="method">The HTTP method of the request.</param> /// <param name="url">The base URL of the request (no query string).</param> /// <param name="postData">The POST data.</param> /// <returns>Returns an instance of <see cref="SocialHttpResponse"/> representing the raw response.</returns> public virtual SocialHttpResponse DoHttpRequest(SocialHttpMethod method, string url, IHttpPostData postData) { return DoHttpRequest(method, url, null, postData); }
/// <summary> /// Makes a POST request to the specified <code>url</code>. /// </summary> /// <param name="url">The URL of the request.</param> /// <param name="queryString">The query string of the request.</param> /// <param name="postData">The POST data of the request.</param> /// <returns>Returns an instance of <see cref="SocialHttpResponse"/> representing the response.</returns> public virtual SocialHttpResponse DoHttpPostRequest(string url, IHttpQueryString queryString, IHttpPostData postData) { if (String.IsNullOrWhiteSpace(url)) throw new ArgumentNullException("url"); return DoHttpRequest(SocialHttpMethod.Post, url, queryString, postData); }
/// <summary> /// Generates the signature. /// </summary> /// <param name="method">The method for the HTTP request.</param> /// <param name="url">The URL of the request.</param> /// <param name="queryString">The query string.</param> /// <param name="body">The POST data.</param> /// <returns>Returns the generated signature.</returns> public virtual string GenerateSignature(SocialHttpMethod method, string url, IHttpQueryString queryString, IHttpPostData body) { HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(GenerateSignatureKey())); return(Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(GenerateSignatureValue(method, url, queryString, body))))); }
/// <summary> /// Generates the string value used for making the signature. /// </summary> /// <param name="method">The method for the HTTP request.</param> /// <param name="url">The URL of the request.</param> /// <param name="queryString">The query string.</param> /// <param name="body">The POST data.</param> /// <returns>The generated signature value.</returns> public virtual string GenerateSignatureValue(SocialHttpMethod method, string url, IHttpQueryString queryString, IHttpPostData body) { return(String.Format( "{0}&{1}&{2}", method.ToString().ToUpper(), Uri.EscapeDataString(url.Split('#')[0].Split('?')[0]), Uri.EscapeDataString(GenerateParameterString(queryString, body)) )); }
/// <summary> /// Makes a PATCH request to the specified <paramref name="url"/>. /// </summary> /// <param name="url">The base URL of the request (no query string).</param> /// <param name="postData">The PATCH data.</param> /// <returns>An instance of <see cref="IHttpResponse"/> representing the raw response.</returns> public static IHttpResponse Patch(string url, IHttpPostData postData) { return(DoHttpRequest(HttpMethod.Patch, url, null, postData)); }
/// <summary> /// Generates the the string of parameters used for making the signature. /// </summary> /// <param name="queryString">Values representing the query string.</param> /// <param name="body">Values representing the POST body.</param> /// <returns>Returns the generated parameter string.</returns> public virtual string GenerateParameterString(IHttpQueryString queryString, IHttpPostData body) { // The parameters must be alphabetically sorted SortedDictionary<string, string> sorted = new SortedDictionary<string, string>(); // Add parameters from the query string if (queryString != null) { foreach (string key in queryString.Keys) { //if (key.StartsWith("oauth_")) continue; sorted.Add(Uri.EscapeDataString(key), Uri.EscapeDataString(queryString[key])); } } // Add parameters from the POST data if (body != null) { foreach (string key in body.Keys) { //if (key.StartsWith("oauth_")) continue; sorted.Add(Uri.EscapeDataString(key), Uri.EscapeDataString(body[key])); } } // Add OAuth values if (!String.IsNullOrEmpty(Callback)) sorted.Add("oauth_callback", Uri.EscapeDataString(Callback)); sorted.Add("oauth_consumer_key", Uri.EscapeDataString(ConsumerKey)); sorted.Add("oauth_nonce", Uri.EscapeDataString(Nonce)); sorted.Add("oauth_signature_method", "HMAC-SHA1"); sorted.Add("oauth_timestamp", Uri.EscapeDataString(Timestamp)); if (!String.IsNullOrEmpty(Token)) sorted.Add("oauth_token", Uri.EscapeDataString(Token)); sorted.Add("oauth_version", Uri.EscapeDataString(Version)); // Merge all parameters return sorted.Aggregate("", (current, pair) => current + ("&" + pair.Key + "=" + pair.Value)).Substring(1); }
/// <summary> /// Generates the string value used for making the signature. /// </summary> /// <param name="method">The method for the HTTP request.</param> /// <param name="url">The URL of the request.</param> /// <param name="queryString">The query string.</param> /// <param name="body">The POST data.</param> /// <returns>Returns the generated signature value.</returns> public virtual string GenerateSignatureValue(SocialHttpMethod method, string url, IHttpQueryString queryString, IHttpPostData body) { return String.Format( "{0}&{1}&{2}", method.ToString().ToUpper(), Uri.EscapeDataString(url.Split('#')[0].Split('?')[0]), Uri.EscapeDataString(GenerateParameterString(queryString, body)) ); }
/// <summary> /// Makes a HTTP request using the specified <paramref name="url"/> and <paramref name="method"/>. /// </summary> /// <param name="url">The URL of the request.</param> /// <param name="method">The HTTP method of the request.</param> /// <param name="queryString">The query string of the request.</param> /// <param name="postData">The POST data of the request.</param> /// <returns>An instance of <see cref="SocialHttpResponse"/> representing the response.</returns> private static SocialHttpResponse DoHttpRequest(SocialHttpMethod method, string url, IHttpQueryString queryString, IHttpPostData postData) { // Initialize the request SocialHttpRequest request = new SocialHttpRequest { Url = url, Method = method, QueryString = queryString, PostData = postData }; // Make the call to the URL return(request.GetResponse()); }
/// <summary> /// Makes a HTTP request to the underlying API based on the specified parameters. /// </summary> /// <param name="method">The HTTP method of the request.</param> /// <param name="url">The base URL of the request (no query string).</param> /// <param name="postData">The POST data.</param> /// <returns>An instance of <see cref="SocialHttpResponse"/> representing the raw response.</returns> public static SocialHttpResponse DoHttpRequest(SocialHttpMethod method, string url, IHttpPostData postData) { return(DoHttpRequest(method, url, null, postData)); }
/// <summary> /// Makes a PATCH request to the specified <paramref name="url"/>. /// </summary> /// <param name="url">The URL of the request.</param> /// <param name="queryString">The query string of the request.</param> /// <param name="postData">The PATCH data of the request.</param> /// <returns>An instance of <see cref="SocialHttpResponse"/> representing the response.</returns> public static SocialHttpResponse DoHttpPatchRequest(string url, IHttpQueryString queryString, IHttpPostData postData) { if (String.IsNullOrWhiteSpace(url)) { throw new ArgumentNullException(nameof(url)); } return(DoHttpRequest(SocialHttpMethod.Patch, url, queryString, postData)); }
/// <summary> /// Makes a HTTP request using the specified <code>url</code> and <code>method</code>. /// </summary> /// <param name="url">The URL of the request.</param> /// <param name="method">The HTTP method of the request.</param> /// <param name="queryString">The query string of the request.</param> /// <param name="postData">The POST data of the request.</param> /// <returns>Returns an instance of <see cref="SocialHttpResponse"/> representing the response.</returns> private static SocialHttpResponse DoHttpRequest(SocialHttpMethod method, string url, IHttpQueryString queryString, IHttpPostData postData) { // Initialize the request SocialHttpRequest request = new SocialHttpRequest { Url = url, Method = method, QueryString = queryString, PostData = postData }; // Make the call to the URL return request.GetResponse(); }
/// <summary> /// Makes a POST request to the specified <code>url</code>. /// </summary> /// <param name="url">The base URL of the request (no query string).</param> /// <param name="postData">The POST data.</param> /// <returns>Returns an instance of <see cref="SocialHttpResponse"/> representing the raw response.</returns> public static SocialHttpResponse DoHttpPostRequest(string url, IHttpPostData postData) { return DoHttpRequest(SocialHttpMethod.Post, url, null, postData); }