private HTTPResponseDTO <T> WebExceptionHandler <T>(WebException webex, string referenceNo) where T : class { HTTPResponseDTO <T> response = null; if (webex.Response != null) { WebResponse errResp = webex.Response; HttpWebResponse resp = (HttpWebResponse)webex.Response; using Stream respStream = errResp.GetResponseStream(); if (resp.StatusCode == HttpStatusCode.BadRequest) { string content = new StreamReader(respStream).ReadToEnd(); if (!string.IsNullOrWhiteSpace(content)) { response = new HTTPResponseDTO <T>() { HttpStatusCode = resp.StatusCode, Body = JsonHandler.Deserialize <T>(content) }; } else { Logger.WriteLog(logType: CommonEnum.LogLevelEnum.Error, methodBase: System.Reflection.MethodBase.GetCurrentMethod(), referenceNo: referenceNo, exception: webex); } } else { Logger.WriteLog(logType: CommonEnum.LogLevelEnum.Error, methodBase: System.Reflection.MethodBase.GetCurrentMethod(), referenceNo: referenceNo, message: new StreamReader(respStream).ReadToEnd(), exception: webex); } } else { Logger.WriteLog(logType: CommonEnum.LogLevelEnum.Error, methodBase: System.Reflection.MethodBase.GetCurrentMethod(), referenceNo: referenceNo, exception: webex); } return(response); }
/// <summary> /// Request URL [GET] /// </summary> /// <typeparam name="T"></typeparam> /// <param name="url">Target url.</param> /// <returns>HTTPResponseDTO<T></returns> public HTTPResponseDTO <T> Get <T>(string url, Dictionary <string, string> headers = default) where T : class { #region Declare return type with initial value HTTPResponseDTO <T> response = new HTTPResponseDTO <T>(); string referenceNo = Guid.NewGuid().ToString(); #endregion try { if (!string.IsNullOrWhiteSpace(url)) { #region Create Request HttpWebRequest httpWebRequest = (HttpWebRequest)System.Net.WebRequest.Create(url); httpWebRequest.Timeout = AppSettings.HttpRequestTimeout; httpWebRequest.Method = HttpEnum.HttpMethod.Get.GetDescription(); httpWebRequest.ContentType = HttpEnum.HttpContentType.JSON.GetDescription(); httpWebRequest.Accept = HttpEnum.HttpContentType.JSON.GetDescription(); #endregion #region Append Headers if (headers?.Any() ?? default) { foreach (KeyValuePair <string, string> item in headers) { httpWebRequest.Headers.Add(item.Key, item.Value); } } #endregion #region Ignore SSL certificate validation if (AppSettings.IgnoreSSL) { ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; } #endregion #region Get Response HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); if (httpResponse != null) { string content = string.Empty; using StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()); content = streamReader.ReadToEnd(); if (!string.IsNullOrWhiteSpace(content)) { response = new HTTPResponseDTO <T>() { HttpStatusCode = httpResponse.StatusCode, Body = JsonHandler.Deserialize <T>(content) }; } } #endregion } } catch (WebException webex) { response = WebExceptionHandler <T>(webex, referenceNo); } catch (Exception exception) { Logger.WriteLog(logType: CommonEnum.LogLevelEnum.Error, methodBase: System.Reflection.MethodBase.GetCurrentMethod(), referenceNo: referenceNo, exception: exception); } return(response); }