Android Question OkHttp GetRequest.RemoveHeaders gives null pointer exception

Martin Larsen

Active Member
Licensed User
Longtime User
I need to to remove cookies before sending a request with OkHttp:

B4X:
Dim job1 As HttpJob
job1.Initialize("Job1", Me)
job1.GetRequest.RemoveHeaders("Cookie")
job1.Download(url)

But I get a null pointer exception when trying to remove the cookie header at line 3.

OkHttp and OkHttpUtils2 are included.

What could be wrong?
 

Martin Larsen

Active Member
Licensed User
Longtime User
and make sure you do it after the .download call, not before.

Wow, that is confusing! I searched and found some info here: https://www.b4x.com/android/forum/threads/set-http-request-header-with-httputils2.39413/

It is very counter intuitive. But now I know :)

However, neither RemoveHeaders("Cookie") nor SetHeader("Cookie","") works if there already is a cookie. If a previous response within the same job has returned a cookie, these methods are ignored totally. If there wasn't a previous cookie, I can create a new one.

Why is that?
 
Upvote 0

Martin Larsen

Active Member
Licensed User
Longtime User
Hmm, that doesn't work either.

In this post Erel has this function:

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

What is hc? That variable is undefined.

If I call this function I get a null pointer exception at line r.Target = r.GetField("client").

Anyway, why doesn't it work with removeHeaders and setHeader? This looks like a bug in the library?
 
Upvote 0

Martin Larsen

Active Member
Licensed User
Longtime User
1. Questions should never be posted in the libraries forum.

Sorry I didn't know that. I actually posted it there because it was a question about a library.

2. You should call Job.Download and then Job.GetRequest.RemoveHeader.

Yes, sorex already told me that.

But it still does not work. Cookies that are set by the remote host are not affected. Seems like a bug in the library? It's the same with both http and okhttp.
 
Upvote 0

Martin Larsen

Active Member
Licensed User
Longtime User
#13:

Is it a "feature" of the underlying java lib?

There is actually a difference between http and okhttp here. In okhttp, if you add a cookie and there already are cookies, your cookie is completely ignored. In http you will end up with two cookie headers being send to the server.
 
Upvote 0
Top