iOS Question HttpUtils2 Get Cookie

lucasheer

Active Member
Licensed User
Longtime User
Hello!

I'm writing an application that allows users to cycle between multiple accounts on an external service. This requires me to store/manage the cookies I use for each account, so I can present them to the server when I require information on a particular account.

On B4A and B4J, I modified the HTTPUtils2Service to grab the cookies after I send my request:

B4X:
#if B4A or B4J
Sub hc_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
    Dim job As HttpJob = TaskIdToJob.Get(TaskId)
    job.Response = Response
    job.Tag = Response.GetHeaders
    Response.GetAsynchronously("response", File.OpenOutput(TempFolder, TaskId, False), _
        True, TaskId)
End Sub

How will I handle this in B4i? The B4i ResponseSuccess method:
B4X:
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
    CompleteJob(TaskId, True, "", Response)
End Sub

I noticed this is HttpResponse instead of OkHttpResponse. Will it be possible to get the headers from this?

Thank you!
 

lucasheer

Active Member
Licensed User
Longtime User
This modification is not needed. You can access job.Response.GetHeaders directly.

I'm using the j.Response.GetHeaders function to get my cookies from the response. That is working :)


However, I use the "set-cookie" header to grab the cookies in the response:

B4X:
Dim headers As Map = j.Response.GetHeaders
            Log("--------------------------------------------------")
            For Each v As String In headers.Values
                Log(v)
            Next
            Log("--------------------------------------------------")

            Log(headers)

            If(headers.IsInitialized) Then
                If(headers.ContainsKey("set-cookie")) Then
                    newHTTPResponse.cookies = headers.Get("set-cookie")
                Else
                    newHTTPResponse.cookies.Initialize
                End If
            End If


I tried logging all the values, and getting the value "set-cookie", but it only lets me have the last header called "set-cookie", which is the "TS015587bf" cookie:
upload_2019-12-8_1-58-36.png



Here is what I get from looping the values:
B4X:
--------------------------------------------------
[no-cache, no-store]
[text/html; charset=utf-8]
[Sun, 08 Dec 2019 08:06:15 GMT]
[-1]
[no-cache]
[TS015587bf=01a28dbe2553f78ec3d7a5e52594ccb49e3c8877c92a23b064a189b4c60b2d672e55437003ab4c74aa88704dd7b417a4217ae3467b148bd3734fceb5b621867290a1680fba7e23d3760bd7a5bdfbac63eda4731dfedbacdc88297a818dc4038686b5fb923ea49d775e4a5f4e055243431cdf6bbdee; Path=/; Domain=.**]
[Accept-Encoding]
[SAMEORIGIN]
[IE=Edge]
--------------------------------------------------

Is it not showing duplicates header names?? It's only giving me the last header value with the name "set-cookie".


I know that the HTTPJob is storing more cookies than I am able to see. The server won't respond without the .AuthCookie, and the server is giving me everything I want. So it's getting sent.

Any advice on what might be happening?

Thank you!
 

Attachments

  • upload_2019-12-8_2-6-48.png
    upload_2019-12-8_2-6-48.png
    14.6 KB · Views: 112
Upvote 0

lucasheer

Active Member
Licensed User
Longtime User
I just figured out that I'll need to modify the HTTPService in order to delete/change the cookies as well XD

I might come back to this later, but for now ill just sign out and log into different accounts as I need to.

Thank you!
 
Upvote 0
Top