处理 http POST 请求时的 ConnectionResetException

ConnectionResetException when handling an http POST request

提问人:ape 提问时间:7/25/2023 更新时间:7/27/2023 访问量:82

问:

我正在尝试将带有正文的 POST 请求从前端客户端发送到 ASP.NET (.NET 7.0) 服务器(下面的代码)。

const URL = API_LINK + `/task-types`

await axios({
    method: 'POST',
    url: URL,
    headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
        Authorization: `Bearer ${this.token}`,
    },
    data: JSON.stringify({
        name: task.name,
        description: task.description,
        deactivated: task.deactivated,
    }),
})
    .then(res => console.log(res))
    .catch(err => console.log(err))
 [HttpPost]
 public async Task<IActionResult> Create([FromBody] CreateTaskTypeCommand command)
 {
     await _mediator.Send(command);

     return Ok();
 }

在后端,它抛出一个 .我不知道如何解决这个问题,我还没有在互联网上找到合适的解决方案。ConnectionResetException

以下是日志中的信息:

2023-07-25 16:21:39.5054|0|INFO|Microsoft.AspNetCore.Hosting.Diagnostics|Request starting HTTP/2 POST https://localhost:44344/api/task-types application/json 70 
2023-07-25 16:21:39.5054|0|TRACE|Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware|All hosts are allowed. 
2023-07-25 16:21:39.5054|0|DEBUG|Microsoft.AspNetCore.Routing.Matching.DfaMatcher|1 candidate(s) found for the request path '/api/task-types' 
2023-07-25 16:21:39.5054|0|DEBUG|Microsoft.AspNetCore.Routing.Matching.DfaMatcher|Endpoint 'TimeboxTA.WebAPI.Controllers.TaskTypeController.Create (TimeboxTA.WebAPI)' with route pattern 'api/task-types' is valid for the request path '/api/task-types' 
2023-07-25 16:21:39.5054|0|DEBUG|Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer|Connection ID "17870283352276205781" disconnecting. 
2023-07-25 16:21:39.5054|0|DEBUG|Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware|Request matched endpoint 'TimeboxTA.WebAPI.Controllers.TaskTypeController.Create (TimeboxTA.WebAPI)' 
2023-07-25 16:21:39.5054|0|DEBUG|Microsoft.AspNetCore.ResponseCaching.ResponseCachingMiddleware|The request cannot be served from cache because it uses the HTTP method: POST. 
2023-07-25 16:21:39.5054|0|DEBUG|Microsoft.AspNetCore.Cors.Infrastructure.CorsService|The request has an origin header: 'https://localhost:44311'. 
2023-07-25 16:21:39.5054|0|INFO|Microsoft.AspNetCore.Cors.Infrastructure.CorsService|CORS policy execution successful. 
2023-07-25 16:21:39.5054|0|DEBUG|Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware|Static files was skipped as the request already matched an endpoint. 
2023-07-25 16:21:53.3856|0|ERROR|TimeboxTA.WebAPI.Middleware.ErrorHandlingMiddleware|Exception message: The client has disconnected Microsoft.AspNetCore.Connections.ConnectionResetException: The client has disconnected
 ---> System.Runtime.InteropServices.COMException (0x800704CD): Próbowano wykonać operację na nieistniejącym połączeniu sieciowym. (0x800704CD)
   --- End of inner exception stack trace ---
   at Microsoft.AspNetCore.Server.IIS.Core.IO.AsyncIOOperation.GetResult(Int16 token)
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.ReadBody()
   at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result)
   at System.IO.Pipelines.Pipe.ReadAsync(CancellationToken token)
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.ReadAsync(Memory`1 memory, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Server.IIS.Core.HttpRequestStream.ReadAsyncInternal(Memory`1 buffer, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
   at System.IO.StreamReader.ReadBufferAsync(CancellationToken cancellationToken)
   at System.IO.StreamReader.ReadToEndAsyncInternal(CancellationToken cancellationToken)
   at TimeboxTA.WebAPI.Middleware.ErrorHandlingMiddleware.ReadRequestBody(HttpRequest request) in C:\Users\lukasz.strus\source\repos\bmroz\TimeboxTA\TimeboxTA.WebAPI\Middleware\ErrorHandlingMiddleware.cs:line 83
   at TimeboxTA.WebAPI.Middleware.ErrorHandlingMiddleware.InvokeAsync(HttpContext context, RequestDelegate next) in C:\Users\lukasz.strus\source\repos\bmroz\TimeboxTA\TimeboxTA.WebAPI\Middleware\ErrorHandlingMiddleware.cs:line 23
2023-07-25 16:22:06.9989|0|ERROR|Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware|An unhandled exception has occurred while executing the request. Microsoft.AspNetCore.Connections.ConnectionResetException: The client has disconnected
 ---> System.Runtime.InteropServices.COMException (0x800704CD): Próbowano wykonać operację na nieistniejącym połączeniu sieciowym. (0x800704CD)
   --- End of inner exception stack trace ---
   at Microsoft.AspNetCore.Server.IIS.Core.IO.AsyncIOOperation.GetResult(Int16 token)
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.ReadBody()
   at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result)
   at System.IO.Pipelines.Pipe.ReadAsync(CancellationToken token)
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.ReadAsync(Memory`1 memory, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Server.IIS.Core.HttpRequestStream.ReadAsyncInternal(Memory`1 buffer, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
   at System.IO.StreamReader.ReadBufferAsync(CancellationToken cancellationToken)
   at System.IO.StreamReader.ReadToEndAsyncInternal(CancellationToken cancellationToken)
   at TimeboxTA.WebAPI.Middleware.ErrorHandlingMiddleware.ReadRequestBody(HttpRequest request) in C:\Users\lukasz.strus\source\repos\bmroz\TimeboxTA\TimeboxTA.WebAPI\Middleware\ErrorHandlingMiddleware.cs:line 83
   at TimeboxTA.WebAPI.Middleware.ErrorHandlingMiddleware.InvokeAsync(HttpContext context, RequestDelegate next) in C:\Users\lukasz.strus\source\repos\bmroz\TimeboxTA\TimeboxTA.WebAPI\Middleware\ErrorHandlingMiddleware.cs:line 45
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass6_1.<<UseMiddlewareInterface>b__1>d.MoveNext()
--- End of stack trace from previous location ---
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.ResponseCaching.ResponseCachingMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
2023-07-25 16:22:07.0112|0|INFO|Microsoft.AspNetCore.Hosting.Diagnostics|Request finished HTTP/2 POST https://localhost:44344/api/task-types application/json 70 - 500 - text/plain;+charset=utf-8 27503.0152ms 

我试图忽略这个异常,但它没有帮助。

asp.net 异常 发布 httprequest

评论


答:

0赞 ape 7/27/2023 #1

我发现了问题。在html文件中,按钮具有“submit”类型(无意识复制和粘贴代码片段的错误)。结果,它无法向服务器发送查询,如果它以某种方式设法读取背面和侧的正文,它就会失去连接。