Xamarin Forms:无法建立 SSL 连接
Xamarin Forms: The SSL connection could not be established
我在 Xamarin Forms 上遇到 SSL 连接问题。它最初工作正常,但现在它给出了例外。同样的 API 在另一个项目中工作正常。
var siteIdResponse = await client.GetAsync(API CALL);
我的Xamarin.Forms版本是4.5.0282.
我的项目类型是便携类型。
System.Net.Http.HttpRequestException: The SSL connection could not be established
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/archive-mono/2020-02/android/release/external/boringssl/ssl/handshake_client.c:1132
为了参考,我创建了一个示例项目并上传了它here
我做了一个 dependencyService 来获取 HttpClientHandler。
首先。创建接口。
public interface IMyOwnNetService
{
HttpClientHandler GetHttpClientHandler();
}
然后在android中实现这个接口。
[assembly: Dependency(typeof(MyOwnNetService))]
namespace Business_App.Droid
{
internal class MyOwnNetService : IMyOwnNetService
{
public HttpClientHandler GetHttpClientHandler()
{
return new Xamarin.Android.Net.AndroidClientHandler();
}
}
}
iOS
[assembly: Dependency(typeof(MyOwnNetService))]
public class MyOwnNetService: IMyOwnNetService {
public HttpClientHandler GetHttpClientHandler() {
return new HttpClientHandler();
}
}
最后,像下面的代码一样使用HttpClient。
var hc = new HttpClient(Xamarin.Forms.DependencyService.Get<IMyOwnNetService>().GetHttpClientHandler());
var siteIdResponse = await hc.GetAsync(API CALL);
我在 Xamarin Forms 上遇到 SSL 连接问题。它最初工作正常,但现在它给出了例外。同样的 API 在另一个项目中工作正常。
var siteIdResponse = await client.GetAsync(API CALL);
我的Xamarin.Forms版本是4.5.0282.
我的项目类型是便携类型。
System.Net.Http.HttpRequestException: The SSL connection could not be established 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/archive-mono/2020-02/android/release/external/boringssl/ssl/handshake_client.c:1132
为了参考,我创建了一个示例项目并上传了它here
我做了一个 dependencyService 来获取 HttpClientHandler。
首先。创建接口。
public interface IMyOwnNetService
{
HttpClientHandler GetHttpClientHandler();
}
然后在android中实现这个接口。
[assembly: Dependency(typeof(MyOwnNetService))]
namespace Business_App.Droid
{
internal class MyOwnNetService : IMyOwnNetService
{
public HttpClientHandler GetHttpClientHandler()
{
return new Xamarin.Android.Net.AndroidClientHandler();
}
}
}
iOS
[assembly: Dependency(typeof(MyOwnNetService))]
public class MyOwnNetService: IMyOwnNetService {
public HttpClientHandler GetHttpClientHandler() {
return new HttpClientHandler();
}
}
最后,像下面的代码一样使用HttpClient。
var hc = new HttpClient(Xamarin.Forms.DependencyService.Get<IMyOwnNetService>().GetHttpClientHandler());
var siteIdResponse = await hc.GetAsync(API CALL);