使用 Powershell v3的 Invoke-WebRequest 和 Invoke-RestMethod,我已经成功地使用 POST 方法将 json 文件发布到 https 网站。
我使用的命令是
$cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("cert.crt")
Invoke-WebRequest -Uri https://IPADDRESS/resource -Credential $cred -certificate $cert -Body $json -ContentType application/json -Method POST
然而,当我尝试使用 GET 方法时,比如:
Invoke-WebRequest -Uri https://IPADDRESS/resource -Credential $cred -certificate $cert -Method GET
返回以下错误
Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
At line:8 char:11
+ $output = Invoke-RestMethod -Uri https://IPADDRESS/resource -Credential $cred
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
我尝试使用下面的代码来忽略 SSL cert,但是我不确定它是否实际上在做任何事情。
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
有人能提供一些指导,什么可能是错误的,以及如何修复它?
谢谢