-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
新增了 `DHWeb` 类中的 `Client` 方法,用于创建 `Pek.Webs.Clients.WebClient` 实例,支持发送 HTTP 请求。 新增了 `HttpContentType` 枚举,定义了多种 HTTP 内容类型,包括 `FormUrlEncoded`、`Json`、`FormData` 和 `Xml`。 新增了 `HttpRequest` 类,继承自 `HttpRequestBase<IHttpRequest>`,实现了 `IHttpRequest` 接口,支持 HTTP 请求的成功回调和 JSON 结果的获取。 新增了泛型类 `HttpRequest<TResult>`,继承自 `HttpRequestBase<IHttpRequest<TResult>>`,实现了 `IHttpRequest<TResult>` 接口,支持 HTTP 请求的成功回调和 JSON 结果的获取,并提供了结果转换功能。 新增了 `HttpRequestBase<TRequest>` 抽象类,作为 HTTP 请求的基类,提供了设置字符编码、内容类型、Cookie、超时时间、请求头、参数、文件参数、失败回调、忽略 SSL、设置 Bearer 令牌等功能,并实现了发送请求和处理响应的逻辑。 新增了 `IHttpRequest` 接口,定义了 HTTP 请求的成功回调和获取 JSON 结果的方法。 新增了泛型接口 `IHttpRequest<TResult>`,定义了 HTTP 请求的成功回调和获取 JSON 结果的方法,并提供了结果转换功能。 在 `IRequest.cs` 文件中,添加了 `using System.Net;` 和 `using System.Text;` 引用,并定义了 `Pek.Webs.Clients` 命名空间。新增了一个泛型接口 `IRequest<out TRequest>`,包含了设置字符编码、内容类型、Cookie、Bearer令牌、超时时间、请求头、参数、文件参数、请求失败回调函数、忽略SSL和获取结果的相关方法。 在 `WebClient.cs` 文件中,定义了 `Pek.Webs.Clients` 命名空间。新增了 `WebClient` 类,包含 `Get`、`Post`、`Put` 和 `Delete` 请求方法。还新增了一个泛型类 `WebClient<TResult>`,同样包含 `Get`、`Post`、`Put` 和 `Delete` 请求方法,但返回类型为 `IHttpRequest<TResult>`。 在 `HttpClientBuilderFactory.cs` 文件中,添加了 `using System.Collections.Concurrent;` 和 `using System.Text.RegularExpressions;` 引用,并定义了 `Pek.Webs.Clients.Internal` 命名空间。新增了一个静态内部类 `HttpClientBuilderFactory`,包含一个用于存储 `HttpClient` 实例的并发字典 `_httpClients` 和一个用于匹配域名的正则表达式 `_domainRegex`。该类提供了创建 `HttpClient` 实例的方法 `CreateClient`,通过URL获取域名的方法 `GetDomainByUrl`,以及创建 `HttpClient` 实例的私有方法 `Create`。 在 `IFileParameter.cs` 文件中,定义了 `Pek.Webs.Clients.Parameters` 命名空间。新增了一个接口 `IFileParameter`,包含获取文件流、文件名称和参数名的方法,并继承了 `IDisposable` 接口。 在 `PhysicalFileParameter.cs` 文件中,定义了 `Pek.Webs.Clients.Parameters` 命名空间。新增了一个类 `PhysicalFileParameter`,实现了 `IFileParameter` 接口。该类包含一个文件流 `_stream`,一个绝对路径 `AbsolutePath` 和一个参数名 `Name`。提供了两个构造函数用于初始化实例,并实现了 `Dispose` 方法释放资源,以及获取文件流、文件名和参数名的方法。
- Loading branch information
猿人易
committed
Dec 2, 2024
1 parent
249674f
commit 3784e54
Showing
10 changed files
with
1,182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.ComponentModel; | ||
|
||
namespace Pek.Webs.Clients; | ||
|
||
/// <summary> | ||
/// Http 内容类型 | ||
/// </summary> | ||
public enum HttpContentType | ||
{ | ||
/// <summary> | ||
/// Form格式:application/x-www-form-urlencoded | ||
/// </summary> | ||
[Description("application/x-www-form-urlencoded")] | ||
FormUrlEncoded, | ||
|
||
/// <summary> | ||
/// JSON格式:application/json | ||
/// </summary> | ||
[Description("application/json")] | ||
Json, | ||
|
||
/// <summary> | ||
/// 表单文件上传:multipart/form-data | ||
/// </summary> | ||
[Description("multipart/form-data")] | ||
FormData, | ||
|
||
/// <summary> | ||
/// XML格式:text/xml | ||
/// </summary> | ||
[Description("text/xml")] | ||
Xml | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
using System.Net; | ||
|
||
using NewLife.Serialization; | ||
|
||
using Pek.Helpers; | ||
|
||
namespace Pek.Webs.Clients; | ||
|
||
/// <summary> | ||
/// Http请求 | ||
/// </summary> | ||
public class HttpRequest : HttpRequestBase<IHttpRequest>, IHttpRequest | ||
{ | ||
/// <summary> | ||
/// 执行成功的回调函数 | ||
/// </summary> | ||
private Action<String>? _successAction; | ||
|
||
/// <summary> | ||
/// 执行成功的回调函数 | ||
/// </summary> | ||
private Action<String, HttpStatusCode>? _successStatusCodeAction; | ||
|
||
/// <summary> | ||
/// 初始化一个<see cref="HttpRequest"/>类型的实例 | ||
/// </summary> | ||
/// <param name="httpMethod">Http请求方法</param> | ||
/// <param name="url">请求地址</param> | ||
public HttpRequest(HttpMethod httpMethod, String url) : base(httpMethod, url) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// 请求成功回调函数 | ||
/// </summary> | ||
/// <param name="action">执行成功的回调函数,参数为响应结果</param> | ||
public IHttpRequest OnSuccess(Action<String> action) | ||
{ | ||
_successAction = action; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// 请求成功回调函数 | ||
/// </summary> | ||
/// <param name="action">执行成功的回调函数,第一个参数为响应结果,第二个参数为状态码</param> | ||
public IHttpRequest OnSuccess(Action<String, HttpStatusCode> action) | ||
{ | ||
_successStatusCodeAction = action; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// 成功处理操作 | ||
/// </summary> | ||
/// <param name="result">结果</param> | ||
/// <param name="statusCode">状态码</param> | ||
/// <param name="contentType">内容类型</param> | ||
protected override void SuccessHandler(String result, HttpStatusCode statusCode, String? contentType) | ||
{ | ||
_successAction?.Invoke(result); | ||
_successStatusCodeAction?.Invoke(result, statusCode); | ||
} | ||
|
||
/// <summary> | ||
/// 获取Json结果 | ||
/// </summary> | ||
/// <typeparam name="TResult">返回结果类型</typeparam> | ||
public async Task<TResult> ResultFromJsonAsync<TResult>() | ||
{ | ||
var result = await ResultAsync().ConfigureAwait(false); | ||
return JsonHelper.ToJsonEntity<TResult>(result) ?? throw new InvalidOperationException("JsonHelper returned null"); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Http请求 | ||
/// </summary> | ||
/// <typeparam name="TResult">结果类型</typeparam> | ||
public class HttpRequest<TResult> : HttpRequestBase<IHttpRequest<TResult>>, IHttpRequest<TResult> | ||
where TResult : class | ||
{ | ||
/// <summary> | ||
/// 执行成功的回调函数 | ||
/// </summary> | ||
private Action<TResult?>? _successAction; | ||
|
||
/// <summary> | ||
/// 执行成功的回调函数 | ||
/// </summary> | ||
private Action<TResult?, HttpStatusCode>? _successStatusCodeAction; | ||
|
||
/// <summary> | ||
/// 执行成功的转换函数 | ||
/// </summary> | ||
private Func<String, TResult>? _convertAction; | ||
|
||
/// <summary> | ||
/// 初始化一个<see cref="HttpRequest{TResult}"/>类型的实例 | ||
/// </summary> | ||
/// <param name="httpMethod">Http请求方法</param> | ||
/// <param name="url">请求地址</param> | ||
public HttpRequest(HttpMethod httpMethod, String url) : base(httpMethod, url) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// 请求成功回调函数 | ||
/// </summary> | ||
/// <param name="action">执行成功的回调函数,参数为响应结果</param> | ||
/// <param name="convertAction">将结果字符串转换为指定类型,当默认转换实现无法转换时使用</param> | ||
public IHttpRequest<TResult> OnSuccess(Action<TResult?> action, Func<String, TResult>? convertAction = null) | ||
{ | ||
_successAction = action; | ||
_convertAction = convertAction; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// 请求成功回调函数 | ||
/// </summary> | ||
/// <param name="action">执行成功的回调函数,第一个参数为响应结果,第二个参数为状态码</param> | ||
/// <param name="convertAction">将结果字符串转换为指定类型,当默认转换实现无法转换时使用</param> | ||
public IHttpRequest<TResult> OnSuccess(Action<TResult?, HttpStatusCode> action, Func<String, TResult>? convertAction = null) | ||
{ | ||
_successStatusCodeAction = action; | ||
_convertAction = convertAction; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// 成功处理操作 | ||
/// </summary> | ||
/// <param name="result">结果</param> | ||
/// <param name="statusCode">状态码</param> | ||
/// <param name="contentType">内容类型</param> | ||
protected override void SuccessHandler(String result, HttpStatusCode statusCode, String? contentType) | ||
{ | ||
var successResult = ConvertTo(result, contentType); | ||
_successAction?.Invoke(successResult); | ||
_successStatusCodeAction?.Invoke(successResult, statusCode); | ||
} | ||
|
||
/// <summary> | ||
/// 将结果字符串转换为指定类型 | ||
/// </summary> | ||
/// <param name="result">结果</param> | ||
/// <param name="contentType">内容类型</param> | ||
private TResult? ConvertTo(String result, String? contentType) | ||
{ | ||
if (typeof(TResult) == typeof(String)) | ||
return Conv.To<TResult>(result); | ||
if (_convertAction != null) | ||
return _convertAction(result); | ||
if (contentType.SafeString().Equals("application/json", StringComparison.CurrentCultureIgnoreCase)) | ||
return JsonHelper.ToJsonEntity<TResult>(result); | ||
return null; | ||
} | ||
|
||
/// <summary> | ||
/// 获取Json结果 | ||
/// </summary> | ||
public async Task<TResult> ResultFromJsonAsync() | ||
{ | ||
var result = await ResultAsync().ConfigureAwait(false); | ||
return JsonHelper.ToJsonEntity<TResult>(result) ?? throw new InvalidOperationException("JsonHelper returned null"); | ||
} | ||
} |
Oops, something went wrong.