从另一个Web API项目调用Web API

时间:2013-03-11 12:03:43

标签: asp.net-web-api

我有三个Web API项目。

他们部署在

  1. API:http://api.domain.com
  2. mobileApi:http://mobile.api.domain.com
  3. logApi:http://log.api.domain.com
  4. 我想这样做: 当其他项目发生错误时,通过调用logApi中的控制器来记录错误。我已阅读http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client

    所以我编写这个类来帮助向Web API控制器发送请求

    public class WebApiRequest
    {
        public static T Get<T>(string url, object param = null, object header = null, Func<HttpResponseMessage, T> converter = null)
        {
            HttpClient client = GetClient(header);
            url = UrlHelper.BuildUrl(url, param);
            HttpResponseMessage response = client.GetAsync(url).Result;
            response.EnsureSuccessStatusCode();
            return converter != null ? converter(response) : DefaultConverter<T>(response);
        }
    
        public static T Post<T>(string url, object param = null, object header = null, Func<HttpResponseMessage, T> converter = null)
        {
            HttpClient client = GetClient(header);
            var content = new FormUrlEncodedContent(
                Tool.AnonymousObjectToDictionary(param).Where(i => i.Value != null).ToDictionary(i => i.Key, i => i.Value.ToString()));
            HttpResponseMessage response = client.PostAsync(url, content).Result;
            response.EnsureSuccessStatusCode();
            return converter != null ? converter(response) : DefaultConverter<T>(response);
        }
    
        public static T Put<T>(string url, object param = null, object header = null, Func<HttpResponseMessage, T> converter = null)
        {
            HttpClient client = GetClient(header);
            var content = new FormUrlEncodedContent(
                Tool.AnonymousObjectToDictionary(param).Where(i => i.Value != null).ToDictionary(i => i.Key, i => i.Value.ToString()));
            HttpResponseMessage response = client.PutAsync(url, content).Result;
            response.EnsureSuccessStatusCode();
            return converter != null ? converter(response) : DefaultConverter<T>(response);
        }
    
        public static T Delete<T>(string url, object param = null, object header = null, Func<HttpResponseMessage, T> converter = null)
        {
            HttpClient client = GetClient(header);
            url = UrlHelper.BuildUrl(url, param);
            HttpResponseMessage response = client.DeleteAsync(url).Result;
            response.EnsureSuccessStatusCode();
            return converter != null ? converter(response) : DefaultConverter<T>(response);
        }
    
        private static T DefaultConverter<T>(HttpResponseMessage response)
        {
            var formatters = new List<MediaTypeFormatter> {
                new JsonMediaTypeFormatter(),
                new XmlMediaTypeFormatter()
            };
            var result = response.Content.ReadAsAsync<T>(formatters).Result;
            return result;
        }
    
        public static string StringConverter(HttpResponseMessage response)
        {
            return response.Content.ReadAsStringAsync().Result;
        }
    
        private static HttpClient GetClient(object header)
        {
            HttpClient client = HttpClientFactory.Create(new DecompressionHandler());
            var headers = Tool.AnonymousObjectToDictionary(header);
            foreach (var h in headers.Where(h=>h.Value!=null)) {
                client.DefaultRequestHeaders.Add(h.Key, h.Value.ToString());
            }
            return client;
        }
    }
    

    在测试项目中,它非常完美。

    但是当我在Web API项目中使用它时,事情变得非常奇怪。

        WebApiRequest.Post<ReturnModel<int>>(logUrl, new {
            Source = "{" + source + "}",
            context.Exception.Message,
            context.Exception.StackTrace,
            ExceptionType = context.Exception.GetType().FullName,
            PriorityLevel = 3,
            AddTime = DateTime.Now,
            ProjectType = (short)ProjectType.WebApi,
        }, request.Headers);
    

    代码似乎没问题,但实际上HttpClient并没有请求我给它的url !!

    任何人都可以提供帮助,我很感激并且非常感谢。

0 个答案:

没有答案