Error with Invoke-WebRequest

I’m using the code below, but I get a message (because of the catch) with error: “The underlying connection is closed: An unexpected error occurred while shipping.”

When I paste the url from $urlWW into a browser, it works. What is going wrong?

$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

$urlWW = 'https://' + $server + '/page.php?user=' + $username + '&password=' + $password
$uriWW = [System.Uri]$urlWW

Try
{
	Invoke-WebRequest -UseBasicParsing -Uri $uriWW | Out-Null
}
Catch
{
	# Send message with $_.Exception.Message and $urlWW
}

Is that the actual error or a mistranslation? I don’t understand what shipping means in this context?

Were you getting a TLS/SSL error before adding this line? If not, does the script work without it?

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

Are you doing anything else in the browser that might explain why it works? Proxy server? TLS inspection bypass for the URL?

  1. Original error:
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a se
nd.
At line:1 char:13
+             Invoke-WebRequest -UseBasicParsing -Uri $uriWW | Out-Null
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [I 
   nvoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.Inv 
   okeWebRequestCommand

The previous was a translation of an automated translation that was send in the catch.

  1. Without the validation callback:
    It works!!! Thx
    In previous attempts I had only (Tls11,) Tls12 as security protocols. It doesn’t work. When I added Ssl3 and Tls, I added the validation callback at the same time.

  2. Via the browser
    I use the standard settings in the browser. So no proxy and I think no TLS inspection bypass either.