Returning cookies using HttpUtils

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
I got another project going here…
I am interfacing my B4A app with PHP and webpages from a website.
I want my next request to include some cookies received with the previous response.

My question is how best to do that?

I have modified the HttpUtils a little bit -

In the HttpUtils Service I changed the Response Sub to also write the headers to a file:

B4X:
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   Dim mp As Map

   Response.GetAsynchronously("response", File.OpenOutput(TempFolder, TaskId, False), True, TaskId)
   
   mp.Initialize
   mp = Response.GetHeaders
   File.WriteMap(TempFolder, TaskId & "mp", mp)
End Sub

In the HttpUtils module I added the the Sub GetHeaders (just below the current GetString Sub):

B4X:
Sub GetString(URL As String) As String
   If IsSuccess(URL) = False Then
      Log("Task not completed successfully.")
      Return ""
   End If
   Return File.GetText(HttpUtilsService.TempFolder, SuccessfulUrls.Get(URL))
End Sub

Sub GetHeaders(URL As String) As Map
    Dim mp As Map
   
   mp.Initialize

   If IsSuccess(URL) = False Then
      Log("Task not completed successfully.")
       Return(mp)
   End If
   
   mp = File.ReadMap(HttpUtilsService.TempFolder, SuccessfulUrls.Get(URL) & "mp")
   Log(mp)  ' remove this line after testing
   Return(mp)
End Sub

One of the entries from the resulting Map looks like (viewed in the log):

Set-Cookie=[PHPSESSID=44iopqshfn80674fm6nkt62rs6; path=/]

When I make the next HttpRequest, how should I include this cookie?
Would I use something like:
req.SetHeader(PHPSESSID, "44iopqshfn80674fm6nkt62rs6")

And will this be interpreted as returning a cookie?

Thanks,
Barry.
 
Last edited:
Top