Skip to content

Instantly share code, notes, and snippets.

@alexeygritsenko
Created October 5, 2021 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexeygritsenko/5efd1e1e2877c703b73888039d282210 to your computer and use it in GitHub Desktop.
Save alexeygritsenko/5efd1e1e2877c703b73888039d282210 to your computer and use it in GitHub Desktop.
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