关闭。这个问题是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/