using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace VOL.Core.Utilities { public class HttpManager { public static async Task HttpPostAsync(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary headers = null) { using (var client = new HttpClient()) { client.Timeout = TimeSpan.FromSeconds(timeOut); if (headers != null) { foreach (var header in headers) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } HttpContent content = null; if (!string.IsNullOrEmpty(postData)) { content = new StringContent(postData, Encoding.UTF8, contentType); } try { var response = await client.PostAsync(url, content); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStringAsync(); } catch (Exception ex) { return ex.Message; } } } public static async Task HttpGetAsync(string url, Dictionary headers = null) { using (var client = new HttpClient()) { if (headers != null) { foreach (var header in headers) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } try { var response = await client.GetAsync(url); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStringAsync(); } catch (Exception ex) { return ex.Message; } } } } }