我在浏览器中安装了一个加载项,它使我可以查看从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标头发送到服务器的内容,然后是服务器的答案。
我需要获取其中一些字段(例如location,Set-Cookie)。 我已经解析了WinHttpReq.ResponseText,但这是纯HTTP代码,根本没有标题。 有指导吗?
答案 0 :(得分:1)
If you want the header information, you can get that from the getAllResponseHeaders
method.
Here is a small demo:
Sub GetHeaders()
Dim headers As String
With CreateObject("MSXML2.ServerXMLHTTP")
.Open "GET", "https://stackoverflow.com/questions/54118535/excel-vba-how-to-scrap-the-http-header-webserver-answer-redirect-url"
.send
headers = .getAllResponseHeaders
Debug.Print headers
End With
End Sub