[英]powershell invoke-webrequest does not work but invoke-restmethod works
I want to get the content of a web page and when I use我想获取网页的内容以及何时使用
$web = Invoke-RestMethod -Uri "https://inkscape.org/"
I will get the content but when I use $web = Invoke-RestMethod -Uri "https://inkscape.org/"
我会得到内容但是当我使用
$web = Invoke-WebRequest -Uri "https://inkscape.org/"
I won't get anything why it happens?? $web = Invoke-WebRequest -Uri "https://inkscape.org/"
我什么也得不到,为什么会这样?? and what is the difference exactly ??到底有什么区别?
Simply put, for plain-text or HTML response bodies, the relationship between the (older) Invoke-WebRequest
cmdlet and the Invoke-RestMethod
cmdlet is as follows with the respect to the default GET
method:简单地说,对于纯文本或HTML响应正文,(旧的)
Invoke-WebRequest
cmdlet 和Invoke-RestMethod
cmdlet 之间的关系相对于默认GET
方法如下:
# -UseBasicParsing is only needed in *Windows PowerShell*.
(Invoke-WebRequest -UseBasicParsing -Uri "https://inkscape.org/").Content
is the same as:是相同的:
Invoke-RestMethod -Uri "https://inkscape.org/"
That is:那是:
Invoke-WebRequest
returns a response object whose .Content
property contains the body of the response, always as text (except if you save the raw body to a file with -OutFile
). Invoke-WebRequest
返回一个响应对象,其.Content
属性包含响应的正文,始终为文本(除非您使用-OutFile
将原始正文保存到文件中)。
.ParsedHTML
property, using the obsolete Internet Explorer ..ParsedHTML
属性显示。 -UseBasicParsing
suppresses this. -UseBasicParsing
抑制了这一点。 This switch has no effect in PowerShell (Core) 7+ , which fundamentally doesn't support parsing HTML, requiring third-party solutions (see this answer for an example) or - on Windows only - a COM-based solution (see this answer ). Invoke-RestMethod
directly returns the response body (only). Invoke-RestMethod
直接返回响应正文(仅)。
Additionally, if the target site indicates that XML or JSON data is being returned, Invoke-RestMethod
doesn't return the body as text , but automatically parses it into an [xml]
instance / [System.Xml.XmlElement]
instances (for RSS / Atom feeds) or a [pscustomobject]
graph ( ConvertFrom-Json
is built in , so to speak).此外,如果目标站点指示正在返回 XML 或 JSON 数据,则
Invoke-RestMethod
不会将正文返回为text ,而是自动将其解析为[xml]
实例 / [System.Xml.XmlElement]
实例(用于 RSS / Atom 提要)或[pscustomobject]
图( ConvertFrom-Json
是内置的,可以这么说)。
Even in the absence of a known response data format, PowerShell tries to parse the response body, first as XML, then as JSON;即使没有已知的响应数据格式,PowerShell 也会尝试解析响应主体,首先是 XML,然后是 JSON; if all attempts fail, plain text (a
[string]
instance) is returned.如果所有尝试都失败,则返回纯文本(
[string]
实例)。
Even for text/html
responses an attempt is made to parse them as XML .即使对于
text/html
响应,也会尝试将它们解析为 XML 。 That is, if a page happens to be valid XML (which is rare these days), you'll get an [xml]
instance back;也就是说,如果一个页面恰好是有效的 XML(现在很少见),您将获得一个
[xml]
实例; for instance, the very simple HTML5 page at https://httpbin.org/html happens to be valid XML (excluding the <!DOCTYPE html>
declaration), whereas HTML5 pages in general are not .例如, https: //httpbin.org/html 上非常简单的 HTML5 页面恰好是有效的 XML(不包括
<!DOCTYPE html>
声明),而 HTML5 页面通常不是. Thus,因此,
(Invoke-RestMethod https://httpbin.org/html).GetType().FullName
returns System.Xml.XmlDocument
, ie an [xml]
instance. (Invoke-RestMethod https://httpbin.org/html).GetType().FullName
返回System.Xml.XmlDocument
,即一个[xml]
实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.