Android Question [SOLVED] httpjob, cookie and redirect problem

sinner181

Member
Hello,
I have a little problem with "httpjob", when I send a POST to a website to login, it replies with a set-cookie and 302 redirect.
The problem is that I need to estract the cookie from the redirect page, because the final page doesn't have any cookie inside the response headers.

here my code
B4X:
Sub SubitoLogin_Click    
    action = "LOGIN"
    Dim http As HttpJob
    http.Initialize("request",Me)
    http.PostString("https://thewebsite/login","username=123456&password=ABCDEF")
End Sub


here the code to get response

B4X:
private Sub jobDone (job1 As HttpJob)
 
   If job1.Success = True Then

        Risultato.Text = job1.GetString
        
        Dim Result As String
        Dim i As Int

        If action = "LOGIN" Then
                Dim resp As OkHttpResponse = job1.Response
                Dim headers As Map  = resp.GetHeaders
                For Each key As String In headers.Keys
                    If key = "set-cookie" Then
                      Log(headers.Get(key))
                    End If
                Next
       End If
 End If
   
End Sub

How I can extract "set-cookie" from the page with 302 redirect? Thanks.
 

drgottjr

Expert
Licensed User
Longtime User
you almost had it!

B4X:
Dim list1 As List
list1 = response.GetHeaders.Get("Set-Cookie")
For i = 0 To list1.Size - 1
    Log(list1.Get(i))
Next
 
Upvote 0

sinner181

Member
Thank you for reply, but problem was not solved =(

I see only the final cookies page, the redirect page was skipped from jobDone" function.

What I can do?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
sorry. i mis-read. try this: handle 302
you need to have the 302 redirect ignored so you can get its cookies.
if you can't figure it out, i can look into it. if you don't want to post the url and any credentials for access, you can pm.
unless i'm mistaken, you'll need to use the original okhttputils2 class (or the B4X version), and make a small change. similar problems/solutions have been handled this way. some of them have proved useful and ended up as part of the library. it would help to have an actual 302 to test with, however. since you actually have one, it's on you to figure things out.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
just a follow up: i can trap the 302, capture the cookie and see where the redirect wants to send me. the first step is the link i posted yesterday. the next step is to unzip okhttputils.b4xlib and make changes to the service. then a couple adjustments to main. not difficult, but a little tedious. it works.

i set up a 302 redirect with a cookie on my web server. when i try to access the page with the 302, i get what i need to go to the new location with the cookie from the 302.

if you've already got it under control, i'm not going to bother with it any further.
 
Upvote 0

sinner181

Member
I can try with handle-redirect=false, but it would be even simpler if I could extract all the cookies from httpjobs after reaching the final page.

Redirect page assign some cookies that I need, there is possibility to extract this cookies?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
Redirect page assign some cookies that I need, there is possibility to extract this cookies?

i just told you i can do this. but redirect has to be disabled, which i also do. it's better if you figure out what you want before changing the problem
 
Upvote 0

sinner181

Member
Sorry, i'm newbie and now in crisis!

I know it will be simple for you, but I do not know this language properly and how it is structured, I am not able to understand.

i'm trying to set this

B4X:
hc.Initialize("hc")
hc.SetHttpParameter("http.protocol.handle-redirects", False)

in HttpUtils2Service.bas (version 7.28) and receive the warning message (as your posted thread)
Errore nella linea: 41
hc.SetHttpParameter("http.protocol.handle-redirects", False)
Word: sethttpparameter

I see the other page from Erel solution and see:
B4X:
'Replace the existing method in HttpUtils2Service
Sub Initialize
   If hc.IsInitialized = False Then
     TempFolder = File.DirTemp
     Dim jo As JavaObject = hc
     Dim builder As JavaObject = jo.RunMethod("sharedInit", Array("hc"))
     builder.RunMethod("retryOnConnectionFailure", Array(False))
     jo.SetField("client", builder.RunMethod("build", Null))     
     TaskIdToJob.Initialize
   End If
End Sub

to replace "retryOnConnectionFailure" with "followRedirects", but exactly.....where is this SUB INITIALIZE to replace? which file?

I'm really confused....sorry.

18 years of delphi programming and it looks like I'm taking the computer for the first time šŸ„µšŸ„µšŸ„µšŸ„µ

only a suggestion, if possible, in your next update insert a call like Job1.followredirect = (boolean), is more simple to manage webpages and cookies and we can choose if follow redirect or not.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Top