C#HttpClient请求HTTPS异常错误

时间:2019-06-27 15:54:59

标签: c# exception https httpclient

.NET Core 2.2上的MVC Web应用程序出现间歇性错误,该应用程序发送Http请求以从各种API获取信息。

我认为该错误与URI是https而不是http有关,因为在没有https的情况下对其他API的其他调用绝对可以。即使使用客户端处理程序定义了证书和协议设置,我仍然时不时地会遇到此异常错误:

  

发送请求时出错。

     在System.Net.Http.HttpConnection.SendAsyncCore中的

(HttpRequestMessage请求,CancellationToken cancelToken)    在System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection连接,HttpRequestMessage请求,布尔值doRequestAuth,CancellationToken取消令牌)    在System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage请求,布尔值doRequestAuth,CancellationToken cancelledToken)    在System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage请求,CancellationToken cancelToken)    在System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage请求,CancellationToken cancelToken)    在System.Net.Http.HttpClient.FinishSendAsyncBuffered(任务1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) at tf.PriceService.Core.Classes.WilliamHillMatching.GetHorsesFromEvent(String eventid) in C:\agent\_work\9\s\src\tf.PriceService.Core\Classes\WilliamHillMatching.cs:line 316 at tf.PriceService.Core.Classes.WilliamHillMatching.getPrices(List 1 whCompetitions,Race tfRaceData)中位于C:\ agent_work \ 9 \ s \ src \ tf.PriceService.Core \ Classes \ WilliamHillMatching.cs:line 94    在tf.PriceService.Core.Services.PriceService。<> c__DisplayClass50_0。 d.MoveNext()在C:\ agent_work \ 9 \ s \ src \ tf.PriceService.Core \ Services \ PriceService.cs:line 173    ---从之前引发异常的位置开始的堆栈结束跟踪---    在tf.PriceService.Core.Services.PriceService.getBookmakerRacePrices(DateTime workDate,List`1 tfPriceList,Race tfRaceData)中位于C:\ agent_work \ 9 \ s \ src \ tf.PriceService.Core \ Services \ PriceService.cs:line 178    位于tf.PriceService.Core.Services.PriceService.processRacePrices(对象状态),位于C:\ agent_work \ 9 \ s \ src \ tf.PriceService.Core \ Services \ PriceService.cs:line 341

代码如下:

    public static async Task<WilliamHillData.Market> GetHorsesFromEvent(string eventid)
    {
        string res = "";

        using (HttpClientHandler handler = new HttpClientHandler())
        {
            handler.ServerCertificateCustomValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
            handler.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;

            using (HttpClient client = new HttpClient(handler))
            {
                client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
                client.DefaultRequestHeaders.TryAddWithoutValidation("apiKey", "key");

                using (HttpResponseMessage response = await client.GetAsync("https://gw.whapi.com/v2/sportsdata/events/" + eventid + "/?&includeAllDescendants=true"))
                {
                    res = await response.Content.ReadAsStringAsync();
                }
            }
        }

        JObject jobject = JObject.Parse(res);
        List<WilliamHillData.Market> list = jobject["markets"].ToObject<List<WilliamHillData.Market>>().ToList();
        list[0].eventid = eventid;

        return list[0];
    }

似乎指出了这一特定部分:

 using (HttpResponseMessage response = await client.GetAsync("https://gw.whapi.com/v2/sportsdata/events/" + eventid + "/?&includeAllDescendants=true"))
                {
                    res = await response.Content.ReadAsStringAsync();
                }

我是否从Http请求中丢失了某些内容?还有其他方法吗?

0 个答案:

没有答案