如何使用Invoke-WebRequest查看发送的标头?

问题描述 投票:0回答:2

当我使用以下命令时:

Invoke-WebRequest -UseBasicParsing -Uri http://example.com -SessionVariable Foo -UserAgent Bar

我得到以下输出:

StatusCode        : 200
StatusDescription : OK
Content           : <!doctype html>
                    <html>
                    <head>
                        <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/1.1 200 OK
                    Vary: Accept-Encoding
                    X-Cache: HIT
                    Content-Length: 1270
                    Cache-Control: max-age=604800
                    Content-Type: text/html
                    Date: Mon, 19 Feb 2018 16:38:28 GMT
                    Expires: Mon, 26 Feb 2018 16:38...
Forms             : 
Headers           : {[Vary, Accept-Encoding], [X-Cache, HIT], [Content-Length, 
                    1270], [Cache-Control, max-age=604800]...}
Images            : {}
InputFields       : {}
Links             : {@{outerHTML=<a 
                    href="http://www.iana.org/domains/example">More 
                    information...</a>; tagName=A; 
                    href=http://www.iana.org/domains/example}}
ParsedHtml        : 
RawContentLength  : 1270

但问题是,我看不到发送的标头(会话变量或用户代理)。也许它被

...
截断了?

如何显示已发送的标头?

powershell webrequest
2个回答
4
投票

Invoke-WebRequest
不存储请求标头 AFAIK。如果您想要标头中的特定值,那么它希望您使用参数
-Headers
-UserAgent
来指定它们。

如果您愿意,

HttpWebRequest
WebRequest
可以提供更多控制。

$req = [System.Net.HttpWebRequest]::Create("http://www.stackoverflow.com")
$res = $req.GetResponse()

$req.Headers.Keys | % { "$_ = $($req.Headers[$_])" }
Host = stackoverflow.com

$req | fl Method, UserAgent, ProtocolVersion
Method          : GET
UserAgent       :
ProtocolVersion : 1.1

0
投票

使用 Powershell 获取 HTTP 标头。

$url = 'https://jsonplaceholder.typicode.com/posts' 
$result = Invoke-WebRequest -Method HEAD -Uri $url -UseBasicParsing # can be used curl instead Invoke-WebRequest
$result.RawContent # outup in raw
$result.Headers # Get the HTTP headers formatted as a table.
$result.Headers.Date # Get a specific item from the HTTP header.

来源:https://techexpert.tips/powershell/powershell-get-http-headers/

© www.soinside.com 2019 - 2024. All rights reserved.