B4J Question Cookie & Get Request - non ui

GabrielM

Member
Licensed User
Longtime User
Hi

Basically I would like to try and see if I can port the following python code to B4J - non-ui app.



B4X:
import sys
import requests
from multiprocessing import Queue


def main(p, s, d):
    # Get a cookie
    cookie = requests.get("http://www.nosuchsite.org/cgi-bin/Initial.py?area=TII&calcNo=11230").headers["Set-Cookie"]
    headers = {
        "Host": "www.nosuchsite.org",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0",
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Accept-Language": "en-US,en;q=0.5",
        "Accept-Encoding": "XMLHttpReques",
        "Referer": "http://www.nosuchsite.org/testmath/calcul.html",
        "Cookie": cookie,
        "Connection": "keep-alive"
    }

    ### Input Data
    # Units
    requests.get("http://www.nosuchsite.org/cgi-bin/SetValues.py?ir=unitGroup&index=0&type=UnitGroup&value=1&calcNo=11230", headers=headers)

    # p - first parameter
    requests.get("http://www.nosuchsite.org/cgi-bin/SetValues.py?ir=input&index=0&type=text&value=%s&calcNo=11230" % p, headers=headers)

    # s - second parameter
    requests.get("http://www.nosuchsite.org/cgi-bin/SetValues.py?ir=input&index=1&type=text&value=%s&calcNo=11230" % s, headers=headers)

    # d - thrird parameter
    requests.get("http://www.nosuchsite.org/cgi-bin/SetValues.py?ir=input&index=2&type=text&value=%s&calcNo=11230" % d, headers=headers)

    results = requests.post("http://www.nosuchsite.org/cgi-bin/Calculation.py?calcNo=11230", headers=headers)
  
    return results.json()[3]["TextValue"]


if _name_ == "__main__":
    my_result = main(p=sys.argv[1], s=sys.argv[2], d=sys.argv[3])
    print(my_result)


What I can't figure how to go with is the line where:

B4X:
# Get a cookie
  cookie = requests.get("http://www.nosuchsite.org/cgi-bin/Initial.py?area=TII&calcNo=11230").headers["Set-Cookie"]


What I have so far in B4J code is:

B4X:
Sub Process_Globals

   Private Submit As Button
End Sub

Sub AppStart (Args() As String)
   Submit_Action
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

Sub Submit_Action
  
   Dim j As HttpJob
  

  
   j.Initialize("downloadFile",Me )
   j.PostString("http://www.nosuchsite.org/cgi-bin/Initial.py?area=TII&calcNo=11230","Set-Cookie")
   j.GetRequest.SetHeader("Host","www.nosuchsite.org")
   j.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0")
   j.GetRequest.SetHeader("Accept", "application/json, text/javascript, */*; q=0.01")
   j.GetRequest.SetHeader("Accept-Language", "en-US,en;q=0.5")
   j.GetRequest.SetHeader("Accept-Encoding", "XMLHttpReques")
   j.GetRequest.Setheader("Referer", "http://www.nosuchsite.org/global/TI/calculator/steam-flow-rate-through-orifice.html")
   j.GetRequest.SetHeader("Cookie","Set-Cookie")
   j.GetRequest.SetHeader("Connection", "keep-alive")

   j.GetRequest.SetHeader("Content-Type", "application/json; charset=UTF-8")
  
  
   j.GetRequest.InitializeGet("http://www.nosuchsite.org/cgi-bin/SetValues.py?ir=unitGroup&index=0&type=UnitGroup&value=2&calcNo=11230")
   j.GetRequest.InitializeGet("http://www.nosuchsite.org/cgi-bin/SetValues.py?ir=unitGroup&index=1&type=UnitGroup&value=1&calcNo=11230")
   j.GetRequest.InitializeGet("http://www.nosuchsite.org/cgi-bin/SetValues.py?ir=unitGroup&index=2&type=UnitGroup&value=0.8&calcNo=11230")


   j.PostString("http://www.nosuchsite.org/cgi-bin/Calculation.py?calcNo=11230", "results")

End Sub


Sub JobDone(job As HttpJob)
  
   Log(job.Tag)
   If job.Success Then
     Log(job.JobName & " - " & job.GetString)
  Else
     Log("Error friends")
   End If
   job.Release

End Sub

Any hint please ?

Gabi.
 
Last edited:

GabrielM

Member
Licensed User
Longtime User
Fantastic :) , I really did not know that, thank you Erel.

I've tried now and monitored with WireShark, just Great!
 
Upvote 0
Top