0

我有一个http://xx.yy.zz.aaa:8080/api/2.0/GetVersion需要从 Powershell 脚本中访问的 API。当我尝试使用Invoke-RestMethod它击中它时,我会在10 minutes.

在此处输入图像描述

当我使用相同的 API 时,Invoke-WebRequest我得到相同的响应10 minutes

在此处输入图像描述

$ProgressPreference = 'SilentlyContinue'在进行 API 调用之前添加了,但似乎没有效果。

而如果我使用 PowerShell 从 PowerShell 访问相同的 API,Invoke-WebRequest或者Invoke-RestMethod我会在几秒钟内得到响应。我的 powerShell 版本是5.1.14393.4530

在此处输入图像描述

这是我的脚本:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$pair = "$($username):$($password)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$headers.Add("Authorization", "Basic $encodedCreds")


function verifyDeployment($vapor_env, $headers) {
    try {
        $uri = "http://{0}:8080/api/2.0/{1}" -f "xx.yy.zz.aaa", "GetVersion"
        $ProgressPreference = 'SilentlyContinue'
        $response = Invoke-WebRequest -URI $uri -Method Get -Headers $headers -UseBasicParsing
        $statusCode = $response | Select-Object -Expand StatusCode
        Write-Output $response, $uri, $statusCode, $api
        if ($statusCode -eq 200) {
            $version = $response | Select-Object -Expand Content | ConvertFrom-Json | Select-Object -Expand Version
            if ($version -eq "12.14.90") {
                Write-Output "Version matched."
            }
            else {
                Write-Output "Version didn't match."
            }
        }
        else {
            Write-Output "Couldn't hit API."
        }
    }
    catch {}
}


verifyDeployment $vapor_env $headers

我能做些什么来减少响应时间。

谢谢。

4

1 回答 1

1

该问题是由于来自 API 的大量响应数据造成的。现在我正在使用分页,它大大减少了响应时间。

于 2021-09-17T06:10:04.637 回答