简体   繁体   English

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

[英]Invoke-RestMethod hangs on long running endpoints

We are using Invoke-RestMethod in a PowerShell script to call a GET method endpoint with a variable length runtime.我们在 PowerShell 脚本中使用Invoke-RestMethod来调用具有可变长度运行时的GET方法端点。 Some calls may return after a couple of seconds, some may take up to 20 minutes.有些电话可能会在几秒钟后返回,有些可能需要长达 20 分钟。 We've set a 50 minute timeout on the call via the -TimeoutSec parameter.我们通过-TimeoutSec参数设置了 50 分钟的通话超时。

Calls that take only a couple of seconds return fine and output the expected response.只需要几秒钟的调用返回正常,output 是预期的响应。 Longer calls (5 minutes, for example) never return and the Invoke-RestMethod command uses up the entire 50 minutes timeout, despite us confirming on the web server logs that the server has long since returned a 200 OK .尽管我们在 web 服务器日志上确认服务器早已返回200 OK ,但更长的调用(例如 5 分钟)永远不会返回,并且Invoke-RestMethod命令用完整个 50 分钟超时。

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
}

There is no authentication on the endpoint.端点上没有身份验证。 The PowerShell version is 7. The script is being ran on the same machine hosting the web server being called. PowerShell 版本为 7。该脚本在托管被调用的 web 服务器的同一台机器上运行。

Is this a configuration issue with Invoke-RestMethod that we are not aware of?这是我们不知道的Invoke-RestMethod配置问题吗? We had similar issues with Invoke-WebRequest using essentially the same script.我们在使用基本相同的脚本时遇到了类似的Invoke-WebRequest问题。

We were able to resolve this problem by adding the -DisableKeepAlive switch to the Invoke-RestMethod command.我们能够通过将-DisableKeepAlive开关添加到Invoke-RestMethod命令来解决此问题。 It seems the HTTP keep-alive feature prevented PowerShell from concluding those long running calls.似乎 HTTP 保持活动功能阻止了 PowerShell 结束那些长期运行的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM