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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With