• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Desired cookie value not returned in response

arpit singh

New Member
I have written a VBA code to get-- option-chain-indices?symbol=nifty file & type=json data through web connection.
I want to get both the request & response cookies from the website . But the issue is my code is returning some other file cookie value not from this file-option-chain-indices?symbol=nifty.......My code is as below-

>>> as You have already noticed <<<
>>> use code - tags <<<

Code:
Sub GetCookie3()

    Dim web, strUrl, strCookie, strCookie1, strUrlrefer
    On Error Resume Next
   
    strUrl = "https://www.nseindia.com/api/option-chain-indices?symbol=" & WorksheetFunction.EncodeURL("NIFTY")
    strUrlrefer = "https://www.nseindia.com/option-chain?symbolCode=-10006&symbol=NIFTY&symbol=NIFTY&instrument=-&date=-&segmentLink=17&symbolCount=2&segmentLink=17"
   
    Set web = CreateObject("MSXML2.ServerXMLHTTP")

    web.Open "GET", strUrl, False
   
    web.SetRequestHeader "Host", "www.nseindia.com"
    web.SetRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:88.0) Gecko/20100101 Firefox/88.0)"
    web.SetRequestHeader "Accept", "*/*"
    web.SetRequestHeader "Connection", "keep-alive"
    web.SetRequestHeader "Accept-Language", "en-US,en;q=0.5"
    web.SetRequestHeader "Accept-Encoding", "gzip, deflate, br"
    web.SetRequestHeader "Referer", strUrlrefer
    web.SetRequestHeader "Connection", "keep-alive"
   
    web.Send
   
   
    If web.Status <> 200 Then Debug.Print web.Status & " website status: " & web.StatusText
    Debug.Print web.Status & "************ website status:********** " & web.StatusText
   
      
       strCookie = web.GetAllResponseHeaders
        strCookie = Split(strCookie, vbCrLf)
        strCookie1 = strCookie(5) & vbCrLf & strCookie(6)
        Debug.Print "___COOKIE_____" & strCookie(10)
        'i = 0
        'For Each Item In strCookie
        'Debug.Print "At position: " & i & "__Is_" & strCookie(i)
        'i = i + 1
        'Next
       
End Sub
 
Last edited by a moderator:
You should really look at the content of strCookie right after web.GetAllResponseHeaders line.
You will see following is returned.

Code:
Connection: keep-alive
Date: Thu, 06 May 2021 17:59:35 GMT
Content-Length: 272
Content-Type: text/html; charset=utf-8
Set-Cookie: AKA_A2=A; expires=Thu, 06-May-2021 18:59:35 GMT; path=/; domain=nseindia.com; secure; HttpOnly
Set-Cookie: ak_bmsc=ADD721C8948DDB8F60D3E08DD09F7E8648F62BDE48290000872E946025C6546C~plClV3oXt2XQ75ymfcjPEJyBwaCVPk23oLkzEkoaH+i59mhf7QFeDX+Rxk3o/vGJEkqHXb0RJy2wVI2zw2oWjrMZzzIgoCcDVhHMi0d+VPN3P0zLNPnAzSHgUZm9no01bb5Gw9M/fb4IhQOdxuSqnz7D4W2IB1DIKMkW+FlLxBW1+PlH8FQMvaEx4g+MIkyNEkwbgISLWWHMAdAWKgzgJc/Llc2OGvPfh5f9i/Nw0wxUk=; expires=Thu, 06 May 2021 19:59:35 GMT; max-age=7200; path=/; domain=.nseindia.com; HttpOnly
X-DNS-Prefetch-Control: off
X-Frame-Options: SAMEORIGIN
Strict-Transport-Security: max-age=15552000; includeSubDomains
X-Download-Options: noopen
X-Content-Type-Options: nosniff
X-XSS-Protection: 0
Access-Control-Allow-Origin: "nseindia.com"
Access-Control-Allow-Methods: GET,POST
Access-Control-Allow-Headers: Content-Type
Server-Timing: cdn-cache; desc=REVALIDATE
Server-Timing: edge; dur=211
Server-Timing: origin; dur=124

So you need 5th and 6th element of 0 based array. You need.
Code:
strCookie1 = strCookie(4) & vbCrLf & strCookie(5)

See below code to get both "Set-Cookie" header.
Code:
Function GetCookie(strUrl)
    With CreateObject("WinHttp.WinHttpRequest.5.1")
        .Open "GET", strUrl, False
        .SetRequestHeader "REFERER", strUrl
        .SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
        .SetRequestHeader "Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
        .SetRequestHeader "Accept-Language", "en-us,en;q=0.5"
        .SetRequestHeader "Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
        .Send
        strCookie = .getAllResponseHeaders
        strCookie = Split(strCookie, vbCrLf)
        GetCookie = strCookie(4) & vbCrLf & strCookie(5)
    End With
End Function

Sub Demo()
    Debug.Print GetCookie("https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY")
End Sub

EDIT: Alternately, you can use below for more dynamic way to retrieve "Set-Cookie" headers.
Code:
        strCookie = Filter(Split(strCookie, vbCrLf), "Set-Cookie")
        GetCookie = Join(strCookie, vbCrLf)
 
Oh by the way, request cookie is generated from browser using page state etc.

So you will need another method. My previous post is for scraping response cookie.
 
Code is returning an error message---- Run time error '-2147012739(80072f7d)-
An error occurred in the secure channel support....


I am a new to VBA so kindly help me out with the function to get request cookie from browser using page state
 
Page state is near impossible to replicate/reproduce using code alone.
You are better off piloting browser via code. Be it IE11, or Chrome etc via Selenium wrapper.

FYI - When I was testing the site, I found it to be very unreliable in terms of response. Sometimes it returns json response, other times it returned resource not found.
 
Back
Top