B4J Question HttpUtils - Cookie Store?

Erel

B4X founder
Staff member
Licensed User
Longtime User
Cookies are managed internally and are kept by default.

Clear cookies:
1.
B4X:
Sub ClearCookies
    If HttpUtils2Service.hc.IsInitialized = False Then
        HttpUtils2Service.Service_Create
    End If
    Log(HttpUtils2Service.hc.IsInitialized)
    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")
    CookieManager.RunMethodJO("getCookieStore", Null).RunMethod("removeAll", Null)
End Sub

2. Add symbol to build configuration (ctrl + b): HU2_PUBLIC
 
Upvote 0

JMB_Downlaod

New Member
Hi,

thank you very much. I tried your code snippet, but I go the following error:

java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NoClassDefFoundError: javafx/beans/value/ChangeListener

Please note, that I use HttpUtils on a non UI app.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You are correct. The behavior depends on the Java version.

Solution without jReflection:
B4X:
Sub ClearCookies
    If HttpUtils2Service.hc.IsInitialized = False Then
        HttpUtils2Service.Service_Create
    End If
    Log(HttpUtils2Service.hc.IsInitialized)
    Dim jo As JavaObject = HttpUtils2Service.hc
    Dim client As JavaObject = jo.GetField("client")
    Dim jme As JavaObject = Me
    Dim handler As Object = jme.RunMethod("getCookieHandler", Array(client.RunMethod("cookieJar", Null)))
    Dim CookieManager As JavaObject = handler
    CookieManager.RunMethodJO("getCookieStore", Null).RunMethod("removeAll", Null)
End Sub


#if JAVA
import java.lang.reflect.*;
public static Object getCookieHandler(Object o) throws Exception {
    Field f = o.getClass().getDeclaredField("cookieHandler");
    f.setAccessible(true);
    return f.get(o);
}
#End If
 
Upvote 0
Top