HTTPUtills2 - Clear Cookies

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
I am using HTTPUtills2 to communicate with some PHP Web Services. They use PHP Sessions via a cookie PHPSESSID=some_long_string. HTTPUtills2 accepts this cookie and returns it on the next request.

My question:
Can I delete this cookie in HTTPUtills2 so that it is not sent with the next request?

Setting the cookie to an empty value would be okay, although I would rather remove the cookie altogether or set it to expire immediately.

Thanks for any help.
Barry.
 

canalrun

Well-Known Member
Licensed User
Longtime User
You can use HttpRequest.RemoveHeaders("Cookie") to remove all the cookies. This should be done after the request is created.

Hello,
Thanks for your reply.

I am looking at the HTTPJob file. I see as part of the
Sub Class_Globals
Public req As HttpRequest

Then further on down in PostBytes, req is initialized

I'm assuming the Header values for this request are inherited from some higher-level Android object. If I remove Header cookies from this request after it is initialized will it remove the cookie from this request but leave the cookie defined in the higher-level Android object – thereby not really removing the cookie?

If my assumption is true, I would maybe be better off keeping a local copy of the cookie value I want and then explicitly setting that value each time a req is initialized?

Or, is my assumption wrong and removing the cookie gets rid of the cookie for the current and all future requests?


Added Later:

I am having trouble using the HTTPRequest methods.

Here's code from my Main B4A file:
B4X:
  hd.Initialize  // header Map
  hd.Put("cookie", "PHPSESSID=changed")
  httpJb.PostString(PostUrl, s, hd)

Here is code from the HttpJob file which I have modified to handle the header map:
B4X:
Public Sub PostString(Link As String, Text As String, hd As Map)
  PostBytes(Link, Text.GetBytes("UTF8"), hd)
End Sub

Public Sub PostBytes(Link As String, Data() As Byte, hd As Map)
  mLink = Link
  req.InitializePost2(Link, Data)
  If (hd.Size > 0) Then
    For i=0 To hd.Size-1
      Log(hd.GetKeyAt(i))
      Log(hd.GetValueAt(i))
      req.RemoveHeaders(hd.GetKeyAt(i))
      req.SetHeader(hd.GetKeyAt(i), hd.GetValueAt(i))
    Next
  End If
  CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub

Here is the corresponding output from the Log file:
B4X:
  cookie
  PHPSESSID=changed

And finally, here is debugger output from within my PHP IDE:
B4X:
  Cookie:
    [PHPSESSID]    changed, PHPSESSID=cmhlhgl25mne7ci876sjl7d897

From the debugger output in the PHP IDE you can see the value of the cookie PHPSESSID contains the original value preceded by my new value "changed".

I've tried every combination I can think of – changing character case, including or not including the RemoveHeader, exchanging "cookie" with "PHPSESSID", adding a colon, and many other things.

I cannot seem to get it to remove the original PHPSESSID cookie or header nor can I get it to change the original value with my new value of "changed".

What am I missing?

Thanks,
Barry.
 
Last edited:
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
I see, the cookies are added later before the request is sent. Please try this solution:
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

Thanks. As soon as I realized this needs to go in HttpUtils2Service it worked like a champ.

Barry.
 
Upvote 0
Top