Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke-WebRequest SSL fails?

Tags:

powershell

ssl

When I try to use Invoke-WebRequest I'm getting some weird error:

Invoke-WebRequest -Uri "https://idp.safenames.com/"  Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send. 

I'm not sure what's causing it, as the website itself seems fine.

Even with all the "ignore ssl errors" functions around stackoverflow, it's still not working, making me wonder if it's related to SSL at all.

like image 727
iTayb Avatar asked Mar 28 '16 15:03

iTayb


People also ask

What is the difference between invoke RestMethod and invoke WebRequest?

Invoke-RestMethod is perfect for quick APIs that have no special response information such as Headers or Status Codes, whereas Invoke-WebRequest gives you full access to the Response object and all the details it provides.

Could not establish a trust relationship for the SSL TLS secure channel?

A common reason you may receive the error Could not establish trust relationship for the SSL/TLS secure channel is because the SSL certificate isn't trusted. If the SSL certificate is not trusted, you will need to install the SSL certificate's root certificate.

How do I check TLS version in Windows Powershell?

3 Answers. @CallMeD-9066 I use powershell command Get-TlsCipherSuite on a windows server to list all cipher suites. If the suggested response helped you resolve your issue, please do not forget to accept the response as Answer and "Up-Vote" for the answer that helped you for benefit of the community.


1 Answers

As BaconBits notes, .NET version > 4.5 uses SSLv3 and TLS 1.0 by default.

You can change this behavior by setting the SecurityProtocol policy with the ServicePointManager class:

PS C:\> $AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12' PS C:\> [System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols PS C:\> (Invoke-WebRequest -Uri "https://idp.safenames.com/").StatusCode 200 

This will apply to all requests in the AppDomain (so it only applies to the current instance of the host application).


There's a module on GitHub and in PSGallery that can manage these settings now:

Install-Module BetterTls -Scope CurrentUser Import-Module BetterTls Enable-Tls -Tls11 -Tls12 
like image 137
Mathias R. Jessen Avatar answered Sep 19 '22 15:09

Mathias R. Jessen