调用 API 时出现问题
Problems when calling an API
我们有两个 API 执行 POST 和 GET 请求。他们两个过去都工作得很好,但是 POST 的 API 开始出错:
Invoke-WebRequest : The underlying connection was: An unexpected error occurred on a receive.`
几天来我一直在努力研究,所有知识库都指向某种 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
如前所述,这种情况一直持续到上周。
将其设置为脚本的顶部:
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
我正在处理从 API 检索数据的类似请求,我发现我将我的函数调用为 funName -val1 1234 val2 9999
,而我的 seconda 参数中缺少“-”。一旦我修复它再次开始工作 funName -val1 1234 -val2 9999
。感谢 Whosebug 社区对此的帮助。
我们有两个 API 执行 POST 和 GET 请求。他们两个过去都工作得很好,但是 POST 的 API 开始出错:
Invoke-WebRequest : The underlying connection was: An unexpected error occurred on a receive.`
几天来我一直在努力研究,所有知识库都指向某种 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
如前所述,这种情况一直持续到上周。
将其设置为脚本的顶部:
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
我正在处理从 API 检索数据的类似请求,我发现我将我的函数调用为 funName -val1 1234 val2 9999
,而我的 seconda 参数中缺少“-”。一旦我修复它再次开始工作 funName -val1 1234 -val2 9999
。感谢 Whosebug 社区对此的帮助。