15

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);
}
PalaBeaveR
  • 183
  • 1
  • 1
  • 7
  • 2
    Please show us your code. We can't help you without it. –  Jan 21 '19 at 20:46
  • Ok. So it told you to look at the Inner Exception. What is it? Also, as Amy stated, you need to supply code when asking a question about non-working code. Please [Edit] your question and include only the relevant code, and the Inner Exception. – Lynn Crumbling Jan 21 '19 at 20:49
  • 1
    Well the inner exception is in Russian and translates to "Unexpected message received or incorrect format" –  Jan 21 '19 at 21:08
  • Possible duplicate of https://stackoverflow.com/questions/31629206/get-request-to-a-url-throws-exception-using-webclient-but-works-fine-if-open-it –  Jan 21 '19 at 21:08
  • Sadly i still encounter the same error. I edited my code to match the https://stackoverflow.com/questions/31629206/get-request-to-a-url-throws-exception-using-webclient-but-works-fine-if-open-it. – PalaBeaveR Jan 21 '19 at 21:17
  • Well if you follow the answer, you'll realize its telling you how to read the content of the stream so you can know what the cause of the error is. The answer *doesn't fix* the error. –  Jan 21 '19 at 21:21
  • 1
    Are you using .NET Core or .NET Framework? This looks like the SSL connection cannot be established, and thus might be a cipher issue. Ensure your HttpClient supports an appropriate version of SSL. Also, off topic, but typically you wouldn't read a JPEG (or any binary data) as a string. – Matthew Jan 21 '19 at 21:27
  • The error states: Object reference not set to an instance of an object. – PalaBeaveR Jan 21 '19 at 21:28
  • .net Core if i remember correctly. As for "why": i'm making a discord bot and want to integrate osu! statistics via html image, but ImageGenerator cant view this url. – PalaBeaveR Jan 21 '19 at 21:29
  • How do you know this URL is valid and is meant to be accessed directly like this? – John Wu Jan 21 '19 at 22:53
  • It's the only url that contains the avatar. Even the official osu! site refers to this link. Also, if you just use this link in html it works, but i use a lib that converts html to image and sadly this lib can't get the image in the url, so i'm trying to download the avatar and use it localy. – PalaBeaveR Jan 21 '19 at 23:08

5 Answers5

6

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

  • 4
    careful, this sort of code leaks into production before you know it – BozoJoe Mar 09 '21 at 05:06
  • This solution works with HttpWebRequest, HttpWebResponse and WebClient, not only with HttpClient. Just leave the return true and remove everything else. Kudos! – Tanner Ornelas Sep 18 '21 at 02:16
3

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.*

SeveneduS
  • 41
  • 1
  • 5
2

I believe we may have solved this exact issue (or one very similar)

In our case we got the same exception, but strangely from only a single machine.

After some investigation, we used this software (https://www.nartac.com/Products/IISCrypto/Download) to determine that there were no shared cipher suites between our box and the one we were attempting to contact.

We used the software to enable a supported suite on our box and rebooted.

Once back up and running, everything worked fine.

Ultimately this was a machine configuration issue and not a coding one.

[Note: I have no affiliation with this software or it's company, and I'm sure other methods could be used to achieve this, but I know this worked one for us]

Rory Becker
  • 15,551
  • 16
  • 69
  • 94
0

I faced similar Issue, now can able to populate schema and rebuild index. Note: Make sure IIS is Stopped and all Solr services in services stopped. <services.msc>

steps 1: Did switch to Linux container in docker and switched back to previous. step 2: Executed down.ps1 and below list in Windows Powershell in Admin Mode

Stop-Service docker
Stop-service hns
Start-service hns
Start-Service docker
docker network prune

step 3: then Execute docker/clean.ps1 step 4: Execute \init.ps1 step 5: Execute docker-compose up -d and up.ps1.

Not sure the flow consideration on actual steps but it started populating and building index for my setup.

-1

Answer:


After some time i figured that the library i was using was a bit bad. And after that i discovered Selenium.

With that in mind i started using Selenium WebDriver and figured that i can take a screenshot of a page i needed, then i cropped that image and i got what i needed.
So theres no need to continue this issue.

Community
  • 1
  • 1
PalaBeaveR
  • 183
  • 1
  • 1
  • 7