public ApiResult <T> PutResourceMessage <T>(string url, object content, Dictionary <string, object> headers = null) { try { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return(true); }); using (var client = new HttpClient()) { client.BaseAddress = new Uri(url); AppendHeaders(headers, client); //Convert To Bytes Array . var myContent = JsonConvert.SerializeObject(content); var buffer = Encoding.UTF8.GetBytes(myContent); var byteContent = new ByteArrayContent(buffer); //Model will be serialized automatically. var response = client.PutAsJsonAsync(url, byteContent).GetAwaiter().GetResult(); if (response.IsSuccessStatusCode) { //ReadAsAsync permits to deserialize the response content var responseContent = response.Content.ReadAsAsync <T>().GetAwaiter().GetResult(); return(ConstructSuccessMessage(response, responseContent)); } var message = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); return(ConstructFailureMessage <T>(response, message)); } } catch (Exception ex) { _logger.Log($"Error: {nameof(PutResourceMessage)}", $"{nameof(PutResourceMessage)} Throws Exception Url:{url}", ex, true); _logger.LogToElmah(ex); return(new ApiResult <T>() { BusinessStatusCode = HttpStatusCode.InternalServerError.ToString(), Status = OperationOutputStatus.ServerError, StatusCode = HttpStatusCode.InternalServerError, MessageAr = $"{ExceptionMessage.GetErrorMessage(ex, "ar")} {ex.Message}", MessageEn = $"{ExceptionMessage.GetErrorMessage(ex, "en")} {ex.Message}", }); } }
public ApiResult <T> GetResourceMessage <T>(string url, Dictionary <string, object> headers = null, List <string> paramters = null, Dictionary <string, string> queryStrings = null) { try { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return(true); }); using (var client = new HttpClient()) { //Passing service base url client.BaseAddress = new Uri(url); AppendHeaders(headers, client); var fullUrl = AppendQueryValues(url, paramters, queryStrings); //Sending request to find web api REST service resource GetAllEmployees using HttpClient var response = client.GetAsync(fullUrl).GetAwaiter().GetResult(); if (response.IsSuccessStatusCode) { //ReadAsAsync permits to deserialize the response content var responseContent = response.Content.ReadAsAsync <T>().GetAwaiter().GetResult(); return(ConstructSuccessMessage(response, responseContent)); } else { var message = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); return(ConstructFailureMessage <T>(response, message)); } } } catch (Exception ex) { _logger.Log($"Error: {nameof(GetResourceMessage)}", $"{nameof(GetResourceMessage)} Throws Exception Url:{url}", ex, true); _logger.LogToElmah(ex); return(new ApiResult <T>() { BusinessStatusCode = HttpStatusCode.InternalServerError.ToString(), Status = OperationOutputStatus.ServerError, StatusCode = HttpStatusCode.InternalServerError, MessageAr = $"{ExceptionMessage.GetErrorMessage(ex, "ar")} {ex.Message}", MessageEn = $"{ExceptionMessage.GetErrorMessage(ex, "en")} {ex.Message}", }); } }