有没有办法在 Xamarin.Forms PCL 应用程序中实现类似此处描述的操作:https://dev59.com/EnE85IYBdhLWcg3wpFSu#2675183?我正在使用 HttpClient 连接到服务器。
有没有办法在 Xamarin.Forms PCL 应用程序中实现类似此处描述的操作:https://dev59.com/EnE85IYBdhLWcg3wpFSu#2675183?我正在使用 HttpClient 连接到服务器。
ServicePointManager
在PCL中未定义,但在特定于平台的类中定义。
Xamarin.iOS
和Xamarin.Android
均具有相同用法的ServicePointManager
。您可以在平台项目中的任何类中引用它。然而,目前不存在这样的类,并且似乎没有办法为Windows Phone应用程序进行引用。
示例:
// Xamarin.Android
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
// You may use ServicePointManager here
ServicePointManager
.ServerCertificateValidationCallback +=
(sender, cert, chain, sslPolicyErrors) => true;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
// Xamarin.iOS
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
ServicePointManager
.ServerCertificateValidationCallback +=
(sender, cert, chain, sslPolicyErrors) => true;
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
}
使用 Xamarin.Forms 的独特代码,实例化一个 HttpClientHandler。 示例:
private HttpClient _httpClient;
public HttpClient HttplicentAccount
{
get
{
_httpClient = _httpClient ?? new HttpClient
(
new HttpClientHandler()
{
ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) =>
{
//bypass
return true;
},
}
, false
)
{
BaseAddress = new Uri("YOUR_API_BASE_ADDRESS"),
};
// In case you need to send an auth token...
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", "YOUR_TOKEN");
return _httpClient;
}
}
如果您正在使用AndroidClientHandler
,则需要提供一个SSLSocketFactory
和一个自定义实现的HostnameVerifier
,并关闭所有检查。为此,您需要对AndroidClientHandler
进行子类化,并覆盖相应的方法。
internal class BypassHostnameVerifier : Java.Lang.Object, IHostnameVerifier
{
public bool Verify(string hostname, ISSLSession session)
{
return true;
}
}
internal class BypassSslValidationClientHandler : AndroidClientHandler
{
protected override SSLSocketFactory ConfigureCustomSSLSocketFactory(HttpsURLConnection connection)
{
return SSLCertificateSocketFactory.GetInsecure(1000, null);
}
protected override IHostnameVerifier GetSSLHostnameVerifier(HttpsURLConnection connection)
{
return new BypassHostnameVerifier();
}
}
然后
var handler = new BypassSslValidationClientHandler();
var httpClient = new System.Net.Http.HttpClient(handler);
public static HttpClient PreparedClient() {
HttpClientHandler handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback+= (sender, cert, chain,sslPolicyErrors) => { return true; };
HttpClient client = new HttpClient(handler); return client;
}
HttpClient client = PreparedClient();
。
Ssl error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED
at /Users/builder/jenkins/workspace/archive-mono/2019-02/android/release/external/boringssl/ssl/handshake_client.c:1132
OnCreate (MainActivity.cs)
中解决了这个问题:protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
// HERE
#if DEBUG
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
#endif
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
Forms.Init(this, savedInstanceState);
// Initialize Material Design renderer
FormsMaterial.Init(this, savedInstanceState);
LoadApplication(new App());
}
FinishedLaunching (AppDelegate.cs)
中也应该有效。protected override void OnCreate(Bundle savedInstanceState)
{
// to bypass ssl
System.Net.ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
...
..
LoadApplication(new App());
}
现在将 var httpClient = new HttpClient();
改为:var httpClient = new HttpClient(new System.Net.Http.HttpClientHandler());
还有别忘了使用 IP 地址而不是 localhost。