Invoke-RestMethod {消息:“请求无效。”}

时间:2016-10-13 19:18:08

标签: rest api powershell

以下curl命令成功进行API调用并返回预期输出:

curl -X GET "https://<rootURI>/calc/add?a={14}&b={22}" -H "Ocp-Apim-Subscription-Key: <key value>" --insecure

当我尝试使用Invoke-RestMethod时,我收到以下回复:

ERROR: Invoke-RestMethod : {"Message":"The request is invalid."}

我的PowerShell代码段是:

add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
    public bool CheckValidationResult(
        ServicePoint srvPoint, X509Certificate certificate,
        WebRequest request, int certificateProblem) {
        return true;
    }
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

$header = @{ 'Ocp-Apim-Subscription-Key' = '<key value>' }
$uri = 'https://<rootURI>/calc/add?a={14}&b={22}'

Invoke-RestMethod -Method Get -Uri $uri -Headers $header

我还尝试使用以下行强制使用TLS 1.2并获得相同的结果。

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

有什么想法吗?

0 个答案:

没有答案