예제 #1
0
        public Response PostToWebApi <Request, Response>(Request request,
                                                         string serviceName,
                                                         string version,
                                                         string action,
                                                         string controller,
                                                         [Optional] Dictionary <string, string> customHeaders) where Response : new()
        {
            var httpClient = _httpClientFactory.CreateClient(serviceName);

            if (customHeaders != null)
            {
                foreach (var header in customHeaders)
                {
                    httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
                }
            }

            var req     = JsonConvert.SerializeObject(request);
            var content = new StringContent(req, Encoding.UTF8, "application/json");

            var    serviceUrl = httpClient.BaseAddress.AbsoluteUri + version;
            string requestUri = GetRequestUri(serviceUrl, action, controller);

            var response = httpClient.PostAsync(requestUri, content).Result;

            var result = HttpClientReponse <Response> .ReadMessage(response);

            return(result);
        }
예제 #2
0
        public Response GetFromWebApi <Response>(string serviceName,
                                                 string version,
                                                 string action,
                                                 string controller,
                                                 [Optional] Dictionary <string, object> parameters,
                                                 [Optional] Dictionary <string, string> customHeaders
                                                 ) where Response : new()
        {
            var httpClient = _httpClientFactory.CreateClient(serviceName);

            if (customHeaders != null)
            {
                foreach (var header in customHeaders)
                {
                    httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
                }
            }

            var serviceUrl = httpClient.BaseAddress.AbsoluteUri + version;

            string requestUri = string.Empty;

            if (parameters != null)
            {
                requestUri = GetRequestUri(serviceUrl, action, controller, parameters);
            }
            else
            {
                requestUri = GetRequestUri(serviceUrl, action, controller);
            }

            var response = httpClient.GetAsync(requestUri).Result;

            var result = HttpClientReponse <Response> .ReadMessage(response);

            return(result);
        }