作者:贝贝2602932923 | 来源:互联网 | 2022-12-27 11:44
最近我将我的代码库从.net core 1.0迁移到了2.0.之后,我收到错误"服务器在随机使用System.Net.Http.HttpClient时返回了无效或无法识别的响应错误".我在100个请求中的2个中收到此错误.
另外如何调试这个,因为它是随机发生的:(
Program.cs中
public class Program
{
public static void Main(string[] args)
{
var cOnfig= new ConfigurationBuilder()
.AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(),"hosting.json"), optional: false)
.Build();
var useHttps = false;
bool.TryParse(config !=null ? config["useHttps"] : "false", out useHttps);
IWebHost host = null;
if (useHttps)
{
var fileInfo = new FileInfo(config["certName"]);
X509Certificate2 cert = new X509Certificate2(fileInfo.FullName, config["certPwd"]);
host = new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup()
.UseKestrel(optiOns=>
{
options.Listen(IPAddress.Loopback, 5000);
options.Listen(IPAddress.Any, 4430, listenOptiOns=>
{
listenOptions.UseHttps(cert);
});
})
.UseIISIntegration()
.Build();
}
else
{
host = new WebHostBuilder()
.UseStartup()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(config["url"])
.Build();
}
host.Run();
}
}
HTTP网络文件
public class NetworkExtensions
{
public static IConfiguration Configuration { get; set; }
public static ILogger Log { get; set; }
///
/// Flurl to get api
///
///
///
/// This param is used to Perform OKTA api Retry
///
public static async Task Get(OktaRequestDto requestDto, OKTARateLimit rateLimit = null)
{
using (MiniProfiler.Current.Step("Okta : Get"))
{
// OKTA code execution starts here.
string url = requestDto.BaseUrl
.AppendPathSegment(requestDto.ApiName + requestDto.Query);
// Handle all querystring append.
if (requestDto.QueryStrings != null && requestDto.QueryStrings.Count > 0)
{
foreach (var querystring in requestDto.QueryStrings.Keys)
{
//FLURL is encoding the value, To ensure the value is passed correct, added true param to stop default behavior
url = url.SetQueryParam(querystring, requestDto.QueryStrings[querystring], true);
}
}
var respOnse= await url.WithHeader("Authorization", requestDto.ApiKey).GetJsonAsync();
Log.Information("respOnse=> " + JsonConvert.SerializeObject(response));
return response;
catch (FlurlHttpException ex)
{
// there is an OKTA exception, return the default value, The exception is captured in the FLURLConfig
OneLoginException ole = new OneLoginException(ex, requestDto);
throw ole;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
收到错误:
{"ClassName":"Flurl.Http.FlurlHttpException","Message":"GET https://xxx-yyy.oktapreview.com/api/v1/users/00ue1x6pgimMy2Zuf0h7失败.发送请求时出错.", "Data":{},"InnerException":{"ClassName":"System.Net.Http.HttpRequestException","Message":"发送请求时发生错误." ,"Data":{},"InnerException":{"ClassName":"System.Net.Http.WinHttpException","Message":"服务器返回无效或无法识别的响应","数据":{}," InnerException":null,"HelpURL":null,"StackTraceString":"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \n在System.Threading.Tasks.RendezvousAwaitable 1.GetResult()\r\n at System.Net.Http.WinHttpHandler.d__105.MoveNext()","RemoteStackTraceString":null,"RemoteStackIndex":0,"ExceptionMethod":null,"HResult":-2147012744,"Source":"System.Private.CoreLib","WatsonBuckets":null,"NativeErrorCode":12152},"HelpURL":null,"StackTraceString":" at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.ConfiguredTaskAwaitable
1.ConfiguredTaskAwaiter.GetResult()\ r \在System.Net.Http.DiagnosticsHandler.d__2.MoveNext()\ r \n ---从抛出异常的上一个位置的堆栈跟踪结束---\r \n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()\ r \n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \n在System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\ r \n在System.Net.Http上.HttpClient.d__58.MoveNext()\ r \n ---从抛出异常的上一个位置开始的堆栈跟踪结束---\r \n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \n at System.Runtime.Co mpilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \n在Flurl.Http.FlurlRequest.d__19.MoveNext()","RemoteStackTraceString":null,"RemoteStackIndex":0,"ExceptionMethod":null,"HResult": - 2147012744, "源": "System.Private.CoreLib", "WatsonBuckets":空}, "HELPURL":空}