Abraham!@#
New Member
Hi All,
I'm using winHTTP to connect to https://www.nseindia.com/ and get the cookies from the response header.
The code works fine in my personal network. But on Office VPN, it gets timed out.
I saw from https://chandoo.org/forum/threads/unable-to-set-proxy-for-winhttp-winhttprequest-5-1-object.38437/, that proxy setting are to be enabled.
Could you please tell me where I can find these proxy credentials that I need to set and how to set it in my code?
Here is my VBA code below. I have also commented the line on which I am getting the error.
I'm using winHTTP to connect to https://www.nseindia.com/ and get the cookies from the response header.
The code works fine in my personal network. But on Office VPN, it gets timed out.
I saw from https://chandoo.org/forum/threads/unable-to-set-proxy-for-winhttp-winhttprequest-5-1-object.38437/, that proxy setting are to be enabled.
Could you please tell me where I can find these proxy credentials that I need to set and how to set it in my code?
Here is my VBA code below. I have also commented the line on which I am getting the error.
Code:
Sub GetNSECookies()
Dim website As String
Dim cookieValues As String
Dim website2 As String
Dim cookieValuesFinal As String
' First call
website = "https://www.nseindia.com/"
cookieValues = NSEDataCall(website, cookieValues) ' function call
Debug.Print (cookieValues)
' Second call for sv_bm cookie
website = "https://www.nseindia.com/market-data/securities-lending-and-borrowing"
cookieValues = NSEDataCall(website, cookieValues) ' function call
cookieValues = Replace(cookieValues, vbCr, " ")
cookieValues = Replace(cookieValues, vbLf, " ")
cookieValues = Replace(cookieValues, vbCrLf, " ")
Debug.Print (cookieValues)
End Sub
Public Function NSEDataCall(website, setCookies) As String
Dim XMLHTTP As WinHttp.WinHttpRequest
'Initialize XMLHttp Object
'Use the best/proper XMLHttp object available on your system
' Set XMLHTTP = New WinHttp.WinHttpRequest
Set XMLHTTP = CreateObject("WinHttp.WinHttpRequest.5.1") ' needs Microsoft WinHTTP Services 5.1 reference
XMLHTTP.Option(6) = False ' WinHttpRequestOption_EnableRedirects=6
XMLHTTP.Option(4) = &H3300
XMLHTTP.Open "GET", website, False
' Set headers.
XMLHTTP.setRequestHeader "REFERER", website
XMLHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
XMLHTTP.setRequestHeader "Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
XMLHTTP.setRequestHeader "Accept-Language", "en-us,en;q=0.5"
XMLHTTP.setRequestHeader "Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
' Set cookie value - used for second call
If Len(setCookies) > 0 Then
XMLHTTP.setRequestHeader "Connection", "keep-alive"
XMLHTTP.setRequestHeader "cookie", setCookies
End If
Debug.Print website
XMLHTTP.send ' HERE is where the error is happening on remote
XMLHTTP.WaitForResponse
Debug.Print website 'checking if this will print
If Len(setCookies) > 0 Then
' Second call comes here - Get response headers
response = XMLHTTP.getAllResponseHeaders
' Debug.Print response
' Split by new line
responseArray = Split(response, vbCrLf)
' Debug.Print responseArray(7)
' Helps to identify dataType - output comes as code numbers
' MsgBox (VarType(Trim(Split(Split(responseArray(5), ";")(0), ":")(1)) & "; " & Trim(Split(Split(responseArray(6), ";")(0), ":")(1))
' Return the sv_bm cookie in response array at indices 7 (indices start from 0)
NSEDataCall = setCookies & "; " & Trim(Split(Split(responseArray(7), ";")(0), ":")(1))
Else
' First call comes here - Get response headers
response = XMLHTTP.getAllResponseHeaders
' Debug.Print response
' Split by new line
responseArray = Split(response, vbCrLf)
' Return the cookies in response array from indices 5 to 9
NSEDataCall = Trim(Split(Split(responseArray(5), ";")(0), ":")(1)) & "; " & Trim(Split(Split(responseArray(6), ";")(0), ":")(1)) & "; " & Trim(Split(Split(responseArray(7), ";")(0), ":")(1)) & "; " & Trim(Split(Split(responseArray(8), ";")(0), ":")(1)) & "; " & Trim(Split(Split(responseArray(9), ";")(0), ":")(1))
'Debug.Print (responseArray(5) + responseArray(6))
End If
End Function