代码之家  ›  专栏  ›  技术社区  ›  kenorb

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

  •  3
  • kenorb  · 技术社区  · 7 年前

    当我使用以下命令时:

    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
    

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

    如何显示发送的邮件头?

    1 回复  |  直到 7 年前
        1
  •  3
  •   Frode F.    7 年前

    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