有什么方法可以执行此处描述的操作:https://stackoverflow.com/a/2675183,但在Xamarin.Forms PCL App中?我正在使用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);
    }
}

08-26 22:10
查看更多