public static async Task <ResponseHttp> GetAsync(string url, Dictionary <string, string> headers = null) { HttpClient client = null; HttpResponseMessage response = null; url = url.GetValidUri(); if (url == null) { return(null); } try { client = new HttpClient(); AddDefaultRequestHeaders(client, headers); response = await client.GetAsync(url); ResponseHttp http = new ResponseHttp(response); http.SetBody(response); return(http); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { client.Dispose(); response?.Dispose(); } return(null); }
public static ResponseHttp Patch(string url, HttpContent content, Dictionary <string, string> headers = null) { url = url.GetValidUri(); if (url == null) { return(null); } HttpResponseMessage response = null; HttpRequestMessage request = null; HttpClient client = null; try { client = new HttpClient(); request = new HttpRequestMessage(new HttpMethod("PATCH"), url) { Content = content }; AddHeaders(client, content, headers); response = client.SendAsync(request).Result; ResponseHttp http = new ResponseHttp(response); http.SetBody(response); return(http); } catch (Exception) { return(null); } finally { client.Dispose(); response?.Dispose(); request.Dispose(); } }
public static async Task <ResponseHttp> PostAsync(string url, HttpContent data, Dictionary <string, string> headers = null) { HttpResponseMessage response = null; HttpClient client = null; try { client = new HttpClient(); AddHeaders(client, data, headers); response = await client.PostAsync(url, data); ResponseHttp http = new ResponseHttp(response); http.SetBody(response); return(http); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { client.Dispose(); response?.Dispose(); } return(null); }
public static async Task <ResponseHttp> Delete(string url, string data, Dictionary <string, string> headers = null) { StringContent content = new StringContent(data); ResponseHttp response = await Delete(url, content, headers); content.Dispose(); return(response); }
public static ResponseHttp Patch(string url, string data, Dictionary <string, string> headers = null) { StringContent content = new StringContent(data); ResponseHttp http = Patch(url, data, headers); content.Dispose(); return(http); }
public static async Task <ResponseHttp> PostAsync(string url, string data, Encoding encoding, string contentType, Dictionary <string, string> headers = null) { StringContent content = new StringContent(data, encoding, contentType); ResponseHttp http = await PostAsync(url, content, headers); content.Dispose(); return(http); }
public static async Task <ResponseHttp> Delete(string url, object data, Dictionary <string, string> headers = null) { string str = JsonConvert.SerializeObject(data, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }); StringContent content = new StringContent(str); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); ResponseHttp http = await Delete(url, content, headers); content.Dispose(); return(http); }
public static async Task <ResponseHttp> PostAsync(string url, object data, Dictionary <string, string> headers = null) { string str = JsonConvert.SerializeObject(data, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None, ContractResolver = Startup.DEFAULT_JSON_CONTRACT }); Console.WriteLine(str); StringContent content = new StringContent(str); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); ResponseHttp response = await PostAsync(url, content, headers); content.Dispose(); return(response); }
public static async Task <ResponseHttp> Delete(string url, HttpContent content, Dictionary <string, string> headers = null) { url = url.GetValidUri(); if (url == null) { return(null); } HttpClient client = null; HttpRequestMessage request = null; HttpResponseMessage response = null; try { client = new HttpClient(); AddHeaders(client, content, headers); request = new HttpRequestMessage(new HttpMethod("DELETE"), url) { Content = content }; response = await client.SendAsync(request); ResponseHttp http = new ResponseHttp(response); http.SetBody(response); return(http); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { request.Dispose(); client.Dispose(); response?.Dispose(); } return(null); }
public static ResponseHttp Put(string url, HttpContent content, Dictionary <string, string> headers = null) { HttpResponseMessage response = null; HttpClient client = null; try { client = new HttpClient(); AddHeaders(client, content, headers); response = client.PutAsync(url, content).Result; ResponseHttp http = new ResponseHttp(response); http.SetBody(response); return(http); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { client.Dispose(); response?.Dispose(); } return(null); }