CSarp调用接口的学习

我的代码

记录一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
static async Task Main(string[] args)
{
// 创建 HttpClient 实例
using (var client = new HttpClient())
{
string qqNumber = "12345678";
string url = $"https://zy.xywlapi.cc/qqapi?qq={qqNumber}";

try
{
// 发送 GET 请求
HttpResponseMessage response = await client.GetAsync(url);

// 确保响应状态码是成功的
response.EnsureSuccessStatusCode();

// 读取响应内容为字符串
string responseBody = await response.Content.ReadAsStringAsync();

// 输出响应内容
Console.WriteLine(responseBody);
}
catch (HttpRequestException e)
{
// 处理请求异常
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ", e.Message);
}
}
}
}