Help Converting From VB.net to Gambas

Post

Posted
Rating:
#1 (In Topic #693)
Enthusiast
AndyGable is in the usergroup ‘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
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
 Try something like …

Dim Request As New HttpClient
Request.URL = Global.PS_URL & "/terminals/" & Global.TerminalIDNumber
Online now: No Back to the top

Post

Posted
Rating:
#3
Enthusiast
AndyGable is in the usergroup ‘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?
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Guru
cogier is in the usergroup ‘Guru’
Have you added the gb.net.curl Component? 'HttpClient' won't work without it. You will also need gb.web for JSON decoding.
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘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
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
 If  Global.TerminalIDNumber is not a string you must use Str(Global.TerminalIDNumber)
Online now: No Back to the top

Post

Posted
Rating:
#7
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
Hi Everyone

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>
</LIST>
am I missing any?
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’
no your error is in passing the credentials.

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
Online now: No Back to the top

Post

Posted
Rating:
#9
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
Hi Everyone,

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
Online now: No Back to the top

Post

Posted
Rating:
#10
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
Hi Everyone,

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
Online now: No Back to the top

Post

Posted
Rating:
#11
Trainee
I have often encountered this problem when working with VB. For me, this platform seems somehow incomprehensible and strange. In addition, Google very often conflicts with VB, and because of this, I have to rewrite the seemingly simple code several times. The only thing I can suggest you solve this problem is to try to use Google with a proxy server. For example, I use malaysia proxies which provide an anonymous and stable connection. With proxies also, Google will swear less on unknown sites. I would be interested to know if this helps you. I wish you good luck!
Online now: No Back to the top

Post

Posted
Rating:
#12
Enthusiast
AndyGable is in the usergroup ‘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)
Online now: No Back to the top

Post

Posted
Rating:
#13
Avatar
Enthusiast
sadams54 is in the usergroup ‘Enthusiast’
I worked in VB since version 6. MS does so many things in strange ways that make no sense and leave opening for many errors. The thing I keep running into is I still think complex like MS. In Gambas you must think simple because it works.

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.
Online now: No Back to the top
1 guest and 0 members have just viewed this.