B4J Question Clear Cookies - jServer5

betaoaf

New Member
Hello, I was using jServer4, and the ClearCookies function was working perfectly. After updating to jServer5, however, the function no longer works. Does anyone have any suggestions on how to fix it?

The error is: java.lang.NoSuchFieldException: cookieHandler

[CODE lang="b4x" title="ClearCookis - jServer4"]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[/CODE]
 
Solution
Try using this code. it is also compatible with JServer4

[CODE lang="b4x" title="Add Jserver5 support"]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 {
try {
Field f =...
Try using this code. it is also compatible with JServer4

[CODE lang="b4x" title="Add Jserver5 support"]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 {
try {
Field f = o.getClass().getDeclaredField("$$delegate_0");
f.setAccessible(true);
o = f.get(o);
} catch (NoSuchFieldException e) {
// Do nothing
}
Field f = o.getClass().getDeclaredField("cookieHandler");
f.setAccessible(true);
return f.get(o);
}
#End If[/CODE]
 
Last edited:
Upvote 0
Solution
Back
Top