Powershell Invoke WebRequest错误:发送时发生意外错误

Powershell Invoke WebRequest错误:发送时发生意外错误,powershell,invoke-webrequest,Powershell,Invoke Webrequest,当我使用cURL.exe运行以下命令以上载文件时,我没有任何问题: Curl.exe -X POST -H "X-API-TOKEN: token here" -F file=@"E:\program files\curl\filetoupload.csv" https://url.com/files 我需要从脚本运行它,所以我想我应该使用PowerShell的Invoke-WebRequest Invoke-WebRequest : The unde

当我使用cURL.exe运行以下命令以上载文件时,我没有任何问题:

Curl.exe -X POST -H "X-API-TOKEN: token here" -F file=@"E:\program files\curl\filetoupload.csv" 
https://url.com/files
我需要从脚本运行它,所以我想我应该使用PowerShell的Invoke-WebRequest

Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
 At E:\Scripts\Celero\qualtrics.ps1:8 char:13
+ $response = Invoke-WebRequest -Headers $headers -Method Post -OutFile ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke- 
WebRequest], WebException
+ FullyQualifiedErrorId : 
WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"

 $filepath = "e:\program files\curl\filetoupload.csv"
 $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
 $headers.Add("X-API-TOKEN", "token here")
 $urlAPI = "https://url.com/files"

 $response = Invoke-WebRequest -Headers $headers -Method 'Post' -OutFile $filepath -URI $urlAPI
我试着添加

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

没有运气。有什么想法吗

更新 我最终在PowerShell中完成了以下工作:

param ($curlExe, $filepath, $headers,$qualtricsAPI,$jobfile)

try{
  Copy-Item $jobfile -Destination $filepath
$upload = & $curlExe -X POST -H $headers -s -o -w "%{http_code}" -F file=@$filepath $qualtricsAPI
if ($upload -match "200 - OK"){
    Write-Host "Upload successful."
    Write-Host "Response: "$upload
    Remove-Item $filepath
    Write-Host "File removed from $($filepath)."
    exit 0
}else{
    Write-Host "Upload not successful"
    Write-Host "Response: "$upload
    exit 2
}
}catch{
    "Exception Message: $($_.Exception.Message)"
    exit 1
}

我用我们的文件传输自动化工具对它进行了测试,结果是成功的。我在Invoke-WebRequest cmdlet上旋转轮子。

我们遇到了类似的问题。最后,这是一个网络防火墙问题(Palo Alto在传输过程中切断了连接)。我仍然无法解释为什么cURL和wget有效而iwr无效,显然协议是相同的。我们打开了服务器的443端口,它工作了。我们还曾经打开服务器上的协议。我不相信魔法,但这一点很接近。我们在互联网上找不到任何东西,除了在问题中提到的脚本中添加协议的一般建议。我希望这会有所帮助。服务器上的端口443已打开。我们的安全团队不希望在服务器上启用1.1和1.0,但1.2是。最后,我得到了curl.exe的命令,可以在带有变量等的Powershell脚本中工作。很高兴听到您的命令可以工作。我很想听到任何人从网络的角度读到这篇文章,了解cURL和iwr之间的区别。这只是另一条信息:当TLS设置不适用于我时,我在我的家庭ISP(comcast)上遇到了与iwr相同的问题,我试着在我的T-Mobile热点上做同样的事情,但没有出错。没有解决根本的问题,但可能说明它不一定与端点的设置有关?我们也有类似的问题。最后,这是一个网络防火墙问题(Palo Alto在传输过程中切断了连接)。我仍然无法解释为什么cURL和wget有效而iwr无效,显然协议是相同的。我们打开了服务器的443端口,它工作了。我们还曾经打开服务器上的协议。我不相信魔法,但这一点很接近。我们在互联网上找不到任何东西,除了在问题中提到的脚本中添加协议的一般建议。我希望这会有所帮助。服务器上的端口443已打开。我们的安全团队不希望在服务器上启用1.1和1.0,但1.2是。最后,我得到了curl.exe的命令,可以在带有变量等的Powershell脚本中工作。很高兴听到您的命令可以工作。我很想听到任何人从网络的角度读到这篇文章,了解cURL和iwr之间的区别。这只是另一条信息:当TLS设置不适用于我时,我在我的家庭ISP(comcast)上遇到了与iwr相同的问题,我试着在我的T-Mobile热点上做同样的事情,但没有出错。不能解决根本问题,但可能说明它不一定与端点的设置相关?
param ($curlExe, $filepath, $headers,$qualtricsAPI,$jobfile)

try{
  Copy-Item $jobfile -Destination $filepath
$upload = & $curlExe -X POST -H $headers -s -o -w "%{http_code}" -F file=@$filepath $qualtricsAPI
if ($upload -match "200 - OK"){
    Write-Host "Upload successful."
    Write-Host "Response: "$upload
    Remove-Item $filepath
    Write-Host "File removed from $($filepath)."
    exit 0
}else{
    Write-Host "Upload not successful"
    Write-Host "Response: "$upload
    exit 2
}
}catch{
    "Exception Message: $($_.Exception.Message)"
    exit 1
}