powershell - PowerShell 调用-RestMethod 超时
问题描述
如果通过 PowerShell 命令可用,我会检查一些服务(回旋处 15)。如果服务可用,则脚本运行时间很好,但如果某些服务不可用,则脚本运行时间不好!那么如何确定Invoke-RestMethod
请求/响应的特定时间呢?我知道有一个 Parameter -TimeoutSec
。例如,如果我在请求超时 4 秒后没有得到响应并抛出错误!
Invoke-RestMethod $uri -TimeoutSec
也许我误解了 Parameter -TimeoutSec
?
解决方案
根据帮助:
> get-help Invoke-RestMethod -Parameter Timeoutsec
-TimeoutSec <Int32>
Specifies how long the request can be pending before it times out. Enter a value in seconds. The default value, 0, specifies an indefinite time-out.
所以参数需要一个整数来表示超时应该多长时间。
Invoke-RestMethod $uri -TimeoutSec 4
请注意帮助中的警告:
A Domain Name System (DNS) query can take up to 15 seconds to return or time out. If your request contains a host name that requires resolution, and you set TimeoutSec to a value greater than zero, but less than 15 seconds, it can take 15
seconds or more before a WebException is thrown, and your request times out.
推荐阅读
- javascript - JavaScript 函数抛硬币函数挑战
- reactjs - 在故事书中使用材质 UI 组件
- python-3.x - 是否可以仅使用 zoautil_py 模块从数据集属性成员读取文本并打印数据集?
- html - WordPress Docent 主题如何左对齐 h2 博客标题
- r - 在 R 中选择性地过滤多个变量
- haskell - 预期类型与实际类型不匹配
- google-apps-script - 已验证的 Google 表格插件(SurveyMonkey 应用)出现“此应用被阻止”错误
- c++ - 字符串的 C++ 向量转换为整数的关联向量
- vue.js - 将 Getter 传递给 Vuex Action 返回 null
- python - 使用 Requests 抓取 ajax 页面