asp.net-mvc - ASP.NET MVC 从服务器端调用 REST 服务

标签 asp.net-mvc rest

关闭。这个问题是opinion-based .它目前不接受答案。












想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题.

7年前关闭。




Improve this question




在 API Controller (服务器端)中的操作期间,我必须调用 REST 服务(我有一个通过 IP 返回用户国家/地区的外部 REST 服务),等待返回结果并继续执行...
这样做的最佳方法是什么?

最佳答案

您可以找到使用 HttpClient 的教程 here

资源 GET 示例:

    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("http://localhost:9000/");
        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>();
            Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category);
        }
    }

关于asp.net-mvc - ASP.NET MVC 从服务器端调用 REST 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21330078/

相关文章:

asp.net-mvc - 如何在 ASP.NET MVC 的同一页面中使用同一 .ascx 的两个实例?

asp.net-mvc - 一句话,这样做 ASP.NET (MVC) 路由崩溃

rest - 带有 NginX 和 auth_request 模块的 CORS header

ruby-on-rails - 在循环中附加到 JSON 数组

java - Spring Boot - 模拟对外部 API 的 POST REST 请求

authentication - RESTful 服务认证

asp.net-mvc - 为什么 ListBoxFor 不选择项目,而 ListBox 是?

c# - 在没有 controller/http.context 的情况下绑定(bind)模型

asp.net-mvc - Html.HiddenFor 以不同方式处理 string 和 int

web-services - Spring+休息+mysql