Ejemplo n.º 1
0
        protected async Task <GatewayResponse> SendRequestAsync(string endpoint, MultipartFormDataContent content)
        {
            HttpClient httpClient = new HttpClient(HttpClientHandlerBuilder.Build(WebProxy))
            {
                Timeout = TimeSpan.FromMilliseconds(Timeout)
            };

            HttpRequestMessage  request  = new HttpRequestMessage(HttpMethod.Post, ServiceUrl + endpoint);
            HttpResponseMessage response = null;

            try {
                request.Content = content;

                RequestLogger?.RequestSent(GenerateRequestLog(request));

                response = await httpClient.SendAsync(request);

                string rawResponse = response.Content.ReadAsStringAsync().Result;

                RequestLogger?.ResponseReceived(rawResponse);

                return(new GatewayResponse {
                    StatusCode = response.StatusCode,
                    RequestUrl = response.RequestMessage.RequestUri.ToString(),
                    RawResponse = rawResponse
                });
            }
            catch (Exception exc) {
                throw new GatewayException("Error occurred while communicating with gateway.", exc);
            }
            finally { }
        }
Ejemplo n.º 2
0
        protected GatewayResponse SendRequest(HttpMethod verb, string endpoint, string data = null, Dictionary <string, string> queryStringParams = null, string contentType = null)
        {
            HttpClient httpClient = new HttpClient(HttpClientHandlerBuilder.Build(WebProxy))
            {
                Timeout = TimeSpan.FromMilliseconds(Timeout)
            };

            var queryString            = BuildQueryString(queryStringParams);
            HttpRequestMessage request = new HttpRequestMessage(verb, ServiceUrl + endpoint + queryString);

            foreach (var item in Headers)
            {
                request.Headers.Add(item.Key, item.Value);
            }

            if (DynamicHeaders != null)
            {
                foreach (var item in DynamicHeaders)
                {
                    request.Headers.Add(item.Key, item.Value);
                }
            }

            HttpResponseMessage response = null;

            try {
                if (verb != HttpMethod.Get && data != null)
                {
                    request.Content = new StringContent(data, Encoding.UTF8, contentType ?? _contentType);
                }

                RequestLogger?.RequestSent(GenerateRequestLog(request));

                response = httpClient.SendAsync(request).Result;

                string rawResponse = response.Content.ReadAsStringAsync().Result;

                RequestLogger?.ResponseReceived(rawResponse);

                return(new GatewayResponse {
                    StatusCode = response.StatusCode,
                    RequestUrl = response.RequestMessage.RequestUri.ToString(),
                    RawResponse = rawResponse
                });
            }
            catch (Exception exc) {
                throw new GatewayException("Error occurred while communicating with gateway.", exc);
            }
            finally { }
        }