From 3f21f5432a6751a16f91f99bedbe420fa469e833 Mon Sep 17 00:00:00 2001 From: Sam <315859133@qq.com> Date: Wed, 5 Nov 2025 19:50:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0HttpClient=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=99=A8=E5=B9=B6=E6=9B=B4=E6=96=B0Dockerfil?= =?UTF-8?q?e=E6=97=B6=E5=8C=BA=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DefaultHttpLoggingInterceptor.cs | 22 ++++++++++++ AutoDispathingWork/Dockerfile | 1 + .../Dto/QueryRequest/QueryReq.cs | 9 +++-- AutoDispathingWork/Program.cs | 6 +++- AutoDispathingWork/Services/SpiderServices.cs | 35 ++++++++++++++----- 5 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 AutoDispathingWork/DefaultHttpLoggingInterceptor.cs diff --git a/AutoDispathingWork/DefaultHttpLoggingInterceptor.cs b/AutoDispathingWork/DefaultHttpLoggingInterceptor.cs new file mode 100644 index 0000000..de2a9dd --- /dev/null +++ b/AutoDispathingWork/DefaultHttpLoggingInterceptor.cs @@ -0,0 +1,22 @@ +namespace AutoDispathingWork; + +public class HttpClientLogHandler(ILogger logger) : DelegatingHandler +{ + protected override async Task SendAsync(HttpRequestMessage request, + CancellationToken cancellationToken) + { + if (request.Content != null) + { + var content = await request.Content.ReadAsStringAsync(cancellationToken); + logger.LogInformation("Request: {}", + "RequestUri:" + request.RequestUri + content + !); + } + + var response = await base.SendAsync(request, cancellationToken); + logger.LogInformation(request.RequestUri + ":Response{Response}", + await response.Content.ReadAsStringAsync(cancellationToken)); + + return response; + } +} \ No newline at end of file diff --git a/AutoDispathingWork/Dockerfile b/AutoDispathingWork/Dockerfile index cb29ecc..a7a38ee 100644 --- a/AutoDispathingWork/Dockerfile +++ b/AutoDispathingWork/Dockerfile @@ -3,5 +3,6 @@ FROM 192.168.100.10:5000/dotnet/aspnet:8.0 AS base WORKDIR /app EXPOSE 80 EXPOSE 443 +ENV TZ=Asia/Shanghai COPY ./release . ENTRYPOINT ["dotnet", "AutoDispathingWork.dll"] \ No newline at end of file diff --git a/AutoDispathingWork/Dto/QueryRequest/QueryReq.cs b/AutoDispathingWork/Dto/QueryRequest/QueryReq.cs index 41e7b88..5adeb88 100644 --- a/AutoDispathingWork/Dto/QueryRequest/QueryReq.cs +++ b/AutoDispathingWork/Dto/QueryRequest/QueryReq.cs @@ -9,6 +9,11 @@ public class QueryReq [JsonPropertyName("pageSize")] public int PageSize { get; set; } [JsonPropertyName("sortType")] public int[]? SortType { get; set; } - [JsonPropertyName("createStartTime")] public long CreateStartTime { get; set; } - [JsonPropertyName("createEndTime")] public long CreateEndTime { get; set; } + [JsonPropertyName("createStartTime")] public long? CreateStartTime { get; set; } + [JsonPropertyName("createEndTime")] public long? CreateEndTime { get; set; } + + + + [JsonPropertyName("handleStartTime")] public long? HandleStartTime { get; set; } + [JsonPropertyName("handleEndTime")] public long? HandleEndTime { get; set; } } \ No newline at end of file diff --git a/AutoDispathingWork/Program.cs b/AutoDispathingWork/Program.cs index 1a2240e..c03fba1 100644 --- a/AutoDispathingWork/Program.cs +++ b/AutoDispathingWork/Program.cs @@ -1,8 +1,10 @@ +using AutoDispathingWork; using AutoDispathingWork.Utils; using LiteDB; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.Extensions.FileProviders; +using Microsoft.Extensions.Http.Logging; using Microsoft.OpenApi.Models; using Npgsql; using WorkerService1; @@ -47,7 +49,9 @@ builder.Services.AddSwaggerGen(); builder.Services.AddSingleton(x => new LiteDatabase(builder.Configuration["Database:ConnectionString"])); builder.Services.AddSingleton(config); builder.Services.Configure(config.GetSection("ClientOptions")); -builder.Services.AddHttpClient(); +builder.Services.AddSingleton(); +builder.Services.AddHttpClient("Default") + .AddHttpMessageHandler(); builder.Services.AddMemoryCache(); builder.Services.AddLogging(); // builder.Services.AddMvcCore(); diff --git a/AutoDispathingWork/Services/SpiderServices.cs b/AutoDispathingWork/Services/SpiderServices.cs index fb872ae..aa40cdb 100644 --- a/AutoDispathingWork/Services/SpiderServices.cs +++ b/AutoDispathingWork/Services/SpiderServices.cs @@ -1,5 +1,7 @@ using System.Net.Http.Json; using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; using AutoDispathingWork.Utils; using LiteDB; using Microsoft.EntityFrameworkCore; @@ -43,7 +45,7 @@ public class SpiderServices public async Task> Login() { using var scope = _serviceProvider.CreateScope(); - using var client = _httpClientFactory.CreateClient(); + using var client = _httpClientFactory.CreateClient("Default"); var getClientOptions = await scope.ServiceProvider.GetService()? .GetClientOptions(); var loginApi = getClientOptions.LoginUrl; @@ -114,7 +116,7 @@ public class SpiderServices // "referrer": "http://33.10.72.100/megcity/", // "referrerPolicy": "strict-origin-when-cross-origin", var token = await GetToken(); - using var client = _httpClientFactory.CreateClient(); + using var client = _httpClientFactory.CreateClient("Default"); client.DefaultRequestHeaders.Add("authorization", $"{token}"); client.DefaultRequestHeaders.Add("accept-language", "zh-CN,zh;q=0.9"); client.DefaultRequestHeaders.Add("client-app-id", "megcity-web"); @@ -126,13 +128,25 @@ public class SpiderServices var endTs = (long)(DateTime.Now - startTime).TotalMilliseconds; var body = new QueryReq { - State = new int[] { state }, + State = [state], PageNo = 1, PageSize = 50, - SortType = new int[] { 20, 10 }, - CreateStartTime = (long)beginTs, - CreateEndTime = (long)endTs }; + switch (state) + { + case 1: + body.SortType = [20, 10]; + body.CreateStartTime = beginTs; + body.CreateEndTime = endTs; + break; + case 3: + body.SortType = [20, 10]; + body.HandleStartTime = beginTs; + body.HandleEndTime = endTs; + break; + } + + // {"state":[3],"pageNo":1,"pageSize":50,"sortType":[40,10],"handleStartTime":1762099200000,"handleEndTime":1762340979629} // {"state":[3],"pageNo":1,"pageSize":50,"sortType":[40,10],"handleStartTime":1697293650750,"handleEndTime":1699885650750} // {"state":[1],"pageNo":1,"pageSize":50,"sortType":[20,10],"createStartTime":1697290618034,"createEndTime":1699882618034} // {\"state\":[1],\"pageNo\":1,\"pageSize\":50,\"sortType\":[20,10],\"createStartTime\":1697204639551,\"createEndTime\":1697204639551} @@ -140,8 +154,13 @@ public class SpiderServices var getClientOptions = await scope.ServiceProvider.GetService()? .GetClientOptions(); var taskPath = getClientOptions.GetTaskUrl; + var response = await client.PostAsync(taskPath, - new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json")); + new StringContent(JsonSerializer.Serialize(body, new JsonSerializerOptions() + { + DefaultIgnoreCondition + = JsonIgnoreCondition.WhenWritingNull + }), Encoding.UTF8, "application/json")); var respJsonStr = await response.Content.ReadAsStringAsync(); _logger.LogInformation("获得列表返回json: {respJsonStr}", respJsonStr); var spiderRes = JsonSerializer.Deserialize(respJsonStr); @@ -322,7 +341,7 @@ public class SpiderServices .SendText( $"案件{caseNumber}已经结案,所在位置{address},处理意见{(string.IsNullOrWhiteSpace(suggestion) ? "无" : suggestion)}", false); - + return new SpiderResponse() { IsSuccess = true,