Help Converting From VB.net to Gambas
Posted
#1
(In Topic #693)
Enthusiast

Code
Dim sBuffer As String
Dim Request As HttpClient = HttpClient.Create(Global.PS_URL & "/terminals/" & Global.TerminalIDNumber)
Dim credentials As String = Base64$(Global.PS_USER & ":" & Global.PS_PASS)
With Request
.Proxy = Null
.Headers = Format("Basic \{0}", credentials)
.UserAgent = Global.PS_USER
End With
If Request.Status < 0 Then
Print "ERROR"
Else
' Success - read the data
If Lof(Request) Then sBuffer = Read #Request, Lof(Request)
Print sBuffer
End If
'If responseFromServer = "0" Then
' MsgBox("Retreal of Status Failed")
'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
' If DontSendDataToPoS = 0 Then SendToPoSterminal("TerminalOnLine|")
' Me.ListBox1.Items.Clear
' addtoStatusList("Terminal Ready")
'
' Case "BUSY"
' If DontSendDataToPoS = 0 Then SendToPoSterminal("TerminalBusy|")
' addtoStatusList("Terminal busy please wait 10 seconds and try again")
'
' Case "Offline", "OFFLINE", "offline"
' If DontSendDataToPoS = 0 Then SendToPoSterminal("offline|")
' addtoStatusList("Terminal OFFLINE NO Card processing Possible - Please check with Payment Sense")
' End Select
' End Select
' Next
' End If
Hi Everyone,
I have now spent 2 days trying to convert this simple function to Gambas (once I can get this done I should be able to get the other more complicated function converted)
but no matter what I tried to do or google i get nothing zip sod all (and I get errors on the Dim Request As HttpClient line)
can someone more smarter then me help me convert this so it works?
I will need to get the JSON support working as well as that is the format the data is returned from the Web request.
Thank-you in advice for any help
Andy
Posted
Guru

Dim Request As New HttpClient
Request.URL = Global.PS_URL & "/terminals/" & Global.TerminalIDNumber
Posted
Enthusiast

BruceSteers said
Try something like …
Dim Request As New HttpClient
Request.URL = Global.PS_URL & "/terminals/" & Global.TerminalIDNumber
I tried that and now I am getting Null object (PSConnect:14) on the Request.URL line
but the URL is being made correclty (https://**.*.*.paymentsense.cloud/*/terminals/22165264) (some of the address have been removed for security)
Am I doing this completely wrong?
Posted
Guru

Posted
Guru

AndyGable said
BruceSteers said
Try something like …
Dim Request As New HttpClient
Request.URL = Global.PS_URL & "/terminals/" & Global.TerminalIDNumber
I tried that and now I am getting Null object (PSConnect:14) on the Request.URL line
but the URL is being made correclty (https://**.*.*.paymentsense.cloud/*/terminals/22165264) (some of the address have been removed for security)
Am I doing this completely wrong?
Possibly.
if you look at /comp/gb.net.curl/httpclient - Gambas Documentation
making a new httpclient object and using URL is correct.
your URL must have the null object in it
Try …
Debug Global.PS_URL
Debug Global.TerminalIDNumber
PS. use &/
not just & and then putting / in the strings as using &/ does / automatically if needed or not
Posted
Guru

Posted
Enthusiast

I have tired what you all recommend and I can say for sure the URL is being created correctly beacuse if I click it I get the following error message on the website
Code
\{"messages":\{"error":["Missing authorization credentials."]}}The url it is createing is
Code
https://test.connect.paymentsense.cloud/pac/terminals/123456789(the Terminal Number is a fake one but this is the URL it makes)
This is my code so far
Code
Dim ReqURL As String = Global.PS_URL & "/terminals/" & Str(Global.TerminalIDNumber)
Print ReqURL
Dim sBuffer As String
Dim Request As HttpClient
Dim credentials As String = Base64$(Global.PS_USER & ":" & Global.PS_PASS)
Request.URL = "" & ReqURL & ""
Request.Headers = Format("Basic \{0}", credentials)
Request.UserAgent = Global.PS_USER
Request.Get
If Request.Status < 0 Then
Print "ERROR"
Else
' Success - read the data
If Lof(Request) Then sBuffer = Read #Request, Lof(Request)
Print sBuffer
End If
and I have the following componets
<LIST>
- <LI>
- gb.form</LI>
<LI> - gb.gui.qt</LI>
<LI> - gb.image</LI>
<LI> - gb.net</LI>
<LI> - gb.net.curl</LI>
<LI> - gb.settings</LI>
<LI> - gb.utl.web</LI>
<LI> - gb.web</LI>
am I missing any?
Posted
Guru

have you tried setting Request.Password and Request.User ? (maybe before setting URL?)
also there is simply using HttpClient.Download ( URL As String , Headers As String[] ) As String
also .Headers is String[] should be in angle brackets even if only one item
So it should be Request.Headers = [one_header, another_header]
Also your use of Format() does not make sense looking at /lang/format - Gambas Documentation
Posted
Enthusiast

I have made some progress now but i am getting Error code 6 (Unable to Resolve Host address) but when I click on the url and it
opens in Chrome I can see the webpage
I have even tried to change it to Google just to test it and I am still getting the same error message.
Any ideas what I need to do
Code
Dim sBuffer As String
Dim Request As HttpClient
Request = New HttpClient As "Request" <<---- MISSED THIS LINE OFF the code (once I saw it on the example I added it)
With Request
.User = Base64$(Global.PS_USER)
.Password = Base64$(Global.PS_PASS)
.URL = "" & Global.PS_URL & "/terminals/" & Str(Global.TerminalIDNumber) & ""
.Get
End With
Print Request.Status
If Request.Status > 0 Then
HttpRequestErrorCode(Request.Status)
Else
' Success - read the data
If Lof(Request) Then sBuffer = Read #Request, Lof(Request)
Print sBuffer
End If
Posted
Enthusiast

I have spoken to my Web Service API supplier and they confirm the Call is reaching their end but I am not sending the correct data that is why I am getting the Error code 6 (401 on thier end)
What I need to send to them is the following
Content-Type header = application/json
Accept header (API version) = application/connect.v2+json
Authorization header
Software-House-Id and Installer-Id headers
Host address – all ready working
any idea what options I need to use on the httpclient so the data can be sent?
So far I have the following
Code
With Request
.Auth = 1 ' BASIC authorization
.User = Base64$(Global.PS_USER)
.Password = Base64$(Global.PS_PASS)
.URL = Global.PS_URL & "/terminals/" & Str(Global.TerminalIDNumber)
.Get
End With
Any advice is welcomed as once I have this single connection working i can port the rest of my application over to Linux
Posted
Trainee
Posted
Enthusiast

molposedra said
I have often encountered this problem when working with VB. For me, this platform seems somehow incomprehensible and strange.
Hi molposedra,
I'm finding it hard at the moment to migrate from VB.net to Gambas (well the web function side of Gambas anyway)
But I've yet to find any good example website yet (and all the examples that I have seen are for C)
But I've been using VB since vb4 (so windows 3.11) and I've migrated some of my apps to Gambas as windows 11 is so demanding for memory and cpu etc that it will not run on older hardware. (And my software is aimed at older hardware i386 processors etc)
Posted
Enthusiast

I converted several VB projects over but ended up mostly rewriting them because of the complexity of VB that is no longer needed. There is a Gambas mailing list that is great for questions that you can't get answered. The developers monitor and respond in that. They are very good about helping. the address is user-request@lists.gambas-basic.org
If you can't get help here try that.
1 guest and 0 members have just viewed this.



