如果你喜欢 HttpLib 项目,请为本项点亮一颗星 ⭐!
- .NET 6.0及以上。
- .NET Core3.1及以上。
- .NET Standard2.0及以上。
既可以上传文件等二进制数据,也可以上传表单键值对
上传与下载的进度监控
类似图片加载场景,同一个id的图片通过磁盘存储减少网络开支
支持多线程分段下载,提高下载速度
支持暂停、恢复下载,下载速度和时间估算
Http.Get("https://www.baidu.com")Http.Post("https://www.baidu.com")Http.Put("https://www.baidu.com")Http.Delete("https://www.baidu.com")GET请求参数会自动注入到地址
data("wd","随便搜一下")
data(new{test1="测试1",test2="测试2"})
data(new{params_="关键字参数"})
data(new{wd=newstring[]{"GitHub - Haku-Men HttpLib","POST数组参数"}})URL参数(除了GET请求)
query("键","值对")
query(new{test="POST下继续传递URL参数"})data(newVal("test1","测试1"),newVal("test2","测试2"))data(newList<Val>{newVal("test1","测试1"),newVal("test2","测试2")})上传字符串 默认
text/plain
stringjson="{\"JSON\":\"json data\"}";datastr(json,"application/json")上传文件
data(newFiles("文件地址"))file(@"文件地址")header("Authorization","abc")header(new{accept="*/*",userAgent="Chrome"})header(newVal("accept","*/*"),newVal("user-agent","Chrome"))proxy("127.0.0.1",1000)默认禁止
redirect()
毫秒(默认不超时)
timeout(3000)默认
utf-8
encoding('utf-8')先配置
Config.CacheFolder缓存文件夹
cache("缓存id")或者设定有效期 1分钟
cache("缓存id",1)before((HttpWebResponseresponse,ResultResponseresult)=>{returntrue;//继续请求})字节大小
uploadProgress(e =>{Console.Write("{0}% 上传",Math.Round(e.Prog*100.0,1).ToString("N1"));})downloadProgress(e =>{if(e.GetProg(outvarprog)){Console.Write("{0}% 下载",Math.Round(prog*100.0,1).ToString("N1"));}})fail((ResultResponseresult)=>{})requestNone();//不下载流request();//返回字符串requestData();//返回字节download("保存目录","保存文件名称(为空自动获取)");//下载文件string?result=Http.Get("https://www.baidu.com/s").data(new{wd="GitHub - Haku-Men HttpLib",params_="关键字参数"}).redirect().request();Console.Write(result);varsavapath=Http.Get("https://dldir1.qq.com/qqfile/qq/QQNT/Windows/QQ_9.9.9_240422_x64_01.exe").redirect().downloadProgress(e =>{Console.SetCursorPosition(0,0);if(e.GetProg(outvarprog)){Console.Write("{0}% 下载 {1}/{2} ",Math.Round(prog*100.0,1).ToString("N1"),CountSize(e.Value),CountSize(e.MaxValue));}else{Console.Write("{0} 下载 ",CountSize(e.Value));}}).download(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),"qq.exe");if(savapath!=null)Console.WriteLine("下载成功保存至:"+savapath);elseConsole.WriteLine("下载失败");Http.Get("https://test.chatgpt.com/local_doc_chat").request(msg =>{if(msg==null)return;if(msg.StartsWith("data: "))msg=msg.Substring(5).Trim();System.Diagnostics.Debug.WriteLine(msg);});Http.Get("https://www.baidu.com").IPConfig.fail+=(HttpCorecore,ResultResponseresult)=>{if(result.Exception==null)return;Console.Write(result.Exception.GetType());Console.Write(result.Exception.Message);};vardownloader=Http.Get("https://dldir1.qq.com/qqfile/qq/QQNT/Windows/QQ_9.9.9_240422_x64_01.exe").downLoad(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));string?savePath=awaitdownloader.Go();if(savePath!=null)Console.WriteLine("下载成功保存至: "+savePath);elseConsole.WriteLine("下载失败");vardownloader=Http.Get("https://example.com/large-file.zip").downLoad(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));// 使用 8 线程下载string?savePath=awaitdownloader.Go(8);vardownloader=Http.Get("https://example.com/file.zip").downLoad(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));// 自定义保存文件名string?savePath=awaitdownloader.Go("custom-name.zip");vardownloader=Http.Get("https://example.com/large-file.zip").downLoad(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)).SetRetryCount(3)// 设置重试次数.SetCacheSize(8192);// 设置缓存大小string?savePath=awaitdownloader.Go(4,"my-file.zip");vardownloader=Http.Get("https://dldir1.qq.com/qqfile/qq/QQNT/Windows/QQ_9.9.9_240422_x64_01.exe").downLoad(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));// 监控下载进度downloader.ValueChange(progress =>{Console.SetCursorPosition(0,0);Console.WriteLine($"下载进度: {downloader.Prog*100:F1}%"+$" 速度: {downloader.Speed}/s"+$" 剩余时间: {downloader.Time}");});// 监控下载状态变化downloader.StateChange((t,err)=>{Console.WriteLine($"下载状态: {t}");});string?savePath=awaitdownloader.Go();vardownloader=Http.Get("https://example.com/large-file.zip").downLoad(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));// 开始下载vardownloadTask=downloader.Go();// 暂停下载downloader.Suspend();// 恢复下载downloader.Resume();// 等待下载完成string?savePath=awaitdownloadTask;| 代码 | 类型 | 解释 | 说明 |
|---|---|---|---|
| StatusCode | int | 状态代码 | 200 为正常 常见的有404未找到、302重定向、502网址报错 |
| IsSuccessStatusCode | bool | 响应是否成功 | range 200-299 |
| ServerHeader | string? | 服务头 | HTTP 200 OK BWS/1.1 Ver:1.1 |
| Uri | Uri | 最终的地址 | |
| Type | string? | 服务指示类型 | Content-Type |
| Header | Dictionary<string, string> | 响应头 | |
| Cookie | Dictionary<string, string> | Cookie | |
| OriginalSize | long | 流原始大小 | 动态压缩 |
| Size | long | 流大小 | |
| Exception | Exception? | 异常信息 |