相关疑难解决方法(0)

Powershell v3 Invoke-WebRequest HTTPS错误

使用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
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用GET方法时:

 Invoke-WebRequest -Uri https://IPADDRESS/resource -Credential $cred -certificate $cert -Method GET
Run Code Online (Sandbox Code Playgroud)

返回以下错误

 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
Run Code Online (Sandbox Code Playgroud)

我曾尝试使用以下代码来忽略SSL证书,但我不确定它是否真的在做任何事情.

 [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
Run Code Online (Sandbox Code Playgroud)

有人可以提供一些指导,说明这里可能出现的问题以及解决方法吗?

谢谢

.net rest powershell https

113
推荐指数
7
解决办法
18万
查看次数

运行简单的未经授权的Rest查询时发生意外错误

我有一个不执行身份验证检查的休息端点.我可以从Linux运行一个简单的curl命令:

curl -k https://application/api/about
Run Code Online (Sandbox Code Playgroud)

这回应了.

但是,如果在PowerShell上尝试以下操作,则会失败:

Invoke-RestMethod https://application/api/about
Run Code Online (Sandbox Code Playgroud)

然后我得到:

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
At line:1 char:1
+ Invoke-RestMethod $Application
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我如何解决这个问题?

编辑:

尝试使用Invoke-WebRequest:

Invoke-WebRequest -Uri "https://application/api/about"
Run Code Online (Sandbox Code Playgroud)

Invoke-WebRequest:基础连接已关闭:发送时发生意外错误.在行:1 char:1 + Invoke-WebRequest -Uri" https:// application/api/a ... + ~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidOperation :(System.Net.HttpWebRequest:HttpWebRequest)[Invoke-WebRequest],WebException + FullyQualifiedErrorId:WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

rest powershell

33
推荐指数
2
解决办法
2万
查看次数

标签 统计

powershell ×2

rest ×2

.net ×1

https ×1