/// <summary> /// This service comprises of registration/ un-registration request and response. /// </summary> /// <param name="subscriptionRequestDto">Subscription Data</param> /// <returns></returns> private async Task <HttpResponseMessage> SubscribeUser(SubscriptionRequestDto subscriptionRequestDto) { try { Log.TraceStart(); var requestUri = string.Format("{0}{1}", _baseUrl, Consts.URL_SUBSCRIPTION_ADD); var contentType = InternetMediaType.ApplicationJson; var postBody = JsonConvert.SerializeObject(subscriptionRequestDto); var content = new StringContent(postBody); using (var client = new HttpClient()) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate); //Send HTTP requests //client.BaseAddress = new Uri(Consts.URL_BASE_ADDRESS); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUri); content.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType); var response = await client.PostAsync(requestUri, content); if (response.IsSuccessStatusCode) { return(response); } var ex = new HttpResponseException(new HttpResponseMessage() { StatusCode = response.StatusCode, Content = response.Content, ReasonPhrase = response.ReasonPhrase, RequestMessage = response.RequestMessage, Version = response.Version, }); var fieldInfo = ex.GetType().GetField("_message", BindingFlags.Instance | BindingFlags.NonPublic); if (fieldInfo != null) { fieldInfo.SetValue(ex, string.Format("{0}{1}HTTP Response:{1}{2}", ex.Message, Environment.NewLine, response.ToString())); } Log.TraceEnd(); throw ex; } } catch (Exception ex) { Log.Exception(ex); throw; } }
/// <summary> /// Sends a HTTP POST request to the specified Uri as an asynchronous operation /// and returns the result as a <see cref="HttpResponseMessage"/>. /// </summary> /// <param name="host">The base address for request.</param> /// <param name="api">The request Uri</param> /// <param name="requestContent">The request content which should be sent to the server.</param> /// <param name="acceptHeaderMediaType">The accept header type for request.</param> /// <param name="httpMessageHandler">The HTTP handler stack to use for sending requests.</param> /// <param name="customHeaders">Custom headers for the http request.</param> /// <param name="authHeader">The authorization header for the request.</param> /// <param name="queryStringParams">The query string params.</param> /// <returns>The task object representing the asynchronous operation.</returns> public static async Task <HttpResponseMessage> PostAsync(string host, string api, dynamic requestContent = null, string acceptHeaderMediaType = InternetMediaType.ApplicationJson, HttpMessageHandler httpMessageHandler = null, KeyValuePair <string, dynamic>[] queryStringParams = null, KeyValuePair <string, string>[] customHeaders = null, AuthenticationHeaderValue authHeader = null) { using (var client = httpMessageHandler == null ? new HttpClient() : new HttpClient(httpMessageHandler)) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(acceptHeaderMediaType)); client.DefaultRequestHeaders.Authorization = authHeader; AddCustomHeaders(client, customHeaders); var queryString = string.Empty; if (queryStringParams != null && queryStringParams.Length > 0) { queryString = string.Format("?{0}", string.Join("&", queryStringParams.Select(m => m.Key + "=" + m.Value))); } var response = await client.PostAsync(string.Format("{0}{1}{2}", host, api, queryString), new StringContent(JsonConvert.SerializeObject(requestContent), Encoding.UTF8, InternetMediaType.ApplicationJson)); if (response.IsSuccessStatusCode) { return(response); } var ex = new HttpResponseException(new HttpResponseMessage() { StatusCode = response.StatusCode, Content = response.Content, ReasonPhrase = response.ReasonPhrase, RequestMessage = response.RequestMessage, Version = response.Version, }); var fieldInfo = ex.GetType().GetField("_message", BindingFlags.Instance | BindingFlags.NonPublic); if (fieldInfo != null) { fieldInfo.SetValue(ex, string.Format("{0}{1}HTTP Response:{1}{2}", ex.Message, Environment.NewLine, response.ToString())); } throw ex; } }
private void ThrowNotFoundException() { var error = new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent("No Personal Loan is found.") }); // Log Exception Errors err = new Errors() { ExceptionType = error.GetType().ToString(), Message = error.Message, Date = DateTime.Now }; errorService.LogError(err); throw error; }
private ProblemJsonModel CreateResponse(HttpResponseException exception) { var problemJsonResponse = new ProblemJsonModel { TypeUri = "about:blank", Status = (int)exception.Response.StatusCode, Instance = string.Format("urn:ietf:rfc:draft-nottingham-http-problem-06:x-exception:{0}", exception.GetType().FullName), }; if (_developerMode) { problemJsonResponse.Detail = exception.Message; problemJsonResponse.DebugInfo = exception.StackTrace; problemJsonResponse.Title = exception.Message; } return(problemJsonResponse); }