如何抓取HTTP标头?(重定向网址?



我在浏览器中安装了一个插件,它让我可以看到从我的Firefox和Web服务器来回传输的所有HTTP标头。

这是我使用的简单代码:

Set WinHttpReq = CreateObject("MSXML2.ServerXMLHTTP")
WinHttpReq.Open "GET", "Initial_URL.com", False
WinHttpReq.Send

下面是获得的 http 标头:

https://Initial_URL.com
Host: Initial_URL.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
GET: HTTP/1.1 302 Moved Temporarily
content-length: 2361
content-type: text/html
date: Wed, 09 Jan 2019 19:46:06 GMT
location: Redirected_to.com
Set-Cookie: PD-S-SESSION-ID=1_2_0_ud2NmPnh++62VQCPVkwxb8xp0wuMBDhqmfZiqltbPrgksUAf; 

标头的第一部分是 HTTP 标头发送到服务器的内容,然后是来自服务器的答案。

我需要获取其中一些字段(例如位置,设置cookie)。我已经解析了WinHttpReq.ResponseText,但它是纯HTTP代码,根本没有标头。有什么指导吗?

如果需要标头信息,可以从 getAllResponseHeaders 方法获取。

这是一个小演示:

Sub GetHeaders()
    Dim headers As String
    
    With CreateObject("MSXML2.ServerXMLHTTP")
        .Open "GET", "https://stackoverflow.com/questions/54118535/excel-vba-how-to-scrape-the-http-header-webserver-answer-redirect-url"
        .send
        headers = .getAllResponseHeaders
        Debug.Print headers
    End With
End Sub

最新更新