开发者

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开发者_JAVA技巧 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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜