🔌 HTTP 传输
命名空间: Snet.Core.communication.net.http | 类: HttpClientOperate, HttpServiceOperate
注意: HTTP 传输类直接继承
CoreUnify而非CommunicationAbstract。这是因为它遵循请求-响应模式而非流式连接。
HttpClientOperate — HTTP 客户端
继承: CoreUnify<HttpClientOperate, HttpClientData.Basics>
配置 (HttpClientData.Basics)
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
MaxConnectCount |
int |
255 |
每服务器最大连接数 |
请求模型 (RequestData)
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
Url |
string |
"http://127.0.0.1:6688/api/snet" |
请求 URL |
Headers |
Dictionary<string,string> |
{} |
自定义请求头 |
Method |
HttpMethod |
GET |
HTTP 方法 |
BodyType |
BType 枚举 |
Raw |
FormData / Raw / None |
BodyContent |
object |
null |
Raw: string, FormData: Dictionary |
Encoding |
Encoding |
UTF8 |
内容编码 |
ContentType |
string |
"application/json" |
Content-Type |
TimeOut |
TimeSpan |
60s |
请求超时 |
Proxy |
IWebProxy? |
null |
代理 |
CookieContainer |
CookieContainer |
new() |
Cookie 存储 |
API
// 发送请求 — 核心方法
public async Task<OperateResult> RequestAsync(RequestData requestData, CancellationToken token = default);
// 响应数据 (ResponseData): StatusCode(int), ResHeadersDatas, ResCookieData, ResData(string), ReqData
使用示例
using Snet.Core.communication.net.http.client;
using static Snet.Core.communication.net.http.client.HttpClientData;
var http = await HttpClientOperate.InstanceAsync(new Basics());
// GET
var getResp = await http.RequestAsync(new RequestData {
Url = "https://api.example.com/data", Method = HttpMethod.Get
});
// POST JSON
var postResp = await http.RequestAsync(new RequestData {
Url = "https://api.example.com/data", Method = HttpMethod.Post,
BodyType = BType.Raw, BodyContent = "{\"name\":\"value\"}"
});
// POST Form
var formResp = await http.RequestAsync(new RequestData {
Url = "https://api.example.com/upload", Method = HttpMethod.Post,
BodyType = BType.FormData,
BodyContent = new Dictionary<string, string> { ["field1"] = "value1" }
});
HttpServiceOperate — HTTP 服务端
继承: CoreUnify<HttpServiceOperate, HttpServiceData.Basics> | 接口: IOn, IOff, IGetStatus
配置 (HttpServiceData.Basics,继承 WAModel)
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
Method |
HttpMethod |
GET |
允许的 HTTP 方法 |
ContentType |
string |
"application/json" |
允许的 Content-Type |
AbsolutePaths |
List<string> |
["/api/snet"] |
有效 API 路径 |
CrossDomain |
bool |
继承 | CORS 支持 |
IpAddress |
string |
继承 | 监听地址 |
Port |
int |
继承 | 监听端口 |
API
await server.OnAsync(); // 启动 HttpListener
await server.OffAsync(); // 停止
await server.GetStatusAsync();
// 静态方法 — 写入 JSON 响应
public static async Task WriteAsync<T>(HttpListenerResponse response, T obj, CancellationToken token);
WaitHandler 事件数据
| 字段 | 类型 | 说明 |
|---|---|---|
Request |
HttpListenerRequest |
请求数据 |
Response |
HttpListenerResponse |
响应对象 |
BodyData |
string? |
请求体内容 |
使用示例
using Snet.Core.communication.net.http.service;
var config = new Basics {
IpAddress = "0.0.0.0", Port = 8080, Method = HttpMethod.Post,
CrossDomain = true, AbsolutePaths = new List<string> { "/api/snet", "/api/health" }
};
var server = await HttpServiceOperate.InstanceAsync(config);
server.OnDataEvent += async (s, e) => {
if (e.ResultData is WaitHandler wh) {
Console.WriteLine($"路径: {wh.Request.Url!.AbsolutePath}");
await HttpServiceOperate.WriteAsync(wh.Response, new { status = "ok" });
}
};
await server.OnAsync();
