[英]Rest API call works with curl but doesn't work with PowerShell
我有以下有效的curl
請求
curl.exe -X GET "https://host.crm.dynamics.com/api/data/v9.1/accounts?$select=name" -H "Authorization: Bearer xxxxx"
並想在 PowerShell 中實現它
Invoke-RestMethod "https://host.crm.dynamics.com/api/data/v9.1/accounts?`$select=name" `
-Headers @{Authorization = "Bearer xxxxx"} `
-Method Get
我還嘗試使用Invoke-WebRequest
以及 Powershell 中的curl
版本進行請求,但沒有奏效
Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
At E:\Repos\myrepo\host\connection_test.ps1:51 char:1
+ Invoke-RestMethod $hostName `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
使用 Invoke-RestMethod 建立連接時,您要確保 URI 接受客戶端使用的加密。
默認情況下,PowerShell 對 Web 請求使用 TLS 1.0,但您連接到的 URI 可能使用 1.1 或 1.2。 您可以使用以下命令強制使用 TLS v1.1 或 v1.2。
[Net.ServicePointManager]::SecurityProtocol = `
[Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12;
Invoke-RestMethod "https://host.crm.dynamics.com/api/data/v9.1/accounts?`$select=name" `
-Headers @{Authorization = "Bearer xxxxx"} `
-Method Get
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.