/// <summary> /// Makes an HTTP with option JSON data serialized from an object /// and parses the result from JSON back into an object. /// Assumes that the service returns a JSON response and that /// any data sent is json. /// </summary> /// <typeparam name="TResultType">The type of the object returned</typeparam> /// <param name="settings"><see cref="HttpRequestSettings"/> /// Configuration object for the HTTP request made to the server. /// </param> /// <returns>deserialized value/object from returned JSON data</returns> public static async Task<TResultType> JsonRequestAsync<TResultType>(HttpRequestSettings settings) { var client = new HttpUtilsWebClient(settings); client.Headers.Add("Accept", "application/json"); string jsonResult; if (settings.HttpVerb == "GET") jsonResult = await client.DownloadStringTaskAsync(settings.Url); else { if (!string.IsNullOrEmpty(settings.ContentType)) client.Headers["Content-type"] = settings.ContentType; else client.Headers["Content-type"] = "application/json"; if (!settings.IsRawData) settings.CapturedRequestContent = JsonSerializationUtils.Serialize(settings.Content, throwExceptions: true); else settings.CapturedRequestContent = settings.Content as string; jsonResult = await client.UploadStringTaskAsync(settings.Url, settings.HttpVerb, settings.CapturedRequestContent); if (jsonResult == null) return default(TResultType); } settings.CapturedResponseContent = jsonResult; settings.Response = client.Response; return (TResultType)JsonSerializationUtils.Deserialize(jsonResult, typeof(TResultType), true); }
/// <summary> /// Retrieves and Http request and returns data as a string. /// </summary> /// <param name="settings">Pass HTTP request configuration parameters object to set the URL, Verb, Headers, content and more</param> /// <returns>string of HTTP response</returns> public static async Task<string> HttpRequestStringAsync(HttpRequestSettings settings) { var client = new HttpUtilsWebClient(settings); if (settings.HttpVerb == "GET") settings.CapturedResponseContent = await client.DownloadStringTaskAsync(new Uri(settings.Url)); else { if (!string.IsNullOrEmpty(settings.ContentType)) client.Headers["Content-type"] = settings.ContentType; if (settings.Content is string) { settings.CapturedRequestContent = settings.Content as string; settings.CapturedResponseContent = await client.UploadStringTaskAsync(settings.Url, settings.HttpVerb, settings.CapturedRequestContent); } else if (settings.Content is byte[]) { settings.ResponseByteData = await client.UploadDataTaskAsync(settings.Url, settings.Content as byte[]); settings.CapturedResponseContent = Encoding.UTF8.GetString(settings.ResponseByteData); } else throw new ArgumentException("Data must be either string or byte[]."); } settings.Response = client.Response; return settings.CapturedResponseContent; }
/// <summary> /// Retrieves and Http request and returns data as a string. /// </summary> /// <param name="settings">Pass HTTP request configuration parameters object to set the URL, Verb, Headers, content and more</param> /// <returns>string of HTTP response</returns> public static async Task <string> HttpRequestStringAsync(HttpRequestSettings settings) { var client = new HttpUtilsWebClient(); if (settings.Credentials != null) { client.Credentials = settings.Credentials; } if (settings.Proxy != null) { client.Proxy = settings.Proxy; } if (settings.Headers != null) { foreach (var header in settings.Headers) { client.Headers[header.Key] = header.Value; } } if (settings.HttpVerb == "GET") { settings.CapturedResponseContent = await client.DownloadStringTaskAsync(new Uri(settings.Url)); } else { if (!string.IsNullOrEmpty(settings.ContentType)) { client.Headers["Content-type"] = settings.ContentType; } if (settings.Content is string) { settings.CapturedRequestContent = settings.Content as string; settings.CapturedResponseContent = await client.UploadStringTaskAsync(settings.Url, settings.HttpVerb, settings.CapturedRequestContent); } else if (settings.Content is byte[]) { settings.ResponseByteData = await client.UploadDataTaskAsync(settings.Url, settings.Content as byte[]); settings.CapturedResponseContent = Encoding.UTF8.GetString(settings.ResponseByteData); } else { throw new ArgumentException("Data must be either string or byte[]."); } } settings.Response = client.Response; return(settings.CapturedResponseContent); }
/// <summary> /// Makes an HTTP with option JSON data serialized from an object /// and parses the result from JSON back into an object. /// Assumes that the service returns a JSON response and that /// any data sent is json. /// </summary> /// <typeparam name="TResultType">The type of the object returned</typeparam> /// <param name="settings"><see cref="HttpRequestSettings"/> /// Configuration object for the HTTP request made to the server. /// </param> /// <returns>deserialized value/object from returned JSON data</returns> public static async Task <TResultType> JsonRequestAsync <TResultType>(HttpRequestSettings settings) { var client = new HttpUtilsWebClient(); if (settings.Credentials != null) { client.Credentials = settings.Credentials; } if (settings.Proxy != null) { client.Proxy = settings.Proxy; } client.Headers.Add("Accept", "application/json"); if (settings.Headers != null) { foreach (var header in settings.Headers) { client.Headers[header.Key] = header.Value; } } string jsonResult; if (settings.HttpVerb == "GET") { jsonResult = await client.DownloadStringTaskAsync(settings.Url); } else { if (!string.IsNullOrEmpty(settings.ContentType)) { client.Headers["Content-type"] = settings.ContentType; } else { client.Headers["Content-type"] = "application/json"; } if (!settings.IsRawData) { settings.CapturedRequestContent = JsonSerializationUtils.Serialize(settings.Content, throwExceptions: true); } else { settings.CapturedRequestContent = settings.Content as string; } jsonResult = await client.UploadStringTaskAsync(settings.Url, settings.HttpVerb, settings.CapturedRequestContent); if (jsonResult == null) { return(default(TResultType)); } } settings.CapturedResponseContent = jsonResult; settings.Response = client.Response; return((TResultType)JsonSerializationUtils.Deserialize(jsonResult, typeof(TResultType), true)); }