Android Question Clear cookies from HttpJob

Martin Larsen

Active Member
Licensed User
When you make multiple requests with HttpJob, cookies returned from the server are kept across the requests. This might be useful in some cases, but in other cases it is a problem as the web server will not send a new cookie if it is already present in the request.

In the following example a click on the button makes a request to http://marlar.dk/cookie.php which will set a cookie and then return the full cookie string.

At the first click no cookies are returned as there are none. But the script sets the cookie mycookie.Then, at subsequent requests, mycookie is sent to the server along with the request and returned by the script.

B4A:
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("http://marlar.dk/cookie.php")
    j.GetRequest.RemoveHeaders("HTTP_COOKIE")
    Wait For (j) JobDone(j As HttpJob)
    EditText1.Text = j.GetString
    If EditText1.Text = "" Then EditText1.Text = "<none>"
    j.Release

cookie.php:
setcookie("mycookie", "B4X");
echo $_SERVER["HTTP_COOKIE"];

Adding j.GetRequest.RemoveHeaders("HTTP_COOKIE") as shown in the example doesn't work.

I have also looked at this post but I don't know if that applies here. Besides, what is hc in the code below?

B4X:
Sub ClearCookies
   Dim r As Reflector
   r.Target = hc
   r.Target = r.GetField("client")
   r.Target = r.RunMethod("getCookieStore")
   r.RunMethod("clear")
End Sub

How can I clear the cookies from HttpJob or simply make a new fresh request with no "memories" of the previous?

Example attached.
 

Attachments

  • cookietest.zip
    9.4 KB · Views: 127

KZero

Active Member
Licensed User
you can use OKHttp (while it's not recommended, I don't know why 😅)

B4X:
Sub Process_Globals
    Dim hc As OkHttpClient
End Sub
Sub Globals
    Private EditText1 As EditText
    Dim OPS As OutputStream
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    hc.Initialize("HC")
End Sub

Sub Response_StreamFinish(Success As Boolean,TaskId As Int)
    EditText1.Text = BytesToString(OPS.ToBytesArray, 0, OPS.ToBytesArray.Length, "UTF-8")
    If EditText1.Text = "" Then EditText1.Text = "<none>"
End Sub
Private Sub HC_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
    OPS.InitializeToBytesArray(0)
    Response.GetAsynchronously("Response", OPS, True, TaskId)
End Sub
Sub Button1_Click
Dim hr As OkHttpRequest
    hr.InitializeGet("http://marlar.dk/cookie.php")
    hc.Execute(hr,1)
End Sub

Sub Clear
hc.Initialize("HC")
End Sub
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
Many mistakes in the above code and it also has nothing to do with clearing the cookies.

The correct code is here: https://www.b4x.com/android/forum/threads/httputils-cookie-store.113930/post-711519
You will need to remove this section:
B4X:
 If HttpUtils2Service.hc.IsInitialized = False Then
        HttpUtils2Service.Service_Create
    End If
However make sure that HttpUtils2Service was started before running this code (it will happen as you will call it after making a request).
 
Upvote 0

Martin Larsen

Active Member
Licensed User
Thanks, it works.

However make sure that HttpUtils2Service was started before running this code (it will happen as you will call it after making a request).

How do I make sure the the service was started? (I don't want to clear cookies after the request as it is only sometimes the cookies should be cleared)

Instead I have added this line:

B4X:
If HttpUtils2Service.hc.IsInitialized = False Then Return

This check ensures it works even if ClearCookies is called before the first request. There aren't any cookies in that case anyway :)

A few questions to make me understand better what is going on behind the scenes:

In the referenced code, what is HttpUtils2Service.hc? What is the hc property?

Why is the symbol HU2_PUBLIC in build configuration needed? What does it control or trigger?

If I declared a completely new variable like Dim jnew As HttpJob, would it still carry the cookies from the other job? Is there actually only one underlying java httpjob?
 
Last edited:
Upvote 0

drgottjr

Well-Known Member
Licensed User
hc is the so-called okhttpclientwrapper, the lower level workhorse. the job triggers the service which sets the rest in motion. setting HU2_PUBLIC allows hc to be seen by b4a. the cookies are kept in a map and built from scratch for each request (the url in a given cookie having to match the url in the request, plus not having expired). plowing through the code is a slow go, but i don't think the job affects the cookies. it happens at a lower level and is controlled by the request (and the url). it's easy enough for you to prove it one way or the other, however.
 
Upvote 0

drgottjr

Well-Known Member
Licensed User
you can search the forum for conditional compiler options (you can make up your own.) "#if java" is an example. HU2_PUBLIC appears in the httputils2service class:
B4X:
#if HU2_PUBLIC
    #if B4A or B4J
        Public hc As OkHttpClient
    #else
        Public hc As HttpClient
    #end if
#else
    #if B4A or B4J
        Private hc As OkHttpClient
    #else
        Private hc As HttpClient
    #end if
#End If

nuggets aplenty
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
How do I make sure the the service was started? (I don't want to clear cookies after the request as it is only sometimes the cookies should be cleared)
You surely don't need to clear cookies before the first request. There won't be any. Once you made the first request hc will be initialized.

Is there a list of symbols recognized by B4X?
This question is not accurate. You should ask about OkHttpUtils2 specifically. The answer is here: [B4X] OkHttpUtils2 / iHttpUtils2 and accept all option
 
Upvote 0

Martin Larsen

Active Member
Licensed User
You surely don't need to clear cookies before the first request. There won't be any. Once you made the first request hc will be initialized.

Yes, that is actually also what I wrote a few lines after.

This question is not accurate. You should ask about OkHttpUtils2 specifically.

You are right. The reason I asked was because I searched all the files and folders in B4A (libs and shared modules etc) for the string HU2_PUBLIC without finding anything, so I thought it was a core thing. It must be packed in the java binary somewhere since I can't find it.
 
Upvote 0
Top