[英]how to change ReadWriteTimeout for powershell Invoke-WebRequest or Invoke-RestMethod?
[英]powershell invoke-webrequest does not work but invoke-restmethod works
我想獲取網頁的內容以及何時使用
$web = Invoke-RestMethod -Uri "https://inkscape.org/"
我會得到內容但是當我使用
$web = Invoke-WebRequest -Uri "https://inkscape.org/"
我什么也得不到,為什么會這樣?? 到底有什么區別?
簡單地說,對於純文本或HTML響應正文,(舊的) Invoke-WebRequest
cmdlet 和Invoke-RestMethod
cmdlet 之間的關系相對於默認GET
方法如下:
# -UseBasicParsing is only needed in *Windows PowerShell*.
(Invoke-WebRequest -UseBasicParsing -Uri "https://inkscape.org/").Content
是相同的:
Invoke-RestMethod -Uri "https://inkscape.org/"
那是:
Invoke-WebRequest
返回一個響應對象,其.Content
屬性包含響應的正文,始終為文本(除非您使用-OutFile
將原始正文保存到文件中)。
Invoke-RestMethod
直接返回響應正文(僅)。
此外,如果目標站點指示正在返回 XML 或 JSON 數據,則Invoke-RestMethod
不會將正文返回為text ,而是自動將其解析為[xml]
實例 / [System.Xml.XmlElement]
實例(用於 RSS / Atom 提要)或[pscustomobject]
圖( ConvertFrom-Json
是內置的,可以這么說)。
即使沒有已知的響應數據格式,PowerShell 也會嘗試解析響應主體,首先是 XML,然后是 JSON; 如果所有嘗試都失敗,則返回純文本( [string]
實例)。
即使對於text/html
響應,也會嘗試將它們解析為 XML 。 也就是說,如果一個頁面恰好是有效的 XML(現在很少見),您將獲得一個[xml]
實例; 例如, https: //httpbin.org/html 上非常簡單的 HTML5 頁面恰好是有效的 XML(不包括<!DOCTYPE html>
聲明),而 HTML5 頁面通常不是. 因此,
(Invoke-RestMethod https://httpbin.org/html).GetType().FullName
返回System.Xml.XmlDocument
,即一個[xml]
實例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.