1

使用“Flurl library”(在我的开发机器上)调用 https 端点时出现错误,所以我添加了“着名”

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 

跳过证书验证。

这似乎不适用于 Flurl(但适用于其他库,如“RestSharp”)

这是我的代码:

public async Task<object> MyMethod()
        {

            var client = new Url(baseUrl)
                                .AppendPathSegment(_endpoint)
                                .SetQueryParam("mbxId", 7)
                                .WithBasicAuth(username, password)
                                .WithHeader("cache-control", "no-cache")
                                .WithHeader("contenttype", "application/json; charset=utf-8")
                                .WithHeader("Accept", "text/html, application/xhtml+xml, image/jxr, */*");


            return await client.GetAsync();
        }

如果在拨打电话之前我打开“fiddler”并确认有关证书问题的消息警告(在这种情况下有效,如果我不先打开 fiddler,则不要)

ServicePointManager.ServerCertificateValidationCallback 是否具有全局范围?为什么 flurls 不考虑这个配置?

- - 更新 - -

内部异常:

[System.Exception {Flurl.Http.FlurlHttpExceprion}] 

信息:

   {"Security Error"} Request to https://demo.xxxxx.xxxxxxxxxxxxx.com:8181/dmlm4ws/ws/sessions/sessions?mbxId=7 failed. An error occurred while sending the request.

堆栈跟踪

   in Flurl.Http.Configuration.FlurlMessageHandler.<InnerSendAsync>d__2.MoveNext() in C:\projects\flurl\src\Flurl.Http.Shared\Configuration\FlurlMessageHandler.cs:riga 59
--- Fine traccia dello stack da posizione precedente dove è stata generata l'eccezione ---
   in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   in Flurl.Http.Configuration.FlurlMessageHandler.<SendAsync>d__1.MoveNext() in C:\projects\flurl\src\Flurl.Http.Shared\Configuration\FlurlMessageHandler.cs:riga 31
--- Fine traccia dello stack da posizione precedente dove è stata generata l'eccezione ---
   in System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   in Flurl.Http.Configuration.FlurlMessageHandler.<SendAsync>d__1.MoveNext() in C:\projects\flurl\src\Flurl.Http.Shared\Configuration\FlurlMessageHandler.cs:riga 43
--- Fine traccia dello stack da posizione precedente dove è stata generata l'eccezione ---
   in System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   in Flurl.Http.Configuration.FlurlMessageHandler.<SendAsync>d__1.MoveNext() in C:\projects\flurl\src\Flurl.Http.Shared\Configuration\FlurlMessageHandler.cs:riga 47
--- Fine traccia dello stack da posizione precedente dove è stata generata l'eccezione ---
   in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   in System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()
--- Fine traccia dello stack da posizione precedente dove è stata generata l'eccezione ---
   in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   in Flurl.Http.FlurlClient.<SendAsync>d__28.MoveNext() in C:\projects\flurl\src\Flurl.Http.Shared\FlurlClient.cs:riga 202
--- Fine traccia dello stack da posizione precedente dove è stata generata l'eccezione ---
   in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   in System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   in DLMN.DMLM.<PerformRequest>d__8.MoveNext() in C:\PROGETTI\Progetti\DMLM\DLMN\DLMN\DMLM.cs:riga 90
--- Fine traccia dello stack da posizione precedente dove è stata generata l'eccezione ---
   in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   in Flurl.Http.HttpResponseMessageExtensions.<ReceiveJson>d__0`1.MoveNext() in C:\projects\flurl\src\Flurl.Http.Shared\HttpResponseMessageExtensions.cs:riga 26
4

1 回答 1

0

在此处查看此解决方案,该解决方案DefaultHttpClientFactory通过覆盖CreateMessageHandler

public override HttpMessageHandler CreateMessageHandler() {
        return new HttpClientHandler {
            ServerCertificateCustomValidationCallback = (a, b, c, d) => true
        };
    }
于 2019-04-09T10:54:19.830 回答