123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- 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<string, string> CreateQuery(string param, string value)
- {
- return new KeyValuePair<string, string>(param, value);
- }
- public static async Task<HttpResponseMessage> GetRequest(this HttpClient httpClient, Uri requestUri, List<KeyValuePair<string, string>> 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<HttpResponseMessage> PostRequest(this HttpClient httpClient, Uri requestUri, List<KeyValuePair<string, string>> 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<HttpResponseMessage> PutRequest(this HttpClient httpClient, Uri requestUri, List<KeyValuePair<string, string>> querys)
- {
- var content = new HttpFormUrlEncodedContent(querys);
- var result = await httpClient.PutAsync(requestUri, content);
- return result;
- }
- public static async Task<HttpResponseMessage> DeleteRequest(this HttpClient httpClient, Uri requestUri, List<KeyValuePair<string, string>> 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<KeyValuePair<string, string>> querys)
- {
- var queryString = "?";
- foreach (var q in querys)
- {
- queryString += q.Key + "=" + q.Value + "&";
- }
- queryString = queryString.Remove(queryString.Length - 1);
- return queryString;
- }
- }
- }
|