有没有办法像这里描述的那样:https://stackoverflow.com/a/2675183但是在Xamarin.Forms PCL App中?我正在使用HttpClient连接到服务器。
答案 0 :(得分:34)
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);
}
}
答案 1 :(得分:2)
如果您使用的是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);
答案 2 :(得分:2)
使用 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;
}
}
答案 3 :(得分:1)
我遇到了这个问题,但错误消息略有不同:
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());
}
这是Android的修复程序,将相同的代码放入iOS的FinishedLaunching (AppDelegate.cs)
中也应起作用。
答案 4 :(得分:0)
应该像:
followingId: { type: mongoose.Types.ObjectId, index: true }
现在更改var httpClient = new HttpClient();
至:var httpClient = new HttpClient(new System.Net.Http.HttpClientHandler());
别忘了调用IP地址而不是使用localhost
答案 5 :(得分:0)
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();