public async Task <HttpResponseBO <T> > GetAsync <T>(Uri baseUrl, string url, object param = null, CookieCollection requestCookies = null, string contentType = "application/json", bool clearDefaultRequestHeaders = false) { var cookies = new CookieContainer(); var handler = new HttpClientHandler { CookieContainer = cookies, ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true }; using var client = new HttpClient(handler) { BaseAddress = baseUrl, Timeout = TimeSpan.FromHours(2) }; if (clearDefaultRequestHeaders) { client.DefaultRequestHeaders.Clear(); } if (requestCookies != null) { var cookieCount = requestCookies.Count; for (var i = 0; i < cookieCount; i++) { handler.CookieContainer.Add(baseUrl, new Cookie(requestCookies.ElementAt(i).Name, requestCookies.ElementAt(i).Value)); } } HttpResponseMessage x; var requestModelJson = param != null?JsonSerializer.Serialize(param).JsonToQuery() : string.Empty; try { x = await client.GetAsync($"{baseUrl.AbsoluteUri}{url}{requestModelJson}"); } catch (Exception e) { return(new HttpResponseBO <T> { StatusCode = HttpStatusCode.BadRequest, ReasonPhrase = $"{e.Message} : {e.InnerException?.Message}", Result = (T)Activator.CreateInstance(typeof(T)) }); } CookieCollection responseCookies = cookies.GetCookies(baseUrl); var response = new HttpResponseBO <T> { StatusCode = x.StatusCode, ReasonPhrase = x.ReasonPhrase, Cookies = responseCookies, Result = typeof(T) != typeof(string) ? JsonSerializer.Deserialize <T>(x.Content.ReadAsStringAsync().Result, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }) : (T)Activator.CreateInstance(typeof(T), x.Content.ReadAsStringAsync().Result.ToCharArray()) }; return(response); }
public async Task <HttpResponseBO <T> > PostAsync <T>(Uri baseUrl, string url, object param, CookieCollection requestCookies = null, string contentType = "application/json", bool clearDefaultRequestHeaders = false) { var cookies = new CookieContainer(); var handler = new HttpClientHandler { CookieContainer = cookies }; using var client = new HttpClient(handler) { BaseAddress = baseUrl, Timeout = TimeSpan.FromHours(2) }; if (clearDefaultRequestHeaders) { client.DefaultRequestHeaders.Clear(); } if (requestCookies != null) { var cookieCount = requestCookies.Count; for (var i = 0; i < cookieCount; i++) { handler.CookieContainer.Add(baseUrl, new Cookie(requestCookies.ElementAt(i).Name, requestCookies.ElementAt(i).Value)); } } var serializeObject = JsonSerializer.Serialize(param); HttpResponseMessage x = await client.PostAsync($"{baseUrl.AbsoluteUri}{url}", new StringContent(serializeObject, Encoding.UTF8, contentType)); CookieCollection responseCookies = cookies.GetCookies(baseUrl); var stringContent = x.Content.ReadAsStringAsync(); var response = new HttpResponseBO <T> { StatusCode = x.StatusCode, ReasonPhrase = x.ReasonPhrase, Cookies = responseCookies, Result = typeof(T) != typeof(string) ? JsonSerializer.Deserialize <T>(x.Content.ReadAsStringAsync().Result, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }) : (T)Activator.CreateInstance(typeof(T), x.Content.ReadAsStringAsync().Result.ToCharArray()) }; return(response); }
public async Task <HttpResponseBO> PostAsyncXForm(Uri ApiUri, string url, object param, CookieCollection requestCookies = null, string contentType = "application/x-www-form-urlencoded") { CookieContainer cookies = new CookieContainer(); HttpClientHandler handler = new HttpClientHandler(); handler.CookieContainer = cookies; handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return(true); }; if (requestCookies != null) { int cookieCount = requestCookies.Count(); for (int i = 0; i < cookieCount; i++) { handler.CookieContainer.Add(ApiUri, new Cookie(requestCookies.ElementAt(i).Name, requestCookies.ElementAt(i).Value)); } } using (HttpClient _client = new HttpClient(handler) { BaseAddress = ApiUri, Timeout = TimeSpan.FromHours(2) }) { //_client.DefaultRequestHeaders.Clear(); string _i = param.ToFormData().ReadAsStringAsync().Result; StringContent y = new StringContent(_i, Encoding.UTF8, contentType); HttpResponseMessage x = await _client.PostAsync(ApiUri.AbsoluteUri + url, y); CookieCollection responseCookies = cookies.GetCookies(ApiUri); if (x.IsSuccessStatusCode) { HttpResponseBO response = new HttpResponseBO(); response.ResponseCookies = responseCookies; response.ResponseResult = await x.Content.ReadAsStringAsync(); return(response); } else { throw new System.ArgumentException(String.Format("{0}", x.ReasonPhrase)); } } }
public async Task <HttpResponseBO> PostAsync(Uri ApiUri, string url, object param, CookieCollection requestCookies = null, string contentType = "application/json") { CookieContainer cookies = new CookieContainer(); HttpClientHandler handler = new HttpClientHandler(); handler.CookieContainer = cookies; if (requestCookies != null) { int cookieCount = requestCookies.Count(); for (int i = 0; i < cookieCount; i++) { handler.CookieContainer.Add(ApiUri, new Cookie(requestCookies.ElementAt(i).Name, requestCookies.ElementAt(i).Value)); } } using (HttpClient _client = new HttpClient(handler) { BaseAddress = ApiUri, Timeout = TimeSpan.FromHours(2) }) { //_client.DefaultRequestHeaders.Clear(); HttpResponseMessage x = await _client.PostAsync(ApiUri.AbsoluteUri + "v2/" + url, new StringContent(JsonConvert.SerializeObject(param), Encoding.UTF8, "application/json")); CookieCollection responseCookies = cookies.GetCookies(ApiUri); if (x.IsSuccessStatusCode) { HttpResponseBO response = new HttpResponseBO(); response.ResponseCookies = responseCookies; response.ResponseResult = await x.Content.ReadAsStringAsync(); return(response); } else { throw new System.ArgumentException(String.Format("{0}", x.ReasonPhrase)); } } }