B4J Question use of the PHPSESSID cookie

jose luis gudino

Active Member
Licensed User
Longtime User
Hello everyone I have a great confusion. I will try to explain:

I am making a Poststring in a web application, so that you can return the correct result to me, I must send it the PHPSESSID cookie, otherwise it does not work

B4X:
PHPSESSID = "0t96biq57q96cq8tpe5rpo0gla"

B4X:
Dim Map As Map = CreateMap("codigoHotel": 10853,"lg":"","usuario":14082, "pass": "Invitado123")
    Dim gen As JSONGenerator
    gen.Initialize(Map)
    Dim j As HttpJob
    PHPSESSID = "0t96biq57q96cq8tpe5rpo0gla"
    j.Initialize("",Me)
    j.PostString("https://app.lobbypms.com/entrar/validarDatos", gen.ToString)
    j.GetRequest.SetContentType("application/x-www-form-urlencoded")
    j.GetRequest.SetHeader("Cookie", "PHPSESSID="&PHPSESSID)
    wait for (j) JobDone(job As HttpJob)
    If j.Success Then
        Dim jp As JSONParser
        jp.Initialize(j.GetString)
        Log(j.GetString)
        File.WriteString(File.DirApp,"captura.txt",j.GetString)
        Dim parser As JSONParser
        parser.Initialize(j.GetString2("UTF8"))
        '****************
            Reporte_exel  '(02)
        '*****************
    End If
    j.Release

and to get that cookie PHPSESSID, I use postman from there I get that value

Captura.PNG


My question is: how can I get that PHPSESSID value before submitting the PostString.

thanks in advance
 
Last edited:

jose luis gudino

Active Member
Licensed User
Longtime User
Hi, thank you for you answer.
what I am trying to do in a web page login.
at the moment I am using this code: it does not have PHPSESSID and I cannot log in:

B4X:
Dim Map As Map = CreateMap("codigoHotel": 10853,"lg":"","usuario":14082, "pass": "Invitado123")
    Dim gen As JSONGenerator
    gen.Initialize(Map)
    Dim j As HttpJob
    j.Initialize("",Me)
    j.PostString("https://app.lobbypms.com/entrar/validarDatos", gen.ToString)
    j.GetRequest.SetContentType("application/x-www-form-urlencoded")
    wait for (j) JobDone(job As HttpJob)
    If j.Success Then
        Dim jp As JSONParser
        jp.Initialize(j.GetString)
        Log(j.GetString)
        File.WriteString(File.DirApp,"captura.txt",j.GetString)
        Dim parser As JSONParser
        parser.Initialize(j.GetString2("UTF8"))
        '****************
            Reporte_exel  '(02)
        '*****************
    End If
    j.Release

this is the code that postmat suggests me, and this as erel says does not include the PHPSESSID:

Captura.PNG


Now, the difference is that with my code the application does not manage to log in, while with postman it does log in.
then I go to the postman results and I check the values returned by the cookie and I get the PHPSESSID I put it in my code and that way if I can log in
 
Upvote 0

jose luis gudino

Active Member
Licensed User
Longtime User
Why would you need the Session ID before initiating a request?
Does that web service "require" the session ID before any previous requests have been made? Because that doesn't make any sense.
Hi
Thanks for your interest,
if indeed it is extremely strange, that's why I try to explain it in the next post.
But in summary it is: if I do not get that variable I cannot log in the page, I have to pass it as a parameter, but as Erel says, that variable is it after the poststring is sent
 
Upvote 0

MegatenFreak

Active Member
Licensed User
Hi
Thanks for your interest,
if indeed it is extremely strange, that's why I try to explain it in the next post.
But in summary it is: if I do not get that variable I cannot log in the page, I have to pass it as a parameter, but as Erel says, that variable is it after the poststring is sent
Hi again! I think I solved your problem!
I tried your code and it didn't work, but then I replaced the line:
B4X:
    j.PostString("https://app.lobbypms.com/entrar/validarDatos", gen.ToString)
with:
B4X:
    j.PostString("https://app.lobbypms.com/entrar/validarDatos", "codigoHotel=10853&lg=&usuario=14082&pass=Invitado123")
And it worked! I thought maybe the string data should be sent in the same format that Postman does, so I tried it and I managed to login and open the page.
 
Upvote 0

jose luis gudino

Active Member
Licensed User
Longtime User
Hi again! I think I solved your problem!
I tried your code and it didn't work, but then I replaced the line:
B4X:
    j.PostString("https://app.lobbypms.com/entrar/validarDatos", gen.ToString)
with:
B4X:
    j.PostString("https://app.lobbypms.com/entrar/validarDatos", "codigoHotel=10853&lg=&usuario=14082&pass=Invitado123")
And it worked! I thought maybe the string data should be sent in the same format that Postman does, so I tried it and I managed to login and open the page.



Fantastic. It worked!!!!
Thank you very much for your help and for your collaboration 👏 👏 👏 :D
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
a useful gift:
B4X:
    Dim Parameters As Map = CreateMap("codigoHotel" : 10853, "lg": "","usuario": 14082, "pass": "Invitado123")
    ....
    j.PostString("https://app.lobbypms.com/entrar/validarDatos", EncodeMapParameter(Parameters))
B4X:
Public Sub EncodeMapParameter(Parameters As Map) As String
    Dim sb As StringBuilder
    sb.Initialize
    If Parameters.Size = 0 Then Return sb.ToString
    Dim su As StringUtils
    Dim f As Boolean
    For Each k As String In Parameters.Keys
        If f Then sb.Append("&")
        sb.Append(su.EncodeUrl(k, "UTF8")).Append("=")
        sb.Append(su.EncodeUrl(Parameters.Get(k), "UTF8"))
        f = True
    Next
    Return sb.ToString
End Sub
Encode:
codigoHotel=10853&lg=&usuario=14082&pass=Invitado123
 
Upvote 0

jose luis gudino

Active Member
Licensed User
Longtime User
a useful gift:
B4X:
    Dim Parameters As Map = CreateMap("codigoHotel" : 10853, "lg": "","usuario": 14082, "pass": "Invitado123")
    ....
    j.PostString("https://app.lobbypms.com/entrar/validarDatos", EncodeMapParameter(Parameters))
B4X:
Public Sub EncodeMapParameter(Parameters As Map) As String
    Dim sb As StringBuilder
    sb.Initialize
    If Parameters.Size = 0 Then Return sb.ToString
    Dim su As StringUtils
    Dim f As Boolean
    For Each k As String In Parameters.Keys
        If f Then sb.Append("&")
        sb.Append(su.EncodeUrl(k, "UTF8")).Append("=")
        sb.Append(su.EncodeUrl(Parameters.Get(k), "UTF8"))
        f = True
    Next
    Return sb.ToString
End Sub
Encode:
Excellent!!.
Thank
 
Upvote 0
Top