我们有两个执行POST和GET请求的API。两者过去都可以正常工作,但是执行POST的API开始出现错误:
Invoke-WebRequest:基础连接为:接收时发生意外错误。
我已经尝试研究了几天,所有的KB都指向某种SSL / TLS,并添加了以下代码:
[Net.ServicePointManager]::SecurityProtocol = "SystemDefault,Tls12, Tls11, Tls, Ssl3"
但是我从一开始就已经有了这段代码。但是,我找不到解决问题的方法。
OS:Windows 2012
PowerShell版本:4.0
function funName ($Val1, $Val2) {
[Net.ServicePointManager]::SecurityProtocol = "SystemDefault,Tls12, Tls11, Tls, Ssl3"
#[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
$url = "https://someAPI/post.request/do-something"
$user = "username"
$pass = "password"
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds "
$Headers = @{
"Accept"="application/json"
Authorization = $basicAuthValue
}
$Body = @{
'@type' ='x'
parm1 = $Val1
parm2 = $Val2
}
#[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $url -Headers $Headers -Method Post -Body $Body | Out-Null
}
## Deactivation Request ffffffff##
funName -RequestID Val1 adID Val2
如前所述,它一直工作到上周。
答案 0 :(得分:1)
将其设置为脚本的顶部:
Add-Type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
namespace myTrust
{
public class TrustAllCertsPolicy : ICertificatePolicy
{
public bool CheckValidationResult( ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
{
return true;
}
}
}
"@
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
[System.Net.ServicePointManager]::CertificatePolicy = New-Object myTrust.TrustAllCertsPolicy
答案 1 :(得分:0)
我正在处理一个类似的请求,以从API检索数据,但发现我正在以funName -val1 1234 val2 9999
的形式调用函数,而我的seconda参数缺少“-”。我确定它会再次开始工作funName -val1 1234 -val2 9999
。感谢Stackoverflow社区对此提供的帮助。