Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving multiple cookies in vbscript

I think this might be an easy question, but I'm just a bit stuck.

I'm using this vbscript function to open a webpage and get the cookie.

Function Fetch(URL)

   Set WshShell = CreateObject("WScript.Shell")
   Set http = CreateObject("Microsoft.XmlHttp")
    http.open "", URL, FALSE
    Fetch = http.getResponseHeader("Set-Cookie")
   set WshShell = nothing
   set http = nothing  

End Function

It works fine for returning one cookie, but I've encountered a page that creates two cookies and I need them both. When I use this, it just returns the first cookie. How do I return both the cookies?

Thanks very much

like image 397
Simon Avatar asked Jul 05 '26 08:07

Simon


1 Answers

Should write your own:

Option Explicit

Function Fetch(ByVal URL, ByVal sHdrName)
    Dim http
    Set http = CreateObject("Microsoft.XmlHttp")
        http.open "GET", URL, False
        http.Send
        Fetch = getHeaders(http, sHdrName)
    Set http = Nothing  
End Function

Function getHeaders(oReq, sHdrName)
    Dim tHdrName : tHdrName = Trim(sHdrName) & ": "
    Dim tArr : tArr = Split(oReq.getAllResponseHeaders(), vbCrLf)
    tArr = Filter(tArr, tHdrName, True, vbTextCompare)
    Dim i
    For i = 0 To UBound(tArr)
        tArr(i) = Mid(tArr(i), Len(tHdrName) + 1, Len(tArr(i)))
    Next
    getHeaders = tArr 'Returns Array
End Function

'Iterate & Fetch
Dim iHdrVal
For Each iHdrVal In Fetch("http://a.url", "Set-Cookie")
    WScript.Echo iHdrVal
Next
like image 61
Kul-Tigin Avatar answered Jul 07 '26 22:07

Kul-Tigin