c# HttpClient
时间: 2024-01-12 15:21:38 浏览: 119
以下是两个使用C# HttpClient的例子:
1. 使用GET方法获取指定URL的内容:
```csharp
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace HttpClientEx
{
class Program
{
static async Task Main(string[] args)
{
using var client = new HttpClient();
var content = await client.GetStringAsync("http://webcode.me");
Console.WriteLine(content);
}
}
}
```
2. 使用HEAD方法获取指定URL的响应头:
```csharp
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace HttpClientHead
{
class Program
{
static async Task Main(string[] args)
{
var url = "http://webcode.me";
using var client = new HttpClient();
var result = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, url));
Console.WriteLine(result);
}
}
}
```
阅读全文