当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 在 PowerShell 中运行 Curl 命令 [2 种方法]

作者:精品下载站 日期:2024-12-14 05:29:32 浏览:14 分类:玩电脑

在 PowerShell 中运行 Curl 命令 [2 种方法]


[玩转系统] 在 PowerShell 中运行 Curl 命令 [2 种方法]

在 PowerShell 3.0 及更高版本中,我们有 Invoke-WebRequestInvoke-RestMethod cmdlet; curl 是 PowerShell 中 Invoke-WebRequest 的别名。大多数学习者都会将其与 Invoke-RestMethod cmdlet 混淆。在 PowerShell 7 及更高版本中,Invoke-RestMethod cmdlet 是用于发送 RESTful Web 服务请求的默认 cmdlet。

它是 PowerShell 中的另一个内置 cmdlet,主要设计用于使用 RESTful API 和 Web 服务。它通过自动将响应解析为 XML 或 JSON,使发送和处理 Web 请求变得简单,并支持基本的 OAuth 身份验证和基本身份验证。

但是,Invoke-WebRequestcurl 在 PowerShell 中仍然可用,它们向 API 和 Web 服务器发送 HTTPS 和 HTTP 请求。在处理非 HTTP 协议和自定义请求方面,Invoke-WebReqest cmdlet 比 Invoke-RestMethod cmdlet 提供了更大的灵活性。

还需要澄清吗?想要检查命令的别名吗?您可以使用 Get-Alias cmdlet 在 PowerShell 中获取具有各自名称、版本和源的所有别名。但是,Get-Comman curl 将仅显示带有相应命令、版本和源的别名。

因此,使用本机 PowerShell cmdlet 比使用别名更合适。不过,我们将学习这两种方法(Invoke-WebRequestcurl),并让您根据自己的要求进行选择。

使用 Invoke-WebRequest Cmdlet

使用 Invoke-WebRequest cmdlet 从 PowerShell 中的指定 URL 检索数据。

使用 Invoke-WebRequest Cmdlet:

Invoke-WebRequest -Uri "https://www.example.com"

输出 :

StatusCode        : 200
StatusDescription : OK
Content           : 
                    
                    
                        <title>Example Domain</title>
                        <meta charset="utf-8">
                        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
                        <meta name="viewport" conten... rawcontent : http ok age: vary: accept-encoding x-cache: hit content-length: cache-control: max-age="604800" content-type: text charset="UTF-8" date: sat apr gmt exp... forms headers images inputfields links information... innertext="More" outerhtml="<A" href="https://www.iana.org/domains/example">More information...; outerText=More
                    information...; tagName=A; href=https://www.iana.org/domains/example}}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 1256

我们使用带有 -Uri 参数的 Invoke-WebRequest cmdlet 从 Internet 上的指定网页获取内容。 -Uri 参数用于指定向其发送 Web 请求的 Internet 资源(Web 服务或页面)的统一资源标识符 (URI)。

请注意,-Uri 参数仅支持 HTTP 和 HTTPS。因此,使用该参数是强制性的,但参数名称 -Uri 是可选的,这意味着我们可以在不使用 -Uri 参数的情况下指定网页或服务的 URL姓名。

返回到 Invoke-WebRequest cmdlet,它向给定网页或服务发送 HTTP 和 HTTPS 请求,解析响应并返回图像、链接和其他基本 HTML 元素的集合。

您可以看到上面的输出作为演示;我们得到了 StatusCodeStatusDescriptionContentRawContent 和其他重要信息。我们还可以将此响应存储在变量中,并使用点表示法检索特定信息。例如,在下面的代码中,我们使用点表示法来访问响应的 Content 属性。

使用 Invoke-WebRequest Cmdlet:

$response = Invoke-WebRequest -Uri "https://www.example.com"
$response.Content

输出 :

    <title>Example Domain</title>
    <meta charset="utf-8">
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI",
        "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        div {
            margin: 0 auto;
            width: auto;
        }
    }
    </style>


<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>

或者,我们可以使用 Invoke-WebRequest 作为 iwr 来避免击键。使用iwr不会影响结果;我们仍然会得到相同的输出。

使用curl命令

使用 curl cmdlet 从 PowerShell 中的指定 URL 检索数据。

使用curl命令:

curl "https://www.example.com"

输出 :

StatusCode        : 200
StatusDescription : OK
Content           : 
                    
                    
                        <title>Example Domain</title>
                        <meta charset="utf-8">
                        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
                        <meta name="viewport" conten... rawcontent : http ok age: vary: accept-encoding x-cache: hit content-length: cache-control: max-age="604800" content-type: text charset="UTF-8" date: sat apr gmt exp... forms headers images inputfields links information... innertext="More" outerhtml="<A" href="https://www.iana.org/domains/example">More information...; outerText=More
                    information...; tagName=A; href=https://www.iana.org/domains/example}}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 1256

看,我们得到了与 Invoke-WebRequest -Uri "https://www.example.com" 命令返回的相同的输出,因为 curl 的别名>调用WebRequest。因此,我们可以将响应存储在变量中,并使用点表示法访问其 Content 属性,如下所示,这也必须与我们在使用 Invoke-WebRequest 时收到的结果相同代码>.

使用curl命令:

$response = curl "https://www.example.com"
$response.Content

输出 :

    <title>Example Domain</title>
    <meta charset="utf-8">
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI",
        "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        div {
            margin: 0 auto;
            width: auto;
        }
    }
    </style>


<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>

这就是如何在PowerShell中运行curl命令的全部内容。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯