我有一个不执行身份验证检查的休息端点。我可以从Linux运行一个简单的curl命令:
curl -k https://application/api/about
这回应了。
但是,如果在PowerShell上尝试以下操作,则会失败:
Invoke-RestMethod https://application/api/about
然后我得到:
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
有人可以告诉我如何解决这个问题吗?
修改
尝试使用Invoke-WebRequest:
Invoke-WebRequest -Uri "https://application/api/about"
Invoke-WebRequest:底层连接已关闭:An 发送时发生意外错误。在行:1个字符:1 + Invoke-WebRequest -Uri“https://application/api/a ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidOperation:(System.Net.HttpWebRequest:HttpWebRequest)[Invoke-WebRequest], 引发WebException + FullyQualifiedErrorId:WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
答案 0 :(得分:62)
使用:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
取自 Powershell 3.0 Invoke-WebRequest HTTPS Fails on All Requests
答案 1 :(得分:6)
在我的情况下,TLS技巧无效,这似乎是powershell中的错误。您需要使用.net代码而不是脚本块添加回调。
#C# class to create callback
$code = @"
public class SSLHandler
{
public static System.Net.Security.RemoteCertificateValidationCallback GetSSLHandler()
{
return new System.Net.Security.RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => { return true; });
}
}
"@
#compile the class
Add-Type -TypeDefinition $code
#disable checks using new class
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = [SSLHandler]::GetSSLHandler()
#do the request
try
{
invoke-WebRequest -Uri myurl -UseBasicParsing
} catch {
# do something
} finally {
#enable checks again
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null
}
答案 2 :(得分:1)
[Net.ServicePointManager] :: SecurityProtocol = [Net.SecurityProtocolType] :: Tls -bor [Net.SecurityProtocolType] :: Tls11 -bor [Net.SecurityProtocolType] :: Tls12
在Windows Server 2016中工作
重大次要版本修订
5 1 17763 1007