C# HttpClient全攻略:GET、POST、文件传输与授权设置一网打尽
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
概述:本文详解了在C#中使用HttpClient发送HTTP请求的全面指南,包括GET、POST请求、文件上传和下载,以及设置Authorization、cookie等操作,为实现网络操作提供了清晰的方法和示例。 1. 说明
2. 方法说明2.1 发送GET请求using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync("https://example.com/api/resource"); // 处理响应
}2.2 发送POST请求using (HttpClient client = new HttpClient())
{
var content = new StringContent("data to send", Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync("https://example.com/api/resource", content); // 处理响应
}2.3 上传文件using (HttpClient client = new HttpClient())
{
var content = new MultipartFormDataContent();
content.Add(new ByteArrayContent(File.ReadAllBytes("file.txt")), "file", "file.txt");
HttpResponseMessage response = await client.PostAsync("https://example.com/api/upload", content); // 处理响应
}2.4 下载文件using (HttpClient client = new HttpClient())
{
byte[] fileData = await client.GetByteArrayAsync("https://example.com/api/download");
File.WriteAllBytes("downloaded_file.txt", fileData);
}2.5 设置Authorizationusing (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "your_access_token");
HttpResponseMessage response = await client.GetAsync("https://example.com/api/resource"); // 处理响应
}2.6 设置Cookieusing (HttpClient client = new HttpClient())
{
var cookieContainer = new CookieContainer();
var handler = new HttpClientHandler { CookieContainer = cookieContainer };
using (var httpClient = new HttpClient(handler))
{
HttpResponseMessage response = await client.GetAsync("https://example.com/api/resource"); // 处理响应
}
}3. 步骤说明3.1 发送GET请求
3.2 发送POST请求
3.3 上传文件
3.4 下载文件
3.5 设置Authorization
3.6 设置Cookie
4. 实例源代码// 示例代码请根据实际情况修改URL和文件路径
// 发送GET请求
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync("https://example.com/api/resource"); // 处理响应
}
// 发送POST请求
using (HttpClient client = new HttpClient())
{
var content = new StringContent("data to send", Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync("https://example.com/api/resource", content); // 处理响应
}
// 上传文件
using (HttpClient client = new HttpClient())
{
var content = new MultipartFormDataContent();
content.Add(new ByteArrayContent(File.ReadAllBytes("file.txt")), "file", "file.txt");
HttpResponseMessage response = await client.PostAsync("https://example.com/api/upload", content); // 处理响应
}
// 下载文件
using (HttpClient client = new HttpClient())
{
byte[] fileData = await client.GetByteArrayAsync("
File.WriteAllBytes("downloaded_file.txt", fileData);
}
// 设置
Authorizationusing (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "your_access_token");
HttpResponseMessage response = await client.GetAsync("https://example.com/api/resource"); // 处理响应
}
// 设置
Cookieusing (HttpClient client = new HttpClient())
{
var cookieContainer = new CookieContainer();
var handler = new HttpClientHandler { CookieContainer = cookieContainer };
using (var httpClient = new HttpClient(handler))
{
HttpResponseMessage response = await client.GetAsync("https://example.com/api/resource"); // 处理响应
}
}5. 注意事项及建议
通过 该文章在 2024/1/23 18:47:51 编辑过 |
关键字查询
相关文章
正在查询... |