【问题标题】:How to consume restful services from another restful service in asp.net?如何从asp.net中的另一个restful服务消费restful服务?
【发布时间】:2017-01-30 16:54:38
【问题描述】:

我有一个 RESTful 服务的 URL,我想从另一个 RESTful 服务中使用这个 RESTful 服务。

假设 URL 是 first rest service 是"http://testapi.com/services/rest/?method=getList&key=123”

Restful service 1  - > Restful service 2  -> asp.net client application

您能否提供任何包含代码和配置设置的示例。

谢谢

【问题讨论】:

    标签: asp.net rest wcf service


    【解决方案1】:

    您可以使用HttpClient。帖子中的示例使用的是控制台应用程序,但您仍然可以从 Web Api 项目(我的一些项目中使用它)中使用它。

    获取异步的示例:

    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("YOURURIHERE");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
        // New code:
        HttpResponseMessage response = await client.GetAsync("api/products/1");
        if (response.IsSuccessStatusCode)
        {
            Product product = await response.Content.ReadAsAsync>Product>();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      相关资源
      最近更新 更多