Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "The SSL connection could not be established, see inner exception." when trying to download osu! avatar

Tags:

c#

.net

ssl

avatar

I want to download osu! avatars to use them, but keep getting this error:

The SSL connection could not be established.

Inner exception is:

System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception. ---> System.ComponentModel.Win32Exception: Получено непредвиденное сообщение или оно имеет неправильный формат

Example url: https://a.ppy.sh/10638551?1524507784.png

I tried using HttpClient and WebClient but without any success.

using(HttpClient client = new HttpClient())
{
     var resp = await client.GetAsync("https://a.ppy.sh/10638551?1547998515.jpeg");
     var responseStr = await resp.Content.ReadAsStringAsync();
     File.WriteAllText("html/avatars/avatar.jpeg", responseStr);
}
like image 439
PalaBeaveR Avatar asked Jan 21 '19 20:01

PalaBeaveR


People also ask

What is SSL in Web technology?

SSL Stands for secure sockets layer. Protocol for web browsers and servers that allows for the authentication, encryption and decryption of data sent over the Internet.


2 Answers

I found te solution this blog helped me

ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) =>
{
    // local dev, just approve all certs
    if (development) return true;
    return errors == SslPolicyErrors.None ;
};

https://www.khalidabuhakmeh.com/validate-ssl-certificate-with-servicepointmanager

like image 88
Diego Mauricio Guerrero Avatar answered Sep 29 '22 16:09

Diego Mauricio Guerrero


Before use HttpClient you should setup HttpClientHandler;

var handler = new HttpClientHandler();

handler.ServerCertificateCustomValidationCallback += 
                (sender, certificate, chain, errors) =>
                {
                    return true;
                };

only than your HttpClient code. Must Work for .net Core 3.*

like image 35
SeveneduS Avatar answered Sep 29 '22 17:09

SeveneduS