MSXML2.XMLHTTP fails to send request cookie header

风流意气都作罢 提交于 2021-01-29 20:47:27

问题


I'm trying to create some automated web searches in this website using Excel VBA but in order to use it, you need to agree with it's terms and conditions. The website then stores your agreement in a cookie "agree=True".

But, while using MSXML2.XMLHTTP, I'm not able to set the cookie request header using setRequestHeader, so it redirects me to the usage agree page on every request. I know there's a bug using MSXML2 as stated here, but I don't see how I can use the workaround provided in the link with Excel. (Invoking setRequestHeader twice will not work).

This is the code I'm using:

Set objHTTP = CreateObject("MSXML2.XMLHTTP.6.0")

Dim CAGE As String

CAGE = "3K723" 'Could be any CAGE Code, using this one as example

url = "https://cage.dla.mil/Search/Results?q=" & CAGE & "&page=1"

objHTTP.Open "GET", url, False
objHTTP.SetRequestHeader "Cookie", "agree=True"
objHTTP.send

Cells(1, 1).Value = objHTTP.ResponseText

I know this problem is solved simply by using MSXML2.ServerXMLHTTP or WinHTTP.WinHTTPRequest.5.1 instead of MSXML2.XMLHTTP, but as the website uses HTTPS protocol and not HTTP, ServerXMLHTTTP and WinHTTP can't resolve it's address. So it returns me the following error:

"The server name of address could not be resolved."

Does anyone knows how can I properly set the Cookie header using MSXML2.XMLHTTP, or how to use WinHTTP with an HTTPS website?

来源:https://stackoverflow.com/questions/59645875/msxml2-xmlhttp-fails-to-send-request-cookie-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!