Martin Larsen Active Member Licensed User Longtime User Mar 3, 2017 #1 I need to to remove cookies before sending a request with OkHttp: Code: Copy 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?
I need to to remove cookies before sending a request with OkHttp: Code: Copy 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 Mar 3, 2017 #3 Thanks, but it is not a webview. I just a small download job I need to do. Upvote 0
MarcoRome Expert Licensed User Longtime User Mar 3, 2017 #4 Try with: Code: Copy job.GetRequest.SetHeader("Cookie","") Upvote 0
S sorex Expert Licensed User Longtime User Mar 3, 2017 #5 and make sure you do it after the .download call, not before. Upvote 0
Martin Larsen Active Member Licensed User Longtime User Mar 4, 2017 #6 sorex said: and make sure you do it after the .download call, not before. Click to expand... 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
sorex said: and make sure you do it after the .download call, not before. Click to expand... 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?
I inakigarm Well-Known Member Licensed User Longtime User Mar 4, 2017 #7 Look at https://www.b4x.com/android/forum/threads/httputills2-clear-cookies.20053/#post-116289, seems you'll have to use the class HttpUtils2Service and not the okhttp lib (zip file in https://www.b4x.com/android/forum/threads/okhttp-replaces-the-http-library.54723/) Upvote 0
Look at https://www.b4x.com/android/forum/threads/httputills2-clear-cookies.20053/#post-116289, seems you'll have to use the class HttpUtils2Service and not the okhttp lib (zip file in https://www.b4x.com/android/forum/threads/okhttp-replaces-the-http-library.54723/)
Martin Larsen Active Member Licensed User Longtime User Mar 4, 2017 #8 Hmm, that doesn't work either. In this post Erel has this function: Code: Copy 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
Hmm, that doesn't work either. In this post Erel has this function: Code: Copy 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?
I inakigarm Well-Known Member Licensed User Longtime User Mar 4, 2017 #9 In B4A IDE --> Project you'll have to Add Existing Modules --> HttpUtils2Service.bas and HttpJob.bas (zip file in https://www.b4x.com/android/forum/threads/okhttp-replaces-the-http-library.54723/) --> don't add the okhttp library In the Process_globals of HttpUtils2Service you have the Variable Okhttpclient (hc) (not tested) Upvote 0
In B4A IDE --> Project you'll have to Add Existing Modules --> HttpUtils2Service.bas and HttpJob.bas (zip file in https://www.b4x.com/android/forum/threads/okhttp-replaces-the-http-library.54723/) --> don't add the okhttp library In the Process_globals of HttpUtils2Service you have the Variable Okhttpclient (hc) (not tested)
Erel B4X founder Staff member Licensed User Longtime User Mar 5, 2017 #10 Martin Larsen said: What could be wrong? Click to expand... 1. Questions should never be posted in the libraries forum. 2. You should call Job.Download and then Job.GetRequest.RemoveHeader. https://www.b4x.com/android/forum/t...is-code-work-correctly-and-why.23328/#content Upvote 0
Martin Larsen said: What could be wrong? Click to expand... 1. Questions should never be posted in the libraries forum. 2. You should call Job.Download and then Job.GetRequest.RemoveHeader. https://www.b4x.com/android/forum/t...is-code-work-correctly-and-why.23328/#content
Martin Larsen Active Member Licensed User Longtime User Mar 5, 2017 #11 inakigarm said: In B4A IDE --> Project you'll have to Add Existing Modules --> HttpUtils2Service.bas and HttpJob.bas (zip file in https://www.b4x.com/android/forum/threads/okhttp-replaces-the-http-library.54723/) --> don't add the okhttp library Click to expand... Thanks, it works! Upvote 0
inakigarm said: In B4A IDE --> Project you'll have to Add Existing Modules --> HttpUtils2Service.bas and HttpJob.bas (zip file in https://www.b4x.com/android/forum/threads/okhttp-replaces-the-http-library.54723/) --> don't add the okhttp library Click to expand... Thanks, it works!
Martin Larsen Active Member Licensed User Longtime User Mar 5, 2017 #12 Erel said: 1. Questions should never be posted in the libraries forum. Click to expand... Sorry I didn't know that. I actually posted it there because it was a question about a library. Erel said: 2. You should call Job.Download and then Job.GetRequest.RemoveHeader. Click to expand... 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
Erel said: 1. Questions should never be posted in the libraries forum. Click to expand... Sorry I didn't know that. I actually posted it there because it was a question about a library. Erel said: 2. You should call Job.Download and then Job.GetRequest.RemoveHeader. Click to expand... 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.
Erel B4X founder Staff member Licensed User Longtime User Mar 5, 2017 #13 Cookies are added to the headers at a later point. Upvote 0
Martin Larsen Active Member Licensed User Longtime User Mar 5, 2017 #14 #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
#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.
Erel B4X founder Staff member Licensed User Longtime User Mar 5, 2017 #15 Martin Larsen said: Is it a "feature" of the underlying java lib? Click to expand... Yes. Upvote 0