我得到了三个 Web API 项目。
他们部署在
- 接口:http : //api.domain.com
- mobileApi:http: //mobile.api.domain.com
- logApi:http ://log.api.domain.com
我想这样做:当其他项目发生错误时,通过调用 logApi 中的控制器记录错误。我已阅读http://www.asp.net/web-api/overview/web-api-clients/calling-来自网络客户端的网络 API
所以我写了这个类来帮助向 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!
任何人都可以提供帮助,我很感激,非常感谢。