Tom.HttpLib.Client 1.1.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Tom.HttpLib.Client --version 1.1.2                
NuGet\Install-Package Tom.HttpLib.Client -Version 1.1.2                
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.Client" Version="1.1.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tom.HttpLib.Client --version 1.1.2                
#r "nuget: Tom.HttpLib.Client, 1.1.2"                
#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.
// Install Tom.HttpLib.Client as a Cake Addin
#addin nuget:?package=Tom.HttpLib.Client&version=1.1.2

// Install Tom.HttpLib.Client as a Cake Tool
#tool nuget:?package=Tom.HttpLib.Client&version=1.1.2                

HttpLib 便捷的Http库

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

🖥支持环境

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

🌴支持

multipart/form-data

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

上传与下载进度回调

上传与下载的进度监控

支持缓存

类似图片加载场景,同一个id的图片通过磁盘存储减少网络开支

工厂创建

默认关闭 使用 HttpClientFactory 池,可以最大程度上节省系统重复请求开支

Config.UsePool = true;

✨示例

示例

创建请求

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(new { test1 = "测试1", test2 = "测试2" })
data(new { wd = new string[] { "GitHub - Haku-Men HttpLib", "POST数组参数" } })
query(new { test = "POST下继续传递URL参数" })
query(new Val("test", "POST下继续传递URL参数1"))

支持Class模型 POST Json 需要自己编程

data(new MyModel{ id = "id参数", file=new Files(@"文件地址") })
data(new Val("test1", "测试1"), new Val("test2", "测试2"))
data(new List<Val> {
	new Val("test1","测试1"),
	new Val("test2","测试2")
})
string json = "{\"JSON\":\"json data\"}";
datastr(json,"application/json")
data(new Files("文件地址"))
file(@"文件地址")

添加请求头

header(new { Accept = "*/*", Token = "test" })
header(new Val("Accept","*/*"), new Val("User-Agent","Chrome"))

启用重定向

默认禁止

redirect(true)

设置超时时长

毫秒(默认100秒)

timeout(3000)

设置编码

默认utf-8

encoding("utf-8")

设置缓存

先配置Config.CacheFolder缓存文件夹

cache("缓存id")

或者设定有效期 1分钟

cache("缓存id",1)

请求之前处理

before((HttpCore r) =>
{
	return true; //继续请求
})

请求之后处理

after((HttpCore r, HttpResponseMessage msg) =>
{
	return true; //继续下载数据
})

注入回调获取进度

字节大小

上传

requestProgres(prog => {
	Console.Write("{0}% 上传", prog);
})

下载

responseProgres((bytesSent, totalBytes) => {
	if (totalBytes.HasValue)
	{
		double prog = (bytesSent * 1.0) / (totalBytes.Value * 1.0);
		Console.Write("{0}% 下载", Math.Round(prog * 100.0, 1).ToString("N1"));
	}
})

请求

方法全异步

requestNone();//仅请求
request();//返回字符串
requestData();//返回字节
download("保存目录", "保存文件名称(为空自动获取)");//下载文件

实例1

异步

Config.UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1 Edg/108.0.0.0";

Http.Get("https://www.baidu.com/s")
.data(new { wd = "GitHub - Haku-Men HttpLib" })
.redirect(true)
.requestProgres(prog => {
    Console.Write("{0}% 上传", prog);
})
.responseProgres((bytesSent, totalBytes) => {
    if (totalBytes.HasValue)
    {
        double prog = (bytesSent * 1.0) / (totalBytes.Value * 1.0);
        Console.Write("{0}% 下载", Math.Round(prog * 100.0, 1).ToString("N1"));
    }
})
.request().ContinueWith((data) => {
    Console.WriteLine(data.Result.Data);
});

实例2

同步

var html = Http.Get("https://www.baidu.com/s")
.data(new { wd = "GitHub - Haku-Men HttpLib" })
.redirect(true)
.requestProgres(prog => {
	Console.Write("{0}% 上传", prog);
})
.responseProgres((bytesSent, totalBytes) => {
	if (totalBytes.HasValue)
	{
		double prog = (bytesSent * 1.0) / (totalBytes.Value * 1.0);
		Console.Write("{0}% 下载", Math.Round(prog * 100.0, 1).ToString("N1"));
	}
})
.request().Result;
Console.WriteLine(html.Data);

实例下载文件

Http.Get("https://dldir1.qq.com/qqfile/qq/PCQQ9.7.3/QQ9.7.3.28946.exe")
       .redirect(true)
       .responseProgres((bytesSent, totalBytes) =>
       {
           Console.SetCursorPosition(0, 0);
           if (totalBytes.HasValue)
           {
               double prog = (bytesSent * 1.0) / (totalBytes.Value * 1.0);
               Console.Write("{0}% 下载 {1}/{2}                  ", Math.Round(prog * 100.0, 1).ToString("N1"), CountSize(bytesSent), CountSize(totalBytes.Value));
           }
           else
           {
               Console.Write("{0} 下载            ", CountSize(bytesSent));
           }
       }).download(@"C:\Users\admin\Desktop").ContinueWith(savapath =>
       {
           if (savapath.Result != null)
           {
               Console.WriteLine("下载成功保存至:" + savapath.Result.Data);
           }
           else
           {
               Console.WriteLine("下载失败");
           }
       }).Wait();

实例获取域名IP

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

WebResult介绍

代码 解释 说明
OK 是否成功响应 true 正常响应
StatusCode 状态代码 200 为正常 常见的有404未找到、302重定向、502网址报错
Type 服务指示类型 Content-Type
Header 响应头
HeaderContent 内容响应头
Exception 错误异常
Data 响应内容
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 is compatible.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  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.
  • .NETCoreApp 3.1

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.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
1.1.3 205 8/11/2023
1.1.2 172 5/4/2023
1.1.1 240 3/8/2023
1.1.0 232 3/8/2023
1.0.0 260 12/19/2022

使用HttpClient重构,不建议在新的框架继续使用HttpWebRequest