0

我们有两个执行 POST 和 GET 请求的 API。它们过去都可以正常工作,但是执行 POST 的 API 开始出现错误:

Invoke-WebRequest:底层连接是:接收时发生意外错误。`

几天来,我一直在尝试研究,所有 KB 都指向某种 SSL/TLS 并添加了这段代码:

[Net.ServicePointManager]::SecurityProtocol = "SystemDefault,Tls12, Tls11, Tls, Ssl3"

但我从一开始就已经有了这个代码。但是,我找不到解决问题的方法。

操作系统: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

如前所述,这种情况一直持续到上周。

4

2 回答 2

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
于 2019-05-16T14:16:37.067 回答
0

我正在处理一个类似的从 API 检索数据的请求,我发现我正在调用我的函数,funName -val1 1234 val2 9999因为我在 seconda 参数上缺少“-”。一旦我修复它,它就会重新开始工作funName -val1 1234 -val2 9999。感谢 Stackoverflow 社区对此的帮助。

于 2019-06-07T14:10:52.333 回答