【问题标题】:HttpClient what and for what? [closed]HttpClient 是什么,为了什么? [关闭]
【发布时间】:2013-03-21 14:30:01
【问题描述】:

什么是HttpClient?它是为了什么?从我需要开始为 api 控制器编写简单的 http 客户端开始?我需要它来简单地访问控制器。

【问题讨论】:

标签: asp.net-web-api httpclient


【解决方案1】:

这应该给你一个开始:

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(并返回一个订单对象)

【讨论】:

  • 谢谢,这正是我需要的)
  • 很好,现在将我的答案标记为正确怎么样?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-03
  • 2010-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多