[英]SSL handshake fails in Xamarin
我正在嘗試訪問我的RESTful API以從MySQL數據庫中檢索數據。 在我的C#WPF項目中,一切都已設置好並且完美運行。 但是當在Xamarin Forms(為Android構建)中使用完全相同的代碼時,我無法與我的服務器成功進行SSL握手。
我正在使用Flurl.Http(使用HttpClient)建立連接,但在jsonReader.Wait()上獲得異常:
var jsonReader = "https://example.com/my_api/api.php/records/my_table?order=id,desc&size=10"
.WithHeader("Accept", "application/json")
.WithBasicAuth("username", "password")
.GetJsonAsync<JsonRootObject>();
// Wait for completion.
jsonReader.Wait();
這是我的AggregateException:
System.AggregateException: One or more errors occurred. (Call failed. An error occurred while sending the request
GET https://example.com/my_api/api.php/records/my_table?order=id,desc&size=10) ---> Flurl.Http.FlurlHttpException: Call failed. An error occurred while sending the request
GET https://example.com/my_api/api.php/records/my_table?order=id,desc&size=10 ---> System.Net.Http.HttpRequestException: An error occurred while sending the request
---> System.Net.WebException: Error: TrustFailure (Authentication failed, see inner exception.) ---> System.Security.Authentication.AuthenticationException:
Authentication failed, see inner exception. ---> Mono.Btls.MonoBtlsException: Ssl error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED
at /Users/builder/jenkins/workspace/xamarin-android-d16-1/xamarin-android/external/mono/external/boringssl/ssl/handshake_client.c:1132
at Mono.Btls.MonoBtlsContext.ProcessHandshake () [0x00038] in <74989b9daab94528ac2c4b7da235b9c5>:0
at Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake (Mono.Net.Security.AsyncOperationStatus status, System.Boolean renegotiate) [0x000a1] in <74989b9daab94528ac2c4b7da235b9c5>:0
at (wrapper remoting-invoke-with-check) Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake(Mono.Net.Security.AsyncOperationStatus,bool)
at Mono.Net.Security.AsyncHandshakeRequest.Run (Mono.Net.Security.AsyncOperationStatus status) [0x00006] in <74989b9daab94528ac2c4b7da235b9c5>:0
at Mono.Net.Security.AsyncProtocolRequest.ProcessOperation (System.Threading.CancellationToken cancellationToken) [0x000ff] in <74989b9daab94528ac2c4b7da235b9c5>:0
at Mono.Net.Security.AsyncProtocolRequest.StartOperation (System.Threading.CancellationToken cancellationToken) [0x0008b] in <74989b9daab94528ac2c4b7da235b9c5>:0
為什么我的SSL握手失敗了? 什么是Xamarin與WPF不同的做法?
你的Flurl.Http會因為使用2種不同的項目類型而失敗(.net標准vs類庫 - 我和我寫的http客戶端有完全相同的問題)
為.net標准嘗試不同的http客戶端。
在任何情況下,你可以編寫自己的http客戶端和調試 - 這是我的建議。
如果你仍然堅持 - 寫回來我會幫助。
原來是Apache配置錯誤 。 我檢查了我的域名 ,發現錯誤。 通過在我的Apache Directives中添加證書的鏈文件來解決這個問題:
SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
最后一行是失蹤者。 奇怪的是,即使沒有鏈文件,瀏覽器和WPF也會信任證書。 無論如何,現在它也適用於Android。
與此同時,我一直在使用這段代碼來禁用我的應用程序中的SSL驗證:
// This disables the SSL certificate validity check.
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
該代碼不安全 ,只能用於測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.