Android Question Manage cookies with HttpJob

sinner181

Member
Hi all,
I need to manage cookies in use for httpjob, I explain below:

A website set a cookie with set-cookie in response, but I need to clear ONLY a single value stored
Set-cookie : a=3689212956
Set-cookie : b=sdghfukgkqwsbh
Set-cookie : c=765wdf7d6s

I need to delete only B for example and leave A and C, is there a possibility?

Sometimes A B and C are store in same row, like
Set-cookie : a=3689212956 expire etc; b=sdghfukgkqwsbh expire etc; c=765wdf7d6s expire etc;

A list of this? A Map? A simple cookiemanager can do this work but I've see only for webview (I think).

Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Code to clear all cookies: https://www.b4x.com/android/forum/threads/httputils-cookie-store.113930/post-711519
That is B4J code. You need to remove this in B4A:
B4X:
   If HttpUtils2Service.hc.IsInitialized = False Then
        HttpUtils2Service.Service_Create
    End If

B4X:
Dim Store as JavaObject = CookieManager.RunMethodJO("getCookieStore", Null)
CookieStore API: https://docs.oracle.com/javase/7/docs/api/java/net/CookieStore.html

You need to first call getURIs to get a list with the URI objects and then call get to get a list with the cookies associated with each URI. This will allow you to call remove with the URI and cookie.
 
Upvote 0

sinner181

Member
I understand, but I don't know how to write it exactly.

I have understand how implement getCookies() and removeAll() from cookiestore without any additionals parameters, but in "add", "remove" and "get" I'm blocked 🥶🥶

B4X:
    Dim jo As JavaObject = HttpUtils2Service.hc
    Dim client As JavaObject = jo.GetField("client")
    Dim r As Reflector
    r.Target = client.RunMethod("cookieJar", Null)
    Dim CookieManager As JavaObject = r.GetField("cookieHandler")
    Dim cookie As String
    Dim cookieStore As JavaObject=CookieManager.RunMethodJO("getCookieStore",Null)
    cookie=""
    Dim s As List=cookieStore.RunMethod("getURIs", Null)
    If s.Size>0 Then
        For i=0 To s.Size-1
            Dim aURI As String=s.Get(i)
            Log(aURI)
           
            'Get Cookie URI list......how implement?
            Dim CookieInside As JavaObject=cookieStore.RunMethod("get", Null) ???? not work obviosly and error "GET NOT MATCHED" (the same error for add and remove)        
            Dim CookieList As List=s.Get(i) ??? no work 

            If CookieList.Size>0 Then
                For i=0 To CookieList.Size-1
                    Dim aCookie As String=CookieList.Get(i)
                    Log(aCookie)
                Next
            End If
        Next
    
    End If

Can you explain me how I can implement parameters?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Please don't bump your questions.

B4X:
Dim jo As JavaObject = HttpUtils2Service.hc
Dim client As JavaObject = jo.GetField("client")
Dim r As Reflector
r.Target = client.RunMethod("cookieJar", Null)
Dim CookieManager As JavaObject = r.GetField("cookieHandler")
Dim cookieStore As JavaObject= CookieManager.RunMethodJO("getCookieStore",Null)
Dim uris As List = cookieStore.RunMethod("getURIs", Null)
For Each uri As JavaObject In uris
    Dim UriAsString As String = uri.RunMethod("toString", Null)
    Log(UriAsString)
    If UriAsString = "http://www.google.com" Then 'cookies with this URI will be removed
        Dim cookies As List = cookieStore.RunMethod("get", Array(uri))
        Log(cookies)
        For Each cookie As JavaObject In cookies
            cookieStore.RunMethod("remove", Array(uri, cookie))
        Next
    End If
Next

Don't forget to add the HU2_PUBLIC conditional symbol.
 
Upvote 0

sinner181

Member
" Please don't bump your questions. "
Sorry Erel 🤦‍♂️
They are almost there, but I can't find a library for creating the cookie to be inserted.
For uri I found the correct library, but to pass the value of the cookie?

B4X:
            Dim jo As JavaObject = HttpUtils2Service.hc
            Dim client As JavaObject = jo.GetField("client")
            Dim r As Reflector
            r.Target = client.RunMethod("cookieJar", Null)
            Dim CookieManager As JavaObject = r.GetField("cookieHandler")
            Dim cookieStore As JavaObject= CookieManager.RunMethodJO("getCookieStore",Null)
                    
            
            Dim uri As Uri
            uri.Parse("https://www.mywebsite.com")   'is correct?
        
            Dim NewCookie as JavaObject 'How can I assign value?

            cookieStore.RunMethod( "add" , Array(uri, NewCookie ) )

Thanks.
 
Upvote 0

sinner181

Member
This my code :

B4X:
            Dim jo As JavaObject = HttpUtils2Service.hc
            Dim client As JavaObject = jo.GetField("client")
            Dim r As Reflector
            r.Target = client.RunMethod("cookieJar", Null)
            Dim CookieManager As JavaObject = r.GetField("cookieHandler")
            Dim cookieStore As JavaObject= CookieManager.RunMethodJO("getCookieStore",Null)
                    
            
            Dim uri As Uri
            uri.Parse("https://www.mywebsite.com")
        
            Dim NewCookie As JavaObject
            NewCookie.InitializeNewInstance("java.net.HttpCookie",Array("s","123456789"))

            Log(NewCookie.RunMethod("getName",Null))
            Log(NewCookie.RunMethod("getValue",Null))
            
            cookieStore.RunMethod("add", Array(uri, NewCookie ))

in LOG I see correctly name and value assigned, i think that cookie is correct, but when arrive to "add" I receive this message :
*** java.lang.RuntimeException: Method: add not matched. ***

Where am i wrong?
 
Upvote 0

sinner181

Member
I've used a website test:

First click on LOGIN button, after login click to SAVE button (to store cookies into sql).

After restart, click on LOAD button, my problem is here.

Thanks.
 

Attachments

  • TestCookie.zip
    10.8 KB · Views: 215
Upvote 0
Top