Android Question Get HTTP redirect page

Shay

Well-Known Member
Licensed User
Longtime User
I am going to some URL, and this page is redirecting to another page
I need to get the new page after it is redirected
I need to get the response and not actually display it
how do I do it with okHTTP?
 

OliverA

Expert
Licensed User
Longtime User
okHttp automatically handles redirects.

Example: going to "https://jigsaw.w3.org/HTTP/300/302.html" will end up at "https://jigsaw.w3.org/HTTP/300/Overview.html"

Below code was tested in B4J 5.9 with only the jCore (Version 5.8) and jOkHttpUtils2_NONUI (version 2.62) libraries checked.

B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
   
End Sub

Sub AppStart (Args() As String)
    doit
    StartMessageLoop
End Sub

Sub doit
    Dim job As HttpJob
    job.Initialize("", Me)
    job.Download("https://jigsaw.w3.org/HTTP/300/302.html")

    Wait For (job) jobDone(job As HttpJob)
    If job.Success Then
        Log(job.GetString)
    Else
        Log($"Error: ${job.ErrorMessage}"$)
    End If
    job.Release

    StopMessageLoop
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
I tried it (without the Start and StopMessageLoop - cannot find it on B4A)
and it still gives first page data
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Ok, tried it in B4A (7.30) using both the OkHttpUtils2 (Version 2.61) and HttpUtils2 (Version 2.50) libraries, and automatic redirection worked. If you manually add the class code/module code of these libraries and modified them, you may need to look at your modifications and see if it changed something in regards to redirects (turned it off?)

B4X:
' Process_Globals and Regions above this
'
Sub Globals
    Dim Panel1 As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Panel1.Initialize("")
    Panel1.Visible=True
    Activity.AddView(Panel1, 0, 0, 100%x, 100%y)

    doit
   
End Sub

Sub doit
    Dim job As HttpJob
    job.Initialize("", Me)
    job.Download("https://jigsaw.w3.org/HTTP/300/302.html")

    Wait For (job) jobDone(job As HttpJob)
    If job.Success Then
        Log(job.GetString)
    Else
        Log($"Error: ${job.ErrorMessage}"$)
    End If
    job.Release

End Sub
'Activity_Resume and Activity_Pause below this

Output:
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
still not working to me, I assume it is either related to some java script or the redirect happens after 1-2 seconds and not right away
you can see it on Jewish Saturday or holiday such as today
http://www.holyclock.com
if you enter the site (click english) on Sat or Holiday it will redirect you to other page, which it seems I cannot get it
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
So are you trying to get to the actual site instead of the re-directed site? So instead of following the re-direction (which okhttp does), your trying to "un-redirect" it?
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
no, I am trying to get the closed page, but I am getting the actual site (twice)
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I'll check it out when I get a chance (if I don't want to change a clock, I'll have to wait a little bit).
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Maybe the site is doing a refresh instead of redirect and okhttp or whatever underlying library does not forward the site due to it being a refresh (just guessing).

See the accepted answer to this stackoverflow question: https://stackoverflow.com/questions/11299006/header-location-delay

Edit: If you can print all the headers out from the request, you may get a clue. Look for redirect/refresh
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
It looks like that particular site used JavaScript's window.location property to redirect the page to the new location. I'm pretty sure the underlying http library used does not understand JavaScript and therefore you are not redirected to the other page.

WARNING: The next suggestion is just from what I've read here and there and has absolutely nothing to do with personal experience with this.

How about using a WebView to load your page. If the WebView processes the JavaScript, should it not properly "redirect" to the next page? Can you then query WebView on what page it is (and, maybe, its contents)? If this happens to work, this could be used even for the "proper" redirects and may even work for "refresh". Please note, I have no clue if it's possible, someone else will have to help out here.
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
just realized, using WebView is problematic, since I am using service
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
b.t.w you are right, this is JS.
I blocked the JS in the browser and it cannot "redirect" the page
now i need to find a way to do it using HTTP request (run JS)
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
ok, never mind I will write my own DB and use it
Thank you all for your help
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…