XYS.Utils.Web
3.0.0
See the version list below for details.
dotnet add package XYS.Utils.Web --version 3.0.0
NuGet\Install-Package XYS.Utils.Web -Version 3.0.0
<PackageReference Include="XYS.Utils.Web" Version="3.0.0" />
<PackageVersion Include="XYS.Utils.Web" Version="3.0.0" />
<PackageReference Include="XYS.Utils.Web" />
paket add XYS.Utils.Web --version 3.0.0
#r "nuget: XYS.Utils.Web, 3.0.0"
#:package XYS.Utils.Web@3.0.0
#addin nuget:?package=XYS.Utils.Web&version=3.0.0
#tool nuget:?package=XYS.Utils.Web&version=3.0.0
XYS.Utils.Web
Web 相关工具类库,提供基于 RestSharp 的高并发 REST 调用封装,以及带超时控制的文件下载客户端。
目标框架
net8.0 · net462 · netcoreapp3.1 · netstandard2.0 · netstandard2.1
依赖
- RestSharp
113.1.0 - RestSharp.Serializers.NewtonsoftJson
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 });
// GET:查询参数用匿名对象(按公共属性展开为 ?id=1&name=test)
var result4 = await HttpRestUtil.GetAsync<ResultDto>(
"https://api.example.com", "/order/detail", new { id = 1, name = "test" });
// GET:查询参数也可用字典
var result5 = HttpRestUtil.GetSync<ResultDto>(
"https://api.example.com", "/order/detail",
new Dictionary<string, object> { ["id"] = 1 });
// GET:无查询参数时省略即可
var result6 = await HttpRestUtil.GetAsync<ResultDto>(
"https://api.example.com", "/order/list");
// 表单提交(multipart/form-data):服务端以表单字段接收,字段值本身可为 JSON 字符串
var result7 = HttpRestUtil.PostFormSync<ResultDto>(
"https://api.example.com", "/gateway",
new Dictionary<string, string>
{
["interfaceName"] = "doctor",
["methodName"] = "getPatientEmrContentById",
["data"] = "{\"recordId\":\"xxx\",\"contentType\":\"html\"}",
});
// 异步同理:await HttpRestUtil.PostFormAsync<ResultDto>(baseUrl, path, fields)
所有方法均支持 timeoutMs 参数(默认 30000ms,传 0 或负数时同样按 30000ms 处理)进行请求级超时隔离,异步方法额外支持 CancellationToken。GET 的查询参数支持匿名对象/实体(按公共可读属性展开)或 IDictionary,值为 null 的参数会自动跳过;参数值与系统区域设置无关地格式化(日期为 ISO 8601 2026-11-19T08:30:00,数值用 . 作小数点,布尔为小写 true/false),集合值按同名键重复展开(ids=1&ids=2)。PostFormSync<T> / PostFormAsync<T> 以 multipart/form-data 提交表单字段,适配「服务端以表单字段接收、字段值为 JSON 字符串」的统一网关类接口(字段 UTF-8 编码,Value 为 null 时按空串提交)。
异常语义
| 情形 | 抛出的异常 |
|---|---|
调用方通过 CancellationToken 取消 |
OperationCanceledException |
超过 timeoutMs |
TimeoutException |
| 非 2xx、网络错误、响应反序列化失败 | Exception(InnerException 为底层异常) |
失败时日志与异常消息中的响应内容最多保留 1024 个字符,超出部分截断,避免敏感数据与大段错误页整段写入日志。
序列化
POST 对象请求体以紧凑格式(无缩进)序列化;传入字符串时视为已序列化好的 JSON 原样发送。
内部统一使用 Newtonsoft.Json(通过 RestSharp.Serializers.NewtonsoftJson 配置),与 XYS.Utils.Sys 的 HttpJsonUtil 保持一致:响应实体上的 [JsonProperty] 特性生效,模型可在两个工具类间无差别复用(RestSharp v107+ 默认使用 System.Text.Json,不识别 [JsonProperty],故在此显式切换)。
认证 Token 注入
为避免本程序集反向引用上层 UI/会话程序集,认证 Token 通过委托注入(通常在应用启动时设置一次):
// 例如在 NovaUI 启动时:
HttpRestUtil.AuthTokenProvider = () => UserSession.Current?.Token;
设置后,每次请求会自动附加 Authorization: Bearer {token} 头;返回 null/空 时不附加(兼容未登录场景)。
自定义请求头
支持两种方式,可叠加使用(同名头以单次请求的值为准):
// 方式一:全局默认头(启动时注入一次,附加到每次请求)
HttpRestUtil.DefaultHeaderProvider = () => new Dictionary<string, string>
{
["X-Api-Key"] = "your-api-key",
["X-Tenant"] = "hospital-001",
};
// 方式二:单次请求头(各方法末尾的 headers 参数,POST/GET 同步异步均支持)
var result = await HttpRestUtil.PostAsync<ResultDto>(
"https://api.example.com", "/order/create", new { id = 1 },
headers: new Dictionary<string, string> { ["X-Request-Id"] = Guid.NewGuid().ToString() });
- 内部使用
AddOrUpdateHeader:同名头覆盖而非追加,因此单次请求头可覆盖全局默认头。 Key为空或Value为null的项自动跳过;全局头读取抛异常时不阻断请求。
TLS 证书校验
// 默认 false:执行标准 TLS 证书校验,防止中间人截获 Token 与业务数据(与 HttpJsonUtil 一致)
// 内网自签名/过期证书部署需在启动时显式放行:
HttpRestUtil.AllowUntrustedCertificate = true;
高并发说明
RestClient按baseUrl通过ConcurrentDictionary<string, Lazy<RestClient>>缓存,保证同一地址仅创建一次实例,正确复用连接池。- 超时在请求级设置,各请求互不干扰。
- 同步方法(
GetSync/PostSync/PostFormSync)会监控线程池可用工作线程数,当可用线程过低时记录告警,并对"线程池饥饿假超时"与"真实网络超时"做区分诊断,便于高并发排障。
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单位为毫秒,作用于底层请求以及"无进度即超时"的空闲计时。- 下载过程中每收到一次数据即重置计时;在超时时间内无任何数据到达则自动取消下载。
- 下载结束(成功、失败或取消)即停止计时,同一实例可安全复用于后续下载。
CalculateTime.Stop()只停止计时,不会触发TimeOver;重复Start()只保留最新一次计时。
版本
当前版本 3.0.0。
3.0.0 不兼容变更
AllowUntrustedCertificate默认值由true改为false:使用自签名证书的内网部署须在启动时显式设为true。- 取消改为抛
OperationCanceledException,超时改为抛TimeoutException(两者均派生自Exception,原有catch (Exception)不受影响)。 - 查询参数改为与区域设置无关的格式(见上文),布尔值由
True变为true。 - 删除遗留的
XYS.Utils.Web.Net462.csproj(net462 由 SDK 项目多目标覆盖)。
| Product | Versions 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. |
-
.NETCoreApp 3.1
- RestSharp (>= 113.1.0)
- RestSharp.Serializers.NewtonsoftJson (>= 113.1.0)
- XYS (>= 2.3.1)
- XYS.Utils.Log4Net (>= 2.6.0)
- XYS.Utils.Sys (>= 2.7.0)
-
.NETFramework 4.6.2
- RestSharp (>= 113.1.0)
- RestSharp.Serializers.NewtonsoftJson (>= 113.1.0)
- XYS (>= 2.3.1)
- XYS.Utils.Log4Net (>= 2.6.0)
- XYS.Utils.Sys (>= 2.7.0)
-
.NETStandard 2.0
- RestSharp (>= 113.1.0)
- RestSharp.Serializers.NewtonsoftJson (>= 113.1.0)
- XYS (>= 2.3.1)
- XYS.Utils.Log4Net (>= 2.6.0)
- XYS.Utils.Sys (>= 2.7.0)
-
.NETStandard 2.1
- RestSharp (>= 113.1.0)
- RestSharp.Serializers.NewtonsoftJson (>= 113.1.0)
- XYS (>= 2.3.1)
- XYS.Utils.Log4Net (>= 2.6.0)
- XYS.Utils.Sys (>= 2.7.0)
-
net8.0
- RestSharp (>= 113.1.0)
- RestSharp.Serializers.NewtonsoftJson (>= 113.1.0)
- XYS (>= 2.3.1)
- XYS.Utils.Log4Net (>= 2.6.0)
- XYS.Utils.Sys (>= 2.7.0)
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 |
|---|---|---|
| 3.0.1 | 84 | 11/30/2026 |
| 3.0.0 | 94 | 11/28/2026 |
| 2.5.0 | 177 | 7/22/2026 |
| 2.4.0 | 144 | 7/21/2026 |
| 2.3.2 | 143 | 7/21/2026 |
| 2.3.1 | 138 | 7/21/2026 |
| 2.1.0.4 | 208 | 1/8/2026 |
| 2.1.0.3 | 179 | 1/7/2026 |
| 2.1.0.2 | 706 | 5/7/2022 |
| 2.1.0.1 | 1,956 | 1/23/2022 |
| 2.0.2.2 | 887 | 12/21/2021 |
| 2.0.2.1 | 565 | 12/20/2021 |
| 2.0.1.2 | 553 | 12/20/2021 |
| 2.0.1.1 | 6,076 | 4/22/2021 |
| 1.0.1.1 | 761 | 5/25/2020 |
| 1.0.0.10 | 1,229 | 5/27/2020 |