cUrl 与调用 WebRequest

cUrl vs Invoke-WebRequest

谁能向我解释为什么 cUrl(真正的 cUrl)有效而 Invoke-WebRequest 无效?同一台机器,相同的变量。在我看来,他们应该都在做同样的事情,将文件上传到 jfrog Artifactory。

$headers = @{
  'X-JFrog-Art-Api' = $apiKey
  "Content-Type" = "application/json"
  "Accept" = "application/json"
}

Invoke-WebRequest -InFile $file -Method Put -Uri "$ARTIFACTORY_HOST/third-party/test/readme.md" -Headers $headers -Verbose

这个 PowerShell 不工作。

curl -T readme.md "${ARTIFACTORY_HOST}/third-party/test/readme.md " \
-H "X-JFrog-Art-Api: ${apiKey}" \
-H "Content-Type: application/json" \
-H "Accept: application/json"

cUrl 有效。

PowerShell 失败

Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At line:1 char:1
+ Invoke-WebRequest -InFile $file -Method Put -Uri "https:// ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

原来 PowerShell 默认为错误的 TLS 版本,需要特别告知才能使用 1.2

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

把它放在 Invoke-WebRequest 前面就可以了。