返回
C# HttpClient GET POST 上传下载文件 使用教程
2024-03-25 2929 0
HttpClient是C#中用于发送HTTP请求的类,基于HttpClient的异步模型,可以实现GET、POST请求,处理响应数据,上传和下载文件,以及设置Authorization、cookie等。

发送GET请求
- 创建HttpClient实例。
- 使用GetAsync方法发送GET请求。
- 处理HttpResponseMessage以获取响应数据。
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync("https://example.com/api/resource");
// 处理响应
}
发送POST请求
- 创建HttpClient实例。
- 创建StringContent或FormUrlEncodedContent作为请求内容。
- 使用PostAsync方法发送POST请求。
- 处理HttpResponseMessage以获取响应数据。
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);
// 处理响应
}
上传文件
- 创建HttpClient实例。
- 创建MultipartFormDataContent,添加ByteArrayContent作为文件内容。
- 使用PostAsync方法发送包含文件的请求。
- 处理HttpResponseMessage以获取响应数据。
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);
// 处理响应
}
下载文件
- 创建HttpClient实例。
- 使用GetByteArrayAsync方法获取文件的字节数组。
- 使用File.WriteAllBytes保存文件。
using (HttpClient client = new HttpClient())
{
byte[] fileData = await client.GetByteArrayAsync("https://example.com/api/download");
File.WriteAllBytes("downloaded_file.txt", fileData);
}
设置Authorization
- 创建HttpClient实例。
- 设置DefaultRequestHeaders.Authorization属性为AuthenticationHeaderValue。
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "your_access_token");
HttpResponseMessage response = await client.GetAsync("https://example.com/api/resource");
// 处理响应
}
设置Cookie
- 创建HttpClient实例。
- 创建CookieContainer和HttpClientHandler实例。
- 在HttpClientHandler中设置CookieContainer。
- 使用包含HttpClientHandler的HttpClient实例发送请求。
using (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");
// 处理响应
}
}
注意事项及建议
- 使用using语句确保及时释放HttpClient资源。
- 异步方法应避免在同步上下文中使用,以提高性能。
- 注意处理HTTP响应状态码和异常,确保请求的正确执行。
- 文件上传和下载时,考虑使用流式传输以节省内存。
通过HttpClient,我们能够在C#中方便地进行HTTP请求操作,包括GET、POST请求、文件上传和下载,以及设置Authorization、cookie等。确保使用异步操作和适当的异常处理,能够保证请求的稳定性和性能。
网友点评
提交
相关阅读
C#使用System.Speech朗读文本
C# 使用mciSendString实现播放声音
2024年常见.NET C#面试题和答案
使用.NET C#将图片转换为.ico图标文件的多种方法
使用VSCode开发.NET MVC常用插件
c# 定时任务cron示例
.NET C#异步编程async/await最佳实践
C#中内置的泛型委托Func和Action用法
相关帖子
韩国电影1987 黎明到来的那一天 网盘磁力BT下载
2025年国内外高速免费 CDN 服务推荐:稳定、高效、零成本方案
谷歌语音电话Google Voice使用教程
解决Google AI Pro/Ultra订阅与Antigravity登录提示该地区不适用的方法
油管、B站、TikTok视频下载工具4K Video Downloader
Steam下载慢如何解决?5款性价比Steam加速器推荐
2025年最佳免费静态网站托管服务推荐:GitHub Pages、Vercel、Netlify等平台对比
探索开源游戏的宝藏库:详解 GitHub 上的 open-source-games 项目