Link to home
Create AccountLog in
Avatar of kanishkpanwar
kanishkpanwar

asked on

Cookie HEX value

I am reading a cookie using GetCookie()
it gives me an output string with "%XX".  i.e Hex values.
how do i get actual string ?
i need a working code sample.
Avatar of learning_t0_pr0gram
learning_t0_pr0gram

Public Function CookieString(ByVal Cookie As String) As String
    Dim Tmp() As String, x As Long, y As Long

    Tmp() = Split(Cookie, "%")

    For x = 1 To UBound(Tmp)
        y = "&H" & Tmp(x)
        CookieString = CookieString & Chr(y)
    Next
End Function

Call it like:

MsgBox CookieString("%74%65%73%74")
Avatar of kanishkpanwar

ASKER

it gives a "type mismatch error" at
y = "&H" & Tmp(x)

i am setting a cookie from  http://www.javascripter.net/faq/settinga.htm

name: test
value: 5
here'z the CODE
----------------------------------

Option Explicit
' No more data is available.
      Const ERROR_NO_MORE_ITEMS = 259

      ' The data area passed to a system call is too small.
      Const ERROR_INSUFFICIENT_BUFFER = 122


Private Declare Function URLDownloadToFile Lib "urlmon" _
   Alias "URLDownloadToFileA" _
  (ByVal pCaller As Long, _
   ByVal szURL As String, _
   ByVal szFileName As String, _
   ByVal dwReserved As Long, _
   ByVal lpfnCB As Long) As Long
   
Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
   Alias "DeleteUrlCacheEntryA" _
  (ByVal lpszUrlName As String) As Long
   
Private Const ERROR_SUCCESS As Long = 0
Private Const BINDF_GETNEWESTVERSION As Long = &H10
Private Const INTERNET_FLAG_RELOAD As Long = &H80000000


      Private Declare Function InternetGetCookie Lib "Wininet.dll" _
       Alias "InternetGetCookieA" _
       (ByVal lpszUrlName As String, _
       ByVal lpszCookieName As String, _
       ByVal lpszCookieData As String, _
       lpdwSize As Long) As Boolean



Private Sub Form_Load()

Dim sCookieVal As String * 256
 Dim bRet As Boolean
 Dim st As String * 256
 
 bRet = InternetGetCookie("http://www.javascripter.net/faq/settinga.htm", _
 "num_downloads", sCookieVal, 4000)
  If bRet = False Then
    MsgBox "Failed to retrieve download information"
    Me.Hide
    Unload Me
     Else
 st = unescape(sCookieVal) 'get the value from st later.
 
 
  End If

End Sub




Function unescape(s As String) As String
 Dim Tmp() As String, x As Long, y As Long
Dim CookieString

    Tmp() = Split(s, "%")

    For x = 1 To UBound(Tmp)
        y = "&H" & Tmp(x)
        CookieString = CookieString & Chr(y)
    Next

unescape = CookieString
End Function
I get a "Failed to retrieve download information" so I can't try it out.  My code will turn a string that looks like:

%6F%75%74

That string becomes: out



Could you please show an example of the cookie?  Because I think what is happening is the cookie is not all hex values, but it has some text also.
learning_t0_pr0gram ,
You need to "set" your cookie first via the link. I believe InternetGetCookie() only retrieves the cookies that are saved on the hard-disk.

Here's an alternative approach that uses IE to obtain the cookie and uses it's JS support to unescape it.

Form1:
----------------
Option Explicit
Private Sub Form_Load()
    'Project -> References -> Microsoft HTML Object Library
    Dim objDoc1 As HTMLDocument, objDoc2 As HTMLDocument
    Set objDoc2 = New HTMLDocument
    Dim strCookie As String
    Set objDoc1 = objDoc2.createDocumentFromUrl("http://www.javascripter.net/faq/settinga.htm", vbNullString)
    Do Until objDoc1.readyState = "complete"
        DoEvents
    Loop
    'execute script within document to change document's title to unescaped cookie
    Call objDoc1.parentWindow.execScript("document.title=unescape(document.cookie);")
    'grab the title!
    strCookie = objDoc1.Title
    'you can parse it for a specific cookie..
    Call MsgBox(strCookie)
    'clean up.. possibly not required
    Set objDoc1 = Nothing
    Set objDoc2 = Nothing
End Sub


click on the link. set a cookie from that URL !!! this URL helps u set a cookie.
This works for me...

Dim sCookieVal As String * 256
Dim bRet As Boolean
Dim sCookiePairs() As String
Dim sCookie() As String
Dim i As Long
 
bRet = InternetGetCookie("http://www.javascripter.net/faq/settinga.htm", vbNullString, sCookieVal, Len(sCookieVal))

If bRet = False Then

    MsgBox "Failed to retrieve download information"
    'Me.Hide
    'Unload Me
   
Else
    'split the cookies into "name=value" strings
    sCookiePairs = Split(sCookieVal, "; ")
    For i = 0 To UBound(sCookiePairs)
        'split apart each name=value pair
        sCookie = Split(sCookiePairs(i), "=")
        MsgBox sCookie(0) & vbCrLf & sCookie(1)
    Next
 
End If

End Sub
what about the HEX values ?
There are no hex values in my string.  Here is what InternetGetCookie() returns:
userTestCookie=ahz_qbjaybnqf; cookieName=cookieValue; num_downloads=100
set value as "HELLOO !!! HEX IS OUT THERE ?!?!?"
i hope set the cookie from that link i have posted
ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
thanks !! Erick37 is the man !! \m/