Tom.HttpLib 3.1.0

dotnet add package Tom.HttpLib --version 3.1.0
                    
NuGet\Install-Package Tom.HttpLib -Version 3.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tom.HttpLib" Version="3.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Tom.HttpLib" Version="3.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Tom.HttpLib" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Tom.HttpLib --version 3.1.0
                    
#r "nuget: Tom.HttpLib, 3.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Tom.HttpLib@3.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Tom.HttpLib&version=3.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Tom.HttpLib&version=3.1.0
                    
Install as a Cake Tool

HttpLib 便捷的Http库 | 多线程下载库

如果你喜欢 HttpLib 项目,请为本项点亮一颗星 ⭐!

NuGet Download

🖥支持环境

  • .NET 6.0及以上。
  • .NET Core3.1及以上。
  • .NET Standard2.0及以上。

🌴支持

multipart/form-data

既可以上传文件等二进制数据,也可以上传表单键值对

上传与下载进度回调

上传与下载的进度监控

支持缓存

类似图片加载场景,同一个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 = new string[] { "GitHub - Haku-Men HttpLib", "POST数组参数" } })

URL参数(除了GET请求)

query("键", "值对")
query(new { test = "POST下继续传递URL参数" })
data(new Val("test1", "测试1"), new Val("test2", "测试2"))
data(new List<Val> {
	new Val("test1","测试1"),
	new Val("test2","测试2")
})

上传字符串 默认 text/plain

string json = "{\"JSON\":\"json data\"}";
datastr(json, "application/json")

上传文件

data(new Files("文件地址"))
file(@"文件地址")

添加请求头

header("Authorization", "abc")
header(new { accept = "*/*", userAgent = "Chrome" })
header(new Val("accept","*/*"), new Val("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((HttpWebResponse response, ResultResponse result) =>
{
	return true; //继续请求
})

注入回调获取进度

字节大小

上传
uploadProgress(e => {
    Console.Write("{0}% 上传", Math.Round(e.Prog * 100.0, 1).ToString("N1"));
})
下载
downloadProgress(e => {
    if (e.GetProg(out var prog))
    {
        Console.Write("{0}% 下载", Math.Round(prog * 100.0, 1).ToString("N1"));
    }
})

请求

异步错误

fail((ResultResponse result) => {
})

同步获取

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);

实例下载文件

var savapath = 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(out var prog))
        {
            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);
else Console.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);
});

实例获取域名IP

Http.Get("https://www.baidu.com").IP

实例全局错误捕获

Config.fail += (HttpCore core, ResultResponse result)=>
{
    if (result.Exception == null) return;
    Console.Write(result.Exception.GetType());
    Console.Write(result.Exception.Message);
};

多线程下载

多线程下载基本用法

简单下载

var downloader = 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 = await downloader.Go();
if (savePath != null) Console.WriteLine("下载成功保存至: " + savePath);
else Console.WriteLine("下载失败");

自定义线程数

var downloader = Http.Get("https://example.com/large-file.zip")
    .downLoad(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));

// 使用 8 线程下载
string? savePath = await downloader.Go(8);

自定义文件名

var downloader = Http.Get("https://example.com/file.zip")
    .downLoad(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));

// 自定义保存文件名
string? savePath = await downloader.Go("custom-name.zip");

多线程下载高级选项

完整控制

var downloader = Http.Get("https://example.com/large-file.zip")
    .downLoad(Environment.GetFolderPath(Environment.SpecialFolder.Desktop))
    .SetRetryCount(3)  // 设置重试次数
    .SetCacheSize(8192);  // 设置缓存大小

string? savePath = await downloader.Go(4, "my-file.zip");

下载状态与监控

监控下载进度

var downloader = 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 = await downloader.Go();

暂停与恢复下载

var downloader = Http.Get("https://example.com/large-file.zip")
    .downLoad(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));

// 开始下载
var downloadTask = downloader.Go();

// 暂停下载
downloader.Suspend();

// 恢复下载
downloader.Resume();

// 等待下载完成
string? savePath = await downloadTask;

ResultResponse介绍

代码 类型 解释 说明
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? 异常信息
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net40 is compatible.  net403 was computed.  net45 is compatible.  net451 was computed.  net452 was computed.  net46 is compatible.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.0

    • No dependencies.
  • .NETFramework 4.5

    • No dependencies.
  • .NETFramework 4.6

    • No dependencies.
  • .NETFramework 4.8

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net10.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.1.0 85 2/2/2026
3.0.6 340 5/7/2025
3.0.5 247 12/9/2024
3.0.4 342 9/13/2024
3.0.3 202 7/1/2024
3.0.2 211 5/21/2024
3.0.0 194 4/24/2024
2.1.0 223 2/20/2024
2.0.9 203 2/20/2024
2.0.8 725 10/12/2022
2.0.7 625 8/10/2022
2.0.6 604 7/28/2022
2.0.5 730 7/16/2022 2.0.5 is deprecated because it has critical bugs.
2.0.4 597 7/14/2022
2.0.2 625 7/14/2022
1.0.6.2 754 4/10/2021
1.0.6.1 634 3/15/2021
1.0.5 623 2/27/2021
1.0.4 597 2/13/2021
1.0.3 621 2/3/2021
1.0.2 585 2/2/2021
1.0.1 721 2/2/2021 1.0.1 is deprecated.