I'm trying to access https://www.nseindia.com/ to get the cookies in response headers. But using MSXML2.XMLHTTP returns the cookie value as empty string.
From, VBA Microsoft.XMLHTTP setRequestHeader not sending cookie - I tried using WinHTTP, which did not connect at all and kept timing out. Apparently that can only be used on HTTP requests. How can the same be done for a HTTPS request?
MSXML2.ServerXMLHTTP also does not seem to support HTTPS requests.
Please find the code snippets I used for the 2 methods below:
MSXML2.XMLHTTP
Sub Get_Web_Data()
Dim request As Object
Dim response As String
Dim website As String
' Website to go to.
website = "https://www.nseindia.com/"
' Create the object that will make the webpage request.
Set request = CreateObject("MSXML2.XMLHTTP")
' Where to go and how to go there.
request.Open "GET", website, False
' Set headers.
request.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
request.setRequestHeader "user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36"
request.setRequestHeader "accept-Encoding", "gzip , deflate"
request.setRequestHeader "accept-language", "en-US,en;q=0.9"
request.setRequestHeader "Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
request.setRequestHeader "Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
' Send the request for the webpage.
request.send
responseHeaders = request.getAllResponseHeaders
MsgBox responseHeaders
End Sub
winHTTP
Sub Get_Web_Data_Cookie()
Dim website As String
Dim cookieString As String
Dim XMLHTTP As WinHttp.WinHttpRequest
' Website to go to.
website = "https://www.nseindia.com/"
'Initialize XMLHttp Object
Set XMLHTTP = CreateObject("WinHttp.WinHttpRequest.5.1") ' needs Microsoft WinHTTP Services 5.1 reference
' XMLHTTP.Option(WinHttpRequestOption_EnableRedirects) = False ' WinHttpRequestOption_EnableRedirects=6
XMLHTTP.Open "GET", website, False
' Set headers.
XMLHTTP.setRequestHeader "user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36"
XMLHTTP.setRequestHeader "accept-encoding", "gzip , deflate"
XMLHTTP.setRequestHeader "accept-language", "en-US,en;q=0.9"
' Send the request for the webpage.
XMLHTTP.send
responseHeaders = XMLHTTP.getAllResponseHeaders
MsgBox responseHeaders
End Sub