WebExtensions 1.0.0.3

Suggested Alternatives

WodToolKit 1.0.1.4

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

WebExtensions

WebExtensions 是一个轻量级的 .NET 工具库,提供 HTTP 请求处理、Cookie 管理、JSON 解析和实用工具功能,专为 .NET 应用程序的网络扩展和开发需求设计。

主要功能

1. HTTP 请求处理

  • 支持 GET、POST 等多种 HTTP 方法
  • 文件上传功能
  • 自定义请求头管理(包括临时请求头)
  • 代理设置支持
  • SSL 证书验证选项
  • Cookie 管理集成
  • 灵活的 Cookie 设置和获取
  • 从 CoreWebView2CookieManager 导入/导出 Cookie
  • 批量 Cookie 操作
  • Cookie 字符串解析和处理
  • Cookie 存在性检查

3. JSON 处理

  • JSON 字符串验证
  • 动态对象解析
  • JSON 序列化和反序列化
  • 数组和对象解析

4. 实用工具功能

  • URL 参数排序(按 ASCII 码顺序)
  • 查询字符串转换为字典
  • 字典转换为查询字符串
  • URL 全路径参数排序
  • 随机码生成
  • 时间戳获取
  • URL 参数解析(支持多个等号的情况)

技术特点

  • 目标框架:.NET Standard 2.1(兼容 .NET Core 3.0+ 和 .NET 5.0+)
  • 依赖少:主要依赖 System.Text.Json
  • 易用性:提供流畅的 API 设计
  • 安全性:包含安全警告和最佳实践提示

安装

NuGet 包安装

dotnet add package WebExtensions
# 或在 Visual Studio 的 NuGet 包管理器中搜索 "WebExtensions"

手动引用

  1. 下载最新的 NuGet 包
  2. 在 Visual Studio 中右键项目 → 管理 NuGet 包 → 浏览 → 上传本地包

使用示例

using WebExtensions.src;

// 创建 Cookie 管理器
var cookieManager = new CookieManager();

// 设置单个 Cookie
cookieManager.SetCookie("sessionId", "abc123");

// 批量设置 Cookie(通过字典)
var cookiesDict = new Dictionary<string, string>
{
    {"user", "admin"},
    {"token", "xyz789"}
};
cookieManager.SetCookie(cookiesDict);

// 通过字符串批量设置 Cookie
cookieManager.SetCookie("user=admin; token=xyz789; loggedIn=true");

// 从 WebView2 导入 Cookie(异步)
await cookieManager.ImportFromWebView2Async(webViewCookieManager);

// 导出 Cookie 到 WebView2(异步)
await cookieManager.ExportToWebView2Async(webViewCookieManager);

// 获取 Cookie 值
string tokenValue = cookieManager.GetCookieValue("token");

// 检查 Cookie 是否存在
bool hasSessionCookie = cookieManager.HasCookie("sessionId");

HTTP 请求

using WebExtensions.src;

// 创建 HTTP 请求对象
var http = new HttpRequestClass();

// 设置请求参数
http.SetUrl("https://api.example.com/data")
    .SetMethod("POST")
    .SetHeaders(new Dictionary<string, string>
    {
        {"Content-Type", "application/json"},
        {"Authorization", "Bearer token123"}
    })
    .SetBody(JsonSerializer.Serialize(new { id = 1, name = "Test" }));

// 发送请求
var response = await http.SendAsync();

// 获取响应内容
var responseContent = response.Content;
var statusCode = response.StatusCode;

JSON 处理

using WebExtensions.src;

// 验证 JSON 字符串
string jsonString = "{\"name\": \"John\", \"age\": 30}";
if (EasyJson.IsValidJson(jsonString))
{
    // 解析为动态对象
    dynamic data = EasyJson.ParseJsonToDynamic(jsonString);
    string name = data.name;
    int age = data.age;
}

实用工具功能

using WebExtensions.src;

// URL 参数排序(按 ASCII 码)
string queryString = "b=value2&a=value1&c=value3";
string sortedQuery = Utility.SortUrlParameters(queryString);
// 结果: a=value1&b=value2&c=value3

// 全URL参数排序
string url = "https://example.com/api?b=2&a=1";
string sortedUrl = Utility.SortUrlParametersInFullUrl(url);
// 结果: https://example.com/api?a=1&b=2

// 查询字符串转字典
Dictionary<string, string> parameters = Utility.QueryStringToDictionary("name=John&age=30");

// 字典转查询字符串
string query = Utility.DictionaryToQueryString(parameters);

// 生成随机码
string randomCode = Utility.refreshCode();

// 获取当前时间戳(13位)
string timestamp = Utility.GetTimeStamp();

// 解析URL中的查询参数
Dictionary<string, string> urlParams = Utility.ParseQueryString("https://example.com/api?name=John&age=30");

// 获取URL中的查询字符串部分
string queryOnly = Utility.GetQueryString("https://example.com/api?name=John&age=30");
// 结果: name=John&age=30

项目结构

WebExtensions/
├── src/
│   ├── Http.cs           # HTTP 请求和 Cookie 管理实现
│   ├── Json.cs           # JSON 处理实现
│   └── Utility.cs        # 实用工具类,包含URL处理、时间戳等功能
├── WebExtensions.csproj  # 项目配置文件
└── README.md             # 项目文档

注意事项

  1. SSL 证书验证:在生产环境中,建议保持 SSL 证书验证启用(默认设置),仅在测试环境中禁用。

  2. 异步操作:库中包含异步方法,建议在使用时正确处理异步操作。

  3. 错误处理:实现时请添加适当的异常处理机制。

贡献

欢迎提交 Issues 和 Pull Requests 来帮助改进这个库。

许可证

MIT License

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 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.  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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.