B4J Question Web Services.... how to get cookies and go on?

RWK

Member
Licensed User
Longtime User
Dear all,

Based upon Erels great tutorials....especially the WebServices Video I had the idea to build a app for my children to logon to the school homepage to get some files automatically.

https://www.schulen-intern.de/scholl/login/index.php

It is a moodle Page.

Then i read many of the articeles here in the forum.... but the based on jHttp, jHttpUtils, jHttUtils2, okHttp, OKHttoUtils(2)...It seems many things have changed the last 1 1/2 year.

There are object OkHttpClient, OKHttpJob, OkHttpRequest, OKHttpResponse
I have problems to understand the relationship between the objects. For a test i did in in B4J.... but in the end i want it to do in B4A with the OKhttpUtils2 and OKHttp.


So i would so glad to see somewhat basic tutorial to handle this:

I can get by a simple request the login page; i understand that i have to get the headers form the response after Login post and from there the cookie...Than use the cookie in the following request to get access.

But I can't get thing in a row....

How would you go to get onto an unknown webService.

I would do it like this:

B4X:
Sub GetLoginPage
    Dim a As OkHttpClient 'is this something like the WebView?
    Dim j As HttpJob ' I work with this
    Dim r As OkHttpRequest ' this belongs to HttpJob? 
    Dim res As OkHttpResponse ' can i get this from given j(httpJob) to get the headers?
    
    j.Initialize("",Me)
    j.Download("https://www.schulen-intern.de/scholl/login/index.php"

    Wait For (j) JobDone(j As HttpJob)
    If j.Success = True Then
        Log(j.GetString)
    End if

        'And now?
        'Look into the code to get the needed Fields to create a httpPost. Build it.
        ' Post the UserName and the Password to the Page
        ' Get the response to read the headers
       
        'res.GetHeaders
        'Dim list1 As List
        'list1 = res.GetHeaders.Get("Set-Cookie")
        'For i = 0 To list1.Size - 1
        '    Log(list1.Get(i))
        '    Get the cookie(s)
        'Next

        'download any page
        'set the cookie to get them
    j.Download("any WebPage from the WebService")  'or maybe httpRequest ?
       ' xxx.setHeaders("Cookie","theCookie")    

    Wait For (j) JobDone(j As HttpJob) ' or is it here httpRequest, cause i had to set a cookie?
    If j.Success = True Then
        Log(j.GetString)
    End If

    ' Got the page I need   
End Sub

I know that all of this I should read out of the Forum Posts here in B4J and B4A, but I am overwhelmed by the amount of post with all the different objects.

Maybe someone has the mercy to put the basic steps together how to handle such WebService problems.

Greetings
Rainer


P.S. As a workaround I also walked along on this with a Webview.
All these works well in a WebView like in other browsers, but I found nothing to interact with Webview to get rid of the manually inputs.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
OkHttp and OkHttpUtils2 replaced Http and HttpUtils2.

If you want to get the response headers then you will need to use OkHttpUtils2 source code instead of the library and modify hc_ResponseSuccess with:
B4X:
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   Dim job As HttpJob = TaskIdToJob.Get(TaskId)
   job.Tag = Response.GetHeaders
   
   Response.GetAsynchronously("response", File.OpenOutput(TempFolder, TaskId, False), _
     True, TaskId)
End Sub

The headers map will be available in Job.Tag.
 
Upvote 0
Top