小编Rya*_*n B的帖子

Invoke-RestMethod 挂在长时间运行的端点上

我们Invoke-RestMethod在 PowerShell 脚本中使用来调用GET具有可变长度运行时的方法端点。有些电话可能会在几秒钟后返回,有些可能需要长达 20 分钟。我们通过-TimeoutSec参数设置了 50 分钟的通话超时。

只需几秒钟的调用即可正常返回并输出预期的响应。更长的调用(例如 5 分钟)永远不会返回并且Invoke-RestMethod命令用完整个 50 分钟超时,尽管我们在 Web 服务器日志中确认服务器早已返回200 OK.

try 
{
    $Url = "https://example.com/task"   # GET
    $Timeout = 3000                     # 50 minute timout
    $Response = Invoke-RestMethod $Url -TimeoutSec $Timeout
        
    Write-Host $Response
}
catch 
{
    Write-Host $_.Exception
}
Run Code Online (Sandbox Code Playgroud)

端点上没有身份验证。PowerShell 版本为 7。该脚本在托管被调用的 Web 服务器的同一台机器上运行。

这是Invoke-RestMethod我们不知道的配置问题吗?我们在Invoke-WebRequest使用基本相同的脚本时遇到了类似的问题。

powershell invoke-webrequest invoke-restmethod

7
推荐指数
1
解决办法
208
查看次数