简体   繁体   English

powershell invoke-webrequest 不起作用,但 invoke-restmethod 有效

[英]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将原始正文保存到文件中)。

    • For HTML response bodies, Windows PowerShell attempts to also parse the HTML text into an HTML DOM, surfaced via the .ParsedHTML property, using the obsolete Internet Explorer .对于 HTML 响应正文, Windows PowerShell还尝试使用过时的Internet Explorer将 HTML 文本解析为 HTML DOM,通过.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 ).此开关在PowerShell (Core) 7+中无效,它从根本上不支持解析 HTML,需要第三方解决方案(请参阅此答案以获取示例)或 - 仅在 Windows 上 - 基于 COM 的解决方案(请参阅此答案)。
  • 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.

相关问题 如何更改Powershell Invoke-WebRequest或Invoke-RestMethod的ReadWriteTimeout? - how to change ReadWriteTimeout for powershell Invoke-WebRequest or Invoke-RestMethod? 将curl命令转换为invoke-WebRequest或Invoke-RestMethod - convert curl command to invoke-WebRequest or Invoke-RestMethod Invoke-WebRequest 和 Invoke-RestMethod 的不同结果 - Different results from Invoke-WebRequest and Invoke-RestMethod 如何正确使用 Jenkins PowerShell 步骤调用 ZC6E190B284633C48E39E55049DA3CCERequest8Z ZDB974238714CA8DE634A7CE1D083A1vokeF 使用 InMethodest-WebvokeF - How To Correctly Use Jenkins PowerShell step to call Web API using Invoke-WebRequest or Invoke-RestMethod Powershell Invoke-RestMethod - Powershell Invoke-RestMethod 调用 RESTMethod PowerShell - Invoke-RESTMethod PowerShell 使用PowerShell调用-RemMethod - Invoke-RestMethod with PowerShell 使用-OutFile参数时如何在Invoke-WebRequest / Invoke-RestMethod上获取HTTP状态? - How to get HTTP status on Invoke-WebRequest/Invoke-RestMethod when using -OutFile parameter? 为什么 Invoke-WebRequest 和 Invoke-RestMethod 同时失败和成功? - Why is Invoke-WebRequest and Invoke-RestMethod failing and succeeding at the same time? Powershell Invoke-WebRequest 有效,但 Python Requests 无效 - Powershell Invoke-WebRequest works but Python Requests does not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM