问题
The error below occurs intermittently when _httpClientFactory.CreateClient() is used to send request to external resources.
System.Net.Http.HttpRequestException : An error occurred while sending the request. ---> System.IO.IOException : The server returned an invalid or unrecognized response. at async System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request,CancellationToken cancellationToken) End of inner exception at async System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request,CancellationToken cancellationToken) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection,HttpRequestMessage request,Boolean doRequestAuth,CancellationToken cancellationToken) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request,Boolean doRequestAuth,CancellationToken cancellationToken)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask,HttpRequestMessage request,CancellationTokenSource cts,Boolean disposeCts) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async IdentityModel.Client.HttpClientTokenRequestExtensions.RequestTokenAsync(??)
public class WebHttpClient
{
private readonly IHttpClientFactory _httpClientFactory;
public WebHttpClient(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
public async Task<string> WebRequestAsync(string url)
{
return await _httpClientFactory.CreateClient().WebRequestAsync(url); //intermittent error: An error occurred while sending the request.
}
}
[assembly: WebJobsStartup(typeof(Startup))]
public sealed class Startup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder webJobsBuilder)
{
webJobsBuilder.Services.AddHttpClient();
}
}
Any solution?
Update
static HttpClient has issues with DNS. This is why I try to avoid static if possible and if using IHttpClientFactory can resolve all issues.
But I am not sure if my issue is caused by DNS or something else.
Would a typed httpclient (or wrapper around httpclient) fix this issue? It is intermittent so it is not easy to reproduce.
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.2#typed-clients
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.2#httpclient-and-lifetime-management
Update 2
Please see the updated exception at the top.
来源:https://stackoverflow.com/questions/55846379/ihttpclientfactory-intermittent-error-an-error-occurred-while-sending-the-reque