Invoke-RestMethod:powershell 脚本中的底层连接已关闭错误

如何解决Invoke-RestMethod:powershell 脚本中的底层连接已关闭错误

我在 PowerShell 中编写了代码以通过 wiql 获取项目区域的工作项。

$token = "PAT"

$url="https://dev.azure.com/Organizationname/_apis/wit/wiql?api-version=5.1"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$JSON = @'
{
   "query": "SELECT [System.Id],[System.WorkItemType],[System.State],[System.AreaPath],[System.Tags],[System.CommentCount],[System.ChangedDate] FROM workitems WHERE[System.Id] IN(@follows) AND [System.TeamProject] = 'Azure' AND [System.State] <> '' ORDER BY [System.ChangedDate] DESC"
}
'@

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json

$listofTasks = New-Object Collections.Generic.List[String]
ForEach( $workitem in $response.workItems ) {
  $listofTasks.Add($workitem.id)

}
$listofTasks = $listofTasks -join ','
$listofTasks

$url1="https://dev.azure.com/Organizationname/ProjectName/_apis/wit/workitems?ids=$listofTasks" +"&" + "`$expand" + "=all&api-version=5.1"

$response1 = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = "Basic $token"} -Method Get 

Write-Host "result = $($response1 | ConvertTo-Json -Depth 100)"

当我执行 aboce 脚本时出现以下错误

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a receive.
At ....
+ ... response1 = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = " ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod],WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

我尝试了以下链接中提到的选项,但没有帮助。

https://blog.darrenjrobinson.com/powershell-the-underlying-connection-was-closed-an-unexpected-error-occurred-on-a-send/

有人可以帮我解决这个错误吗? 提前致谢!!!

解决方法

这通常是由于您的 TLS 版本。

尝试添加:

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

到脚本的顶部并重新运行它。

,

你的脚本在我这边运行良好。问题似乎与您的客户端机器环境有关。您可以检查 this blog 以查看解决方案是否对您有帮助:

问题在于旧版本的 DOTNET 框架(即 3.5 和 4.0)本身并不支持新版本的 TLS,如 1.2 和 1.3。您可以尝试安装较新版本的 DOTNET。可以在此处找到 Microsoft 有关 SSL/TLS 的更多信息:

https://docs.microsoft.com/en-us/dotnet/framework/network-programming/tls

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?