rest - Microsoft Azure 中的 Web App 服务能否访问第三方 REST API?
问题描述
我们有一个网站公开 REST API 以访问其存储的数据。
我们希望从 Microsoft Azure 中的 WEB APP 服务访问这些 REST API。这可能吗?
解决方案
您可以简单地使用 HttpClient 并调用您的 web api。她的代码是相同的:
PS:-我给你一个控制台应用程序的示例,但你也可以使用网络应用程序来做同样的事情
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace HttpClientSample
{
public class Product
{
public string Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
}
class Program
{
static HttpClient client = new HttpClient();
static void ShowProduct(Product product)
{
Console.WriteLine($"Name: {product.Name}\tPrice: " +
$"{product.Price}\tCategory: {product.Category}");
}
static async Task<Uri> CreateProductAsync(Product product)
{
HttpResponseMessage response = await client.PostAsJsonAsync(
"api/products", product);
response.EnsureSuccessStatusCode();
// return URI of the created resource.
return response.Headers.Location;
}
static async Task<Product> GetProductAsync(string path)
{
Product product = null;
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
product = await response.Content.ReadAsAsync<Product>();
}
return product;
}
static async Task<Product> UpdateProductAsync(Product product)
{
HttpResponseMessage response = await client.PutAsJsonAsync(
$"api/products/{product.Id}", product);
response.EnsureSuccessStatusCode();
// Deserialize the updated product from the response body.
product = await response.Content.ReadAsAsync<Product>();
return product;
}
static async Task<HttpStatusCode> DeleteProductAsync(string id)
{
HttpResponseMessage response = await client.DeleteAsync(
$"api/products/{id}");
return response.StatusCode;
}
static void Main()
{
RunAsync().GetAwaiter().GetResult();
}
static async Task RunAsync()
{
// Update port # in the following line.
client.BaseAddress = new Uri("http://localhost:64195/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
try
{
// Create a new product
Product product = new Product
{
Name = "Gizmo",
Price = 100,
Category = "Widgets"
};
var url = await CreateProductAsync(product);
Console.WriteLine($"Created at {url}");
// Get the product
product = await GetProductAsync(url.PathAndQuery);
ShowProduct(product);
// Update the product
Console.WriteLine("Updating price...");
product.Price = 80;
await UpdateProductAsync(product);
// Get the updated product
product = await GetProductAsync(url.PathAndQuery);
ShowProduct(product);
// Delete the product
var statusCode = await DeleteProductAsync(product.Id);
Console.WriteLine($"Deleted (HTTP Status = {(int)statusCode})");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
}
}
请检查以下线程以获取更多信息:
https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
希望能帮助到你。
推荐阅读
- rust - Rust 单线程环境中的可变引用
- python - Spyder Mac OS 10.10.5 Yosemite 在使用 conda update anaconda 更新 anaconda 后失败
- r - ggplot x轴标签不会垂直
- c++ - main.cpp|69|未定义对“ibdev”的引用|
- python - python dual for 循环没有提供预期的结果
- java - Gradle 无法定位平台:Java SE 15 使用工具链 JDK 8 (1.8),但我使用的是 JDK 1.8?
- python - scapy 嗅探在 VM 上找不到任何设备
- javascript - 如何添加
- c++ - 再次pthreads:为什么我的线程函数中的for循环会产生溢出?
- python - 尝试打开子菜单时出现 PyQt 分段错误