CShrap调接口示例
时间: 2023-02-07 11:24:46 浏览: 54
下面是一个在 C# 中使用 HttpClient 类调用 API 的示例:
```
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace MyApp
{
class Program
{
static async Task Main(string[] args)
{
// 创建 HTTP 客户端
var client = new HttpClient();
// 发送 GET 请求并获取响应
var response = await client.GetAsync("https://api.example.com/endpoint");
// 读取响应内容
var responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseContent);
}
}
}
```
这个示例中,我们使用 HttpClient 类的 GetAsync 方法向 https://api.example.com/endpoint 发送了一个 GET 请求。请求完成后,我们使用 Content.ReadAsStringAsync 方法读取响应内容。
你也可以使用 HttpClient 类的其他方法,如 PostAsync、PutAsync 和 DeleteAsync,来发送 POST、PUT 和 DELETE 请求。
希望这个示例能帮到你!