• 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.

Copy cookie from request headers

shahin

Active Member
If one cookie is good to go then this approach is alright:
Code:
Sub GetCookie()
    Dim strCookie As Variant

    With CreateObject("WinHttp.WinHttpRequest.5.1")
        .Open "GET", "http://162.217.184.82/i2/default.aspx", False
        .setRequestHeader "User-Agent", "Mozilla/5.0"
        .send
        strCookie = .getAllResponseHeaders
        strCookie = Split(Split(strCookie, "Cookie:")(1), ";")(0)
        MsgBox Trim(strCookie)
    End With
End Sub
 

YasserKhalil

Well-Known Member
Hello
Do you mean something like that
Code:
Sub GetCookie()
    Dim strCookie  As Variant
    Dim x          As Variant
    Dim e          As Variant
    Dim i          As Long
    Dim r          As Long
    Dim c          As Long

    With CreateObject("WinHttp.WinHttpRequest.5.1")
        .Open "GET", "http://162.217.184.82/i2/default.aspx", False
        .setRequestHeader "User-Agent", "Mozilla/5.0"
        .send

        strCookie = .getAllResponseHeaders
        strCookie = Split(strCookie, "Cookie:")
        strCookie = Split(Trim(strCookie(UBound(strCookie))), vbLf)

        For i = LBound(strCookie) To UBound(strCookie)
            If Not IsEmpty(strCookie(i)) Then
                r = r + 1: c = 0
                Cells(r, 1).Value = strCookie(i)

                If InStr(strCookie(i), ";") > 0 Then
                    x = Split(strCookie(i), ";")
                    For Each e In x
                        Cells(r, c + 2).Value = e
                        c = c + 1
                    Next e
                End If
            End If
        Next i
    End With
End Sub
 

shahin

Active Member
The problem with your approach is it will give only one cookie and the rest are different things.
 

YasserKhalil

Well-Known Member
You have to have a look at the successful UDF as there are different stuff at this part .. so there are different result
Code:
Open "GET", "http://162.217.184.82/i2/default.aspx", False
        .setRequestHeader "User-Agent", "Mozilla/5.0"
        .send
I have worked just on your code .. and thought you need to separate its parts
 

Chihiro

Excel Ninja
So what is your question really?

If there's only one cookie header, below is better way to grab it than grabbing all of the headers and manipulating it.
Code:
 strCookie = .getResponseHeader("Set-Cookie")
I only used .getAllResponseHeaders to grab all headers (cookies included), since the site in question had 2 Set-Cookie header.
 

shahin

Active Member
@sir Chihiro, the ones I wished to get are within "Request header" category but the one you have demonstrated is by using ".getResponseHeader("Set-Cookie")" is within "Response Header" category that is why I got confused. I think the value of the two categories are not identical. I would not have any question If I could use ".getRequestHeader("Cookie")" but no such method exists. However, the bottom line is I wanted to use all the cookies available within "Cookie". Not a single one.

If I consider the below one:
Code:
Cookie:AspxAutoDetectCookieSupport=1; BIGipServerpool_rod_public_website=1376261292.20480.0000; ASP.NET_SessionId=0duv4dnowcc4lvjuah3yayqj
My expected output was (derived from the above one):
Code:
.setRequestHeader "Cookie", "AspxAutoDetectCookieSupport=1"
.setRequestHeader "Cookie", "BIGipServerpool_rod_public_website=1376261292.20480.0000"
.setRequestHeader "Cookie", "ASP.NET_SessionId=0duv4dnowcc4lvjuah3yayqj"
In fact I wanted to use all of them. That's it.
 

Chihiro

Excel Ninja
This...
Code:
"AspxAutoDetectCookieSupport=1"
Isn't part of cookie.

See below full response headers returned.
Code:
Cache-Control: private
Date: Fri, 09 Feb 2018 13:59:35 GMT
Content-Length: 81979
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.5
Set-Cookie: ASP.NET_SessionId=nbgiim55xq23mkr2i3d1wj55; path=/; HttpOnly
Set-Cookie: BIGipServerpool_rod_public_website=1376261292.20480.0000; path=/; Httponly
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
X-UA-Compatible: IE=EmulateIE9
 

YasserKhalil

Well-Known Member
Hello Mr. Chihiro
I have used chrome to have a look .. but it seems different for me ..as cookie in request headers not in response headers .. is there a difference?
 

Attachments

shahin

Active Member
One last thing on this to know sir: why my response header doesn't contain the "Set-Cookie:" parameter like Yasser and unlike what you have shown? I'm using chrome dev tools.
 

shahin

Active Member
No way. I'm also using the latest version of chrome:
"Google Chrome is up to date'
"Version 64.0.3282.140 (Official Build) (32-bit)"
Can you see the "Set-Cookie:" parameter @YasserKhalil?
 

shahin

Active Member
I noticed it in the first place and that is why I erected such question. No "set-Cookie:" parameter is there.
 

Chihiro

Excel Ninja
... Cookie expires with time. So, if the cookie is already set on a site. There is no need to set one until it expires. Makes sense?
 

shahin

Active Member
Hi @YasserKhalil, are you around? Which excel version are you using? If you are using excel 2019, I wish to know what happens when you click on the button in the following excel workbook? I'm using excel 2016 and I get "19" in a message box when I run this macro.
 

Attachments

YasserKhalil

Well-Known Member
I have faced this error before
The solution is to comment these two lines
Code:
Dim HtmlDoc As HTMLDocument
Set HtmlDoc = New HTMLDocument
And use these two lines instead
Code:
Dim HtmlDoc As Object
Set HtmlDoc = CreateObject("HtmlFile")
 

shahin

Active Member
If I use this `CreateObject("HtmlFile")`, I won't be able to use `.querySelector()` and I have to rely on tag name, id e.t.c. However, what did you get after pressing that button?
 
Top