B4J Question Sever Json (Jserver) post to remote wservice XML

victormedranop

Well-Known Member
Licensed User
Longtime User
hi, i'm doing a proxy server that receive json from a android device the server must connect to and web server and consume one web service.

my server that receive it's simple. I use MySQL to validate de machine it's accessing.

B4X:
Sub Process_Globals
 Dim sql1 As SQL
 Dim wserver As Server
End Sub
Sub AppStart (Args() As String)
 wserver.Initialize("wserver")
 wserver.Port = 9999
 wserver.LogsFileFolder=File.Combine(File.DirApp,"logs")
 wserver.AddHandler("/error", "Error_page", False)
 wserver.AddHandler("/webservice", "wservice", False)
 wserver.Start
 sql1.Initialize2("com.mysql.jdbc.Driver", "jdbc:mysql://127.0.0.1/retails?autoReconnect=true&useSSL=false&characterEncoding=utf8","admin","popola")
 If sql1.IsInitialized = True Then
 Log("Connected to database")
 Else
 Log("NOT Connected to database")
 End If
 Log("Server started")
 StartMessageLoop
End Sub

the webservice module received the information

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
 mreq = req
 mresp = resp
 Dim jp As JSONParser
 Dim data() As Byte = Bit.InputStreamToBytes(req.InputStream)
 jp.Initialize(BytesToString(data, 0, data.Length, "UTF8"))
 Dim resJson As Map
 resJson.Initialize
 Try
 Dim json As Map = jp.NextObject
 Catch
 resJson.Put("success", False)
 resJson.Put("error", LastException.Message)
 End Try
 resp.ContentType = "application/xml; charset=UTF-8"
 check_retailID(json.Get("RetailId"),json.Get("TerminalId"))
End Sub

I check if the user is valid.

B4X:
Sub check_retailID (company As String, user As String)
 If Main.sql1.IsInitialized = True Then
 Dim query As String = $"SELECT id FROM retails WHERE retail_id = "${company}" and terminal_id = "${user}""$
' Log(query)
 Try
 Dim rs As ResultSet = Main.sql1.ExecQuery(query)
 Dim resultado As String
 Do While rs.NextRow
 resultado = rs.GetString2(0)
 Loop
 If resultado = "" Then
 mresp.SendError(500,"Nothing Here!")
 Else
 Log("User is valid")
 Login_utd
 End If
 Catch
 Log(LastException)
 End Try
 End If
 rs.Close
End Sub


my problem is when I try to connect to the remote service
how can I read a variable in the header ? I can login but cant consume the web service
because the key in the header.

if someone can point me in the right direction

B4X:
Sub Login_utd
 Log("Starting connection to UTD")
 Dim Login As HttpJob
 Login.Initialize("login",Me)
 Login.PostString("http://X.x.x.x/Sessse.asmx",login_data)
 Login.GetRequest.SetHeader("SOAPAction","http://tempuri.org/Login")
 Login.GetRequest.SetContentType("text/xml; charset=utf-8")
End Sub
Sub JobDone(Job As HttpJob)
 Log("class:JobDone("&Job.GetString&")")
  If Job.Success Then
  Select Job.JobName
 Case "login"
 mresp.Write(Job.GetString)
  End Select
  Else
  mresp.Write(Job.ErrorMessage)
  End If
  Job.Release 
End Sub
 
Top