HttpClient是什么,为了什么?

时间:2013-03-21 14:30:02

标签: asp.net-web-api httpclient

什么是HttpClient?这是什么?从我需要开始为api控制器编写简单的http客户端?我需要它才能轻松访问控制器。

1 个答案:

答案 0 :(得分:0)

这应该给你一个开始:

private Order SendOrderRequest(Models.OrderTest model)
{
    Uri uri = new Uri(model.BaseUrl + "order");

    HttpClient client = new HttpClient();

    client.BaseAddress = uri;

    var mediaType = new MediaTypeHeaderValue("application/json");
    var jsonFormatter = new JsonMediaTypeFormatter();

    HttpContent content = new ObjectContent<Order>(model.Order, jsonFormatter);
    HttpResponseMessage responseMessage = client.PostAsync(uri, content).Result;

    return responseMessage.Content.ReadAsAsync(typeof(Supertext.API.POCO.Order)).Result as Supertext.API.POCO.Order;
}

它只是将模型中的订单对象发布到Web API(并获取订单对象)