using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Windows.Web.Http; namespace KdanCommon.Helpers { public static class HttpClientHelper { public static KeyValuePair CreateQuery(string param, string value) { return new KeyValuePair(param, value); } public static async Task GetRequest(this HttpClient httpClient, Uri requestUri, List> querys) { requestUri = new Uri(requestUri.AbsoluteUri + querys.HttpQueryString()); var result = new HttpResponseMessage(); try { result = await httpClient.GetAsync(requestUri); } catch (Exception ex) { var ii = ex.Data; result.StatusCode = HttpStatusCode.RequestTimeout; } return result; } public static async Task PostRequest(this HttpClient httpClient, Uri requestUri, List> querys) { var content = new HttpFormUrlEncodedContent(querys); var result = new HttpResponseMessage(); try { result = await httpClient.PostAsync(requestUri, content); } catch (Exception ex) { var ii = ex.Data; result.StatusCode = HttpStatusCode.RequestTimeout; } return result; } public static async Task PutRequest(this HttpClient httpClient, Uri requestUri, List> querys) { var content = new HttpFormUrlEncodedContent(querys); var result = await httpClient.PutAsync(requestUri, content); return result; } public static async Task DeleteRequest(this HttpClient httpClient, Uri requestUri, List> querys) { // requestUri = new Uri(requestUri.AbsoluteUri + HttpQueryString(querys)); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Delete, requestUri); request.Content = new HttpFormUrlEncodedContent(querys); var result = await httpClient.SendRequestAsync(request); return result; } public static string HttpQueryString(this List> querys) { var queryString = "?"; foreach (var q in querys) { queryString += q.Key + "=" + q.Value + "&"; } queryString = queryString.Remove(queryString.Length - 1); return queryString; } } }