最近我将我的代码库从.net core 1.0迁移到了2.0。之后我收到错误"当随机使用System.Net.Http.HttpClient" 时,服务器返回了无效或无法识别的响应错误。我在100个请求中的2个中收到此错误。
ALso如何调试它,因为它是随机发生的:(
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<Startup>()
.UseKestrel(options =>
{
options.Listen(IPAddress.Loopback, 5000);
options.Listen(IPAddress.Any, 4430, listenOptions =>
{
listenOptions.UseHttps(cert);
});
})
.UseIISIntegration()
.Build();
}
else
{
host = new WebHostBuilder()
.UseStartup<Startup>()
.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; }
/// <summary>
/// Flurl to get api
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="requestDto"></param>
/// <param name="rateLimit">This param is used to Perform OKTA api Retry </param>
/// <returns></returns>
public static async Task<T> Get<T>(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<T>();
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;
}
}
}
}
收到错误:
{&#34; ClassName&#34;:&#34; Flurl.Http.FlurlHttpException&#34;,&#34; Message&#34;:&#34; GET https://xxx-yyy.oktapreview.com/api/v1/users/00ue1x6pgimMy2Zuf0h7失败。发送请求时发生错误。&#34;,&#34;数据&#34;:{},&#34; InnerException&#34;:{&#34; ClassName&#34;:&#34;系统。 Net.Http.HttpRequestException&#34;,&#34; Message&#34;:&#34;发送请求时出错。&#34; ,&#34;数据&#34; :{},&#34;的InnerException&#34; {&#34;的ClassName&#34;:&#34; System.Net.Http.WinHttpException&#34;,的&#34;消息&#34 ;: &#34;服务器返回了无效或无法识别的响应&#34; ,&#34;数据&#34;:{},&#34; InnerException&#34;:null,&#34; HelpURL&#34 ;:空,&#34; StackTraceString&#34;:&#34; at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n在System.Threading.Tasks.RendezvousAwaitable 1.GetResult()\r\n at System.Net.Http.WinHttpHandler.<StartRequest>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 \ n在System.Net.Http.DiagnosticsHandler.d__2 .MoveNext()\ r \ n ---在抛出异常的前一个位置的堆栈跟踪结束---在System.Runtime的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n中的\ r \ n。 System.Net.Http.HttpClient.d__58.MoveNext()\ r \的System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\ r \ n的CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n n ---抛出异常的前一个位置的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n中的\ r \ n任务)\ r \ n在Flurl.Http.FlurlRequest.d__19.MoveNext()&#34;,&#34; RemoteStackTraceString&#34;:null,&#34; RemoteStackIndex&# 34;:0,&#34; ExceptionMethod&#34;:空,&#34; HResult的&#34;: - 2147012744&#34;来源&#34;:&#34; System.Private.CoreLib&#34 ;, &#34; WatsonBuckets&#34;:空}&#34; HELPURL&#34;:空}
答案 0 :(得分:0)
我遇到了同样的问题,并且仍然存在,因此解决该问题的解决方案是:将客户端请求块包装在一次尝试中,并在发生错误的情况下多次进行处理,根据: https://docs.microsoft.com/en-us/azure/architecture/patterns/retry