忽略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屋!