Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Solution for Ssl error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
namespace MyXamarinNativeApp.Shared.Helpers
{
public class HttpClientHelper
{
public static HttpClient Create(CookieContainer cookieContainer = null)
{
HttpClient http;
#if __ANDROID__
var handler = new Xamarin.Android.Net.AndroidClientHandler();
if (cookieContainer != null)
handler.CookieContainer = cookieContainer;
http = new HttpClient(handler);
#else
var handler = new HttpClientHandler();
if (cookieContainer != null)
handler.CookieContainer = cookieContainer;
http = new HttpClient(handler);
#endif
return http;
}
public static HttpClientHandler CreateHandle()
{
HttpClientHandler handler;
#if __ANDROID__
handler = new Xamarin.Android.Net.AndroidClientHandler();
#else
handler = new HttpClientHandler();
#endif
return handler;
}
}
}
@MarcAlx
Copy link

MarcAlx commented Oct 8, 2021

Here's a small refactor for your proposal :

public static HttpClient Create(CookieContainer cookieContainer = null)
{
    return new HttpClient(HttpClientHelper.CreateHandle(cookieContainer));
}

public static HttpClientHandler CreateHandle(CookieContainer cookieContainer = null)
{
    HttpClientHandler handler;

#if __ANDROID__
    handler = new Xamarin.Android.Net.AndroidClientHandler();
#else
    handler = new HttpClientHandler();
#endif

    if (cookieContainer != null)
        handler.CookieContainer = cookieContainer;
    return handler;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment