我在Azure应用服务上部署了两个ASP.NET Core 7应用程序。对于某些请求,service1
需要在私有vnet上向service2
发出POST
请求。这在我们的暂存环境中工作正常。然而,在我们的生产环境中,由于某种原因,POST
被转换为GET
,导致请求失败。
.NET Core将传出的POST
请求转换为GET
的原因是什么?
上下文-向Refit添加额外的日志记录:
services
.AddRefitClient<IService2Api>(
(sp) =>
new RefitSettings
{
CollectionFormat = CollectionFormat.Multi,
HttpMessageHandlerFactory = () =>
new LoggingHandler(
sp.GetRequiredService<ILoggerFactory>()
.CreateLogger<LoggingHandler>(),
new HttpClientHandler() { AllowAutoRedirect = true }
)
}
)
.ConfigureHttpClient(
// ...
字符串LoggingHandler
:
private class LoggingHandler : DelegatingHandler
{
private readonly ILogger<LoggingHandler> logger;
public LoggingHandler(ILogger<LoggingHandler> logger, HttpMessageHandler innerHandler)
: base(innerHandler)
{
this.logger = logger;
}
protected override HttpResponseMessage Send(
HttpRequestMessage request,
CancellationToken cancellationToken
)
{
logger.LogDebug("Before [{Method}] to {Url}", request.Method, request.RequestUri);
var response = base.Send(request, cancellationToken);
logger.LogDebug("After [{Method}] to {Url}", request.Method, request.RequestUri);
return response;
}
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken
)
{
logger.LogDebug("Before [{Method}] to {Url}", request.Method, request.RequestUri);
var response = await base.SendAsync(request, cancellationToken);
logger.LogDebug("After [{Method}] to {Url}", request.Method, request.RequestUri);
return response;
}
}
型
:
2023-11-24 23:34:11.315 +00:00 [Information] System.Net.Http.HttpClient.Refit.Implementation.Generated+IService2Api, Common, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null.ClientHandler: Sending HTTP request POST http://service2.azurewebsites.net/my/endpoint/
2023-11-24 23:34:11.315 +00:00 [Trace] System.Net.Http.HttpClient.Refit.Implementation.Generated+IService2Api, Common, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null.ClientHandler: Request Headers:
Authorization: Bearer [Redacted]
User-Agent: Service1, (Service1/1.0.1.0)
Content-Type: application/json; charset=utf-8
2023-11-24 23:34:11.315 +00:00 [Debug] LoggingHandler: Before [POST] to http://service2.azurewebsites.net/my/endpoint/
2023-11-24 23:34:11.400 +00:00 [Debug] LoggingHandler: After [GET] to https://service2.azurewebsites.net/my/endpoint/
2023-11-24 23:34:11.401 +00:00 [Information] System.Net.Http.HttpClient.Refit.Implementation.Generated+IService2Api, Common, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null.ClientHandler: Received HTTP response headers after 85.2956ms - 405
2023-11-24 23:34:11.401 +00:00 [Trace] System.Net.Http.HttpClient.Refit.Implementation.Generated+IService2Api, Common, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null.ClientHandler: Response Headers:
Date: Fri, 24 Nov 2023 23:34:11 GMT
Set-Cookie: [Redacted]
Content-Length: 0
Allow: POST
型
另请参阅:
1条答案
按热度按时间z18hc3ub1#
在
ConfigureHttpClient
中,尝试通过在HttpClientHandler
中设置AllowAutoRedirect = false
来禁用重定向(例如Refit):字符串
然后记录响应状态和标头。
我认为你在某种程度上混合了301/302重定向,http客户端的默认行为是自动遵循它。
也许你已经在一个环境中将基本URL配置为以
http://
开头,而服务器配置为重定向到https://
?