XYS.Utils.Web 2.3.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package XYS.Utils.Web --version 2.3.1
                    
NuGet\Install-Package XYS.Utils.Web -Version 2.3.1
                    
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="XYS.Utils.Web" Version="2.3.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="XYS.Utils.Web" Version="2.3.1" />
                    
Directory.Packages.props
<PackageReference Include="XYS.Utils.Web" />
                    
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 XYS.Utils.Web --version 2.3.1
                    
#r "nuget: XYS.Utils.Web, 2.3.1"
                    
#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 XYS.Utils.Web@2.3.1
                    
#: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=XYS.Utils.Web&version=2.3.1
                    
Install as a Cake Addin
#tool nuget:?package=XYS.Utils.Web&version=2.3.1
                    
Install as a Cake Tool

XYS.Utils.Web

Web 相关工具类库,提供基于 RestSharp 的高并发 REST 调用封装,以及带超时控制的文件下载客户端。

目标框架

net8.0 · net462 · netcoreapp3.1 · netstandard2.0 · netstandard2.1

依赖

  • RestSharp 113.1.0
  • XYS.Utils.Log4Net、XYS.Utils.Sys、XYS(同解决方案内项目引用)

HttpRestUtil

基于 RestSharp 的静态 REST 工具类,命名空间 XYS.Utils.Web。按 baseUrl 复用 RestClient(线程安全缓存),适用于高并发场景。

基本用法

using XYS.Utils.Web;

// 异步:对象体(内部自动序列化为 JSON)
var result = await HttpRestUtil.PostAsync<ResultDto>(
    "https://api.example.com", "/order/create", new { id = 1, name = "test" });

// 异步:直接传 JSON 字符串
var result2 = await HttpRestUtil.PostAsync<ResultDto>(
    "https://api.example.com", "/order/create", "{\"id\":1}");

// 同步(内部已做并发与超时隔离,失败会抛异常而非返回 null)
var result3 = HttpRestUtil.PostSync<ResultDto>(
    "https://api.example.com", "/order/create", new { id = 1 });

所有方法均支持 timeoutMs 参数(默认 30000ms)进行请求级超时隔离,异步方法额外支持 CancellationToken

认证 Token 注入

为避免本程序集反向引用上层 UI/会话程序集,认证 Token 通过委托注入(通常在应用启动时设置一次):

// 例如在 NovaUI 启动时:
HttpRestUtil.AuthTokenProvider = () => UserSession.Current?.Token;

设置后,每次请求会自动附加 Authorization: Bearer {token} 头;返回 null/空 时不附加(兼容未登录场景)。

TLS 证书校验

// 默认 true:兼容内网自签名/过期证书部署
// 生产环境建议置为 false 以启用标准 TLS 校验,防止中间人攻击
HttpRestUtil.AllowUntrustedCertificate = false;

高并发说明

  • RestClientbaseUrl 通过 ConcurrentDictionary<string, Lazy<RestClient>> 缓存,保证同一地址仅创建一次实例,正确复用连接池。
  • 超时在请求级设置,各请求互不干扰。
  • PostSync 会监控线程池可用工作线程数,当可用线程过低时记录告警,并对"线程池饥饿假超时"与"真实网络超时"做区分诊断,便于高并发排障。

NovaWebClient

继承自 WebClient 的下载客户端,命名空间 XYS.Utils.Web,支持"无数据接收即超时取消"的文件下载。

using XYS.Utils.Web;

var client = new NovaWebClient { Timeout = 5000 }; // 毫秒
client.DownloadFileAsyncWithTimeout(new Uri("https://.../big.zip"), "big.zip", null);
  • Timeout 单位为毫秒,作用于底层请求以及"无进度即超时"的空闲计时。
  • 下载过程中每收到一次数据即重置计时;在超时时间内无任何数据到达则自动取消下载。

提示:XYS.Utils.Sys 中提供了一个基于 HttpClient 的现代化下载客户端(同名 NovaWebClient),新代码推荐优先使用该版本;本类保留用于兼容旧调用。


版本

当前版本 2.3.1

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  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.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on XYS.Utils.Web:

Package Downloads
XYS.Lab.Report

Description

XYS.CDR.Lab

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.5.0 104 7/22/2026
2.4.0 102 7/21/2026
2.3.2 100 7/21/2026
2.3.1 99 7/21/2026
2.1.0.4 178 1/8/2026
2.1.0.3 150 1/7/2026
2.1.0.2 677 5/7/2022
2.1.0.1 1,932 1/23/2022
2.0.2.2 867 12/21/2021
2.0.2.1 535 12/20/2021
2.0.1.2 531 12/20/2021
2.0.1.1 6,049 4/22/2021
1.0.1.1 737 5/25/2020
1.0.0.10 1,203 5/27/2020