Using HTTP calls in Gambas 3.15
Posted
#1
(In Topic #624)
Enthusiast

I was wondering if someone could talk me though converting this bit of VB.net code so it can function with Gambas
The following are loaded at start up of the module
PS_URL
TerminalIDNumber
PS_USER
PS_PASS
as these are unique to each client
Code
Private Sub GetTerminalStatus()
Try
Dim Request As HttpWebRequest = HttpWebRequest.Create(PS_URL & "/terminals/" & TerminalIDNumber)
Dim credentials As String = Convert.ToBase64String(Encoding.ASCII.GetBytes(PS_USER & ":" & PS_PASS))
txtResults.Text = vbNullString
With Request
.Proxy = Nothing
.Headers(HttpRequestHeader.Authorization) = String.Format("Basic \{0}", credentials)
.UserAgent = PS_USER
End With
Dim response As HttpWebResponse = Request.GetResponse()
Dim dataStream As Stream = response.GetResponseStream
Dim reader As New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
txtResults.Text = responseFromServer
If responseFromServer = "0" Then
MsgBox("Retreal of Status Failed")
addtoStatusList("Retreal of Status Failed")
If DebugActive = "Yes" Then AddToDebugList("Retreal of Status Failed Please try again")
SendToPoSterminal("RetrealFailed")
Else
Dim json As String = responseFromServer
Dim ser As JObject = JObject.Parse(json)
Dim data As List(Of JToken) = ser.Children().ToList
For Each item As JProperty In data
item.CreateReader()
Select Case item.Name
Case "status"
Select Case item.Value
Case "AVAILABLE"
GetStatus = 1
SendToPoSterminal("TerminalOnLine|")
addtoStatusList("Terminal Ready")
Case "BUSY"
SendToPoSterminal("TerminalBusy|")
addtoStatusList("Terminal busy please wait 10 seconds and try again")
Case "Offline", "OFFLINE", "offline"
SendToPoSterminal("offline|")
addtoStatusList("Terminal OFFLINE NO Card processing Possible")
End Select
End Select
Next
End If
Catch ex As Exception
addtoStatusList(ex.ToString)
If DebugActive = "Yes" Then AddToDebugList(ex.ToString)
SendToPoSterminal("ProcessingError")
FromPoSTCP.Stop()
FromPoSTCP.Start()
End Try
End Sub
I ask as I am considering to moving some commercial applications from Windows into Linux and this is the one that if I can not be converted would be the cancellation of the full migration.
If someone could guide me on this one (as it is one of the simple functions I can work out the rest of the other functions myself
The PoS Talks to the Module via TCP Connection at the moment but if someone knows a more quicker and stable way (that does not use files) then I am open to the idea of updating the communication method
Posted
Guru

You will need to use gb.curl for the web part and gb.util.web to decode the JSON data. Something along the lines of: -
Code (gambas)
- With hClient 'With the Client..
- .URL = "https://poloniex.com/public?command=returnTicker" 'Set up the URL
- .TimeOut = 60 'Don't hang around waiting for more than 60 seconds
- .get 'Get the data
1 guest and 0 members have just viewed this.



