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.
6 Answers
ServicePointManager
isn't defined in PCL but defined in platform specific classes.
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.
Example:
// 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,687
- 10
- 17
-
2This isn't working for me. The callback never gets called and an exception is generated that says the SSL is invalid – DaveEP Aug 29 '18 at 14:11
-
2AndroidClientHandler doesn't seem to support ServerCertificateValidationCallback. You got any updated information to handle this problem on Android? – Klatschen Sep 21 '18 at 12:16
-
18**THIS SOLUTION DOES NOT WORK FOR ANDROID** – Abdurakhmon Apr 22 '19 at 10:05
With a unique code on Xamarin.Forms way, instantiate a HttpClientHandler Example:
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;
}
}

- 19
- 1
- 6

- 360
- 3
- 6
-
2Of all the solutions here, this one works in VS2019 for Android after the latest 2019/20 updates. – TheLegendaryCopyCoder Jan 07 '20 at 18:48
-
-
1This worked for me with VS2022 for Android with latest updates. I am so thankful. I've been working on this forever and I only used this part: ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { //bypass return true; }, – Karlomanio Aug 18 '22 at 22:13
If you are using AndroidClientHandler
, you need to supply a SSLSocketFactory
and a custom implementation of HostnameVerifier
with all checks disabled. To do this, you’ll need to subclass AndroidClientHandler
and override the appropriate methods.
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();
}
}
And then
var handler = new BypassSslValidationClientHandler();
var httpClient = new System.Net.Http.HttpClient(handler);

- 7,035
- 1
- 26
- 45
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();

- 1,232
- 1
- 12
- 37

- 31
- 1
I faced this but with a slightly different error message:
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
I fixed it with the following in 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());
}
This was the fix for Android, putting the same code in FinishedLaunching (AppDelegate.cs)
for iOS should also work.

- 3,527
- 2
- 29
- 55
- Put this line of code inside OnCreate in MainActivity.cs: System.Net.ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
it should be like :
protected override void OnCreate(Bundle savedInstanceState)
{
// to bypass ssl
System.Net.ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
...
..
LoadApplication(new App());
}
Now change var httpClient = new HttpClient();
to: var httpClient = new HttpClient(new System.Net.Http.HttpClientHandler());
And don't forget to call IP address instead of using localhost

- 109
- 8