忽略 Xamarin.Forms (PCL) 中的 SSL 证书错误 [英] Ignore SSL certificate errors in Xamarin.Forms (PCL)
问题描述
有什么方法可以做这里描述的事情:https://stackoverflow.com/a/2675183 但在 Xamarin 中.Forms PCL 应用程序?我正在使用 HttpClient 连接到服务器.
Is there any way to do something like described here: https://stackoverflow.com/a/2675183 but in Xamarin.Forms PCL App? I'm using HttpClient to connect to the server.
推荐答案
ServicePointManager
不是在 PCL 中定义的,而是在特定于平台的类中定义的.
ServicePointManager
isn't defined in PCL but defined in platform specific classes.
Xamarin.iOS 和 Xamarin.Android 中都有 ServicePointManager
,用法相同.您可以在平台项目的任何类中引用它.但是,目前没有这样的类,而且对于 Windows Phone 应用似乎没有办法这样做.
There are ServicePointManager
in both Xamarin.iOS and Xamarin.Android with same usage. You can reference it inside any classes in your platform projects. However, currently there is no such class and seems no way to do so for Windows Phone app.
示例:
// 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 (PCL) 中的 SSL 证书错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!