如何使用excel vba获取cookie信息 [英] How to get cookie information using excel vba

查看:166
本文介绍了如何使用excel vba获取cookie信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从网络下获取 cookie 信息 -->下面页面的 RequestHeader

解决方案

问题是 cookie 每 1 小时过期一次,因此我们必须刷新它们.

函数 GetCookie(strUrl)使用 CreateObject("WinHttp.WinHttpRequest.5.1").Open GET", strUrl, False.SetRequestHeader "REFERER", strUrl.SetRequestHeader用户代理"、Mozilla/4.0(兼容;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";.发送strCookie = .getAllResponseHeadersstrCookie = Split(strCookie, vbCrLf)GetCookie = Trim(Split(Split(strCookie(5), ";")(0), ":")(1)) &";"&Trim(Split(Split(strCookie(6), ";")(0), ":")(1))结束于结束函数子 get_cookies()Msgbox(GetCookie(https://www.nseindia.com/market-data/equity-derivatives-watch"))结束子

试试这个它会返回 nsit 和 nseappid

<代码>(如: - NSIT = ZK3-uf45niHVOsW_yaK2kIdWg; nseappid = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiONKJklmlmNIOJkjnkoivhvIiwiYXVkIjoiYXBpLm5zZSIsImlhdCI6MTVHJVHuihnjyuGyufhJNkoioxNjEzNjI3MjMyfQ.PDBf7k6fGsYicbM7sYeJrhE0OtGENf_e5YdRENkAHq0)

为我工作,我已经自动化了它.我们需要 cookie 的链接是 NSE 网站,而不是具体的罢工或脚本.

I am trying to get cookie information from under Network --> RequestHeader of below page https://www.nseindia.com/market-data/equity-derivatives-watch All I am getting from the below code is Response headers "set-cookie", but I need "cookie" information under request headers. Below is the code I've tried.

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)

        Debug.Print strCookie

'        strCookie = Split(strCookie, ";")(0)
'        GetCookie = strCookie
    End With
End Function



Sub Demo()
Debug.Print GetCookie("https://www.nseindia.com/market-data/equity-derivatives-watch")
End Sub

I need the below cookie information

解决方案

The problem is the cookies get expire every 1 hr so we have to refresh them.

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 = Trim(Split(Split(strCookie(5), ";")(0), ":")(1)) & "; " & Trim(Split(Split(strCookie(6), ";")(0), ":")(1))
End With
End Function

Sub get_cookies()
Msgbox(GetCookie("https://www.nseindia.com/market-data/equity-derivatives-watch"))
End Sub

Try this it will return nsit and nseappid

(Eg:- nsit=zk3-uf45niHVOsW_yaK2kIdWg; nseappid=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiONKJklmlmNIOJkjnkoivhvIiwiYXVkIjoiYXBpLm5zZSIsImlhdCI6MTVHJVHuihnjyuGyufhJNkoioxNjEzNjI3MjMyfQ.PDBf7k6fGsYicbM7sYeJrhE0OtGENf_e5YdRENkAHq0)

Worked for me and I have automated it. The link which we need the cookies for is the NSE site not the specific strike or script.

这篇关于如何使用excel vba获取cookie信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆