Announcement

Collapse
No announcement yet.

Using a vbscript code segment hrough PB Win 9.03

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using a vbscript code segment hrough PB Win 9.03

    I have a vbscript code segment that I wish to access from PB Win.

    The segment is:

    EnableScript("VBScript");

    <%

    Public Sub callurl(varname,val)
    Dim oXMLHTTP
    Dim oStream
    Set oXMLHTTP = CreateObject("Msxml2.ServerXMLHTTP")

    token="xxmmfkfh58547n5ign" // dummy value not actual

    url = "https://api.xx.xx/api?auth-token="+token+"&key="+varname+"&value="+val

    oXMLHTTP.Open "GET", url, False
    oXMLHTTP.setRequestHeader "Cache-Control", "no-cache"
    oXMLHTTP.setRequestHeader "Pragma", "no-cache"
    oXMLHTTP.Send
    End Sub
    %>

    Then the calling program (Amibroker) does this:

    pb = GetScriptObject();

    if <some condition>
    pb.callurl("NIFTY","2");

    That's it. I assume I can write code in PBWin to call the vbscript Sub: callurl

    But I am at a blank when it comes to internet programming. Could you nudge me in the right direction, please.

    added: If there is some way to do this work without vbscript, please suggest.

    Thanks,

    Sudarshan Sukhani



  • #2
    Looks like a simple HTTP Get, but there seems to be some code missing.
    I don't see what that sub does with the response from the web server and you are not doing anything with oStream.

    You should be able to replace that VBScript with something like the following PowerBASIC code:
    '
    Code:
    #COMPILE EXE ' Roca Includes!
    #DIM ALL
    #INCLUDE ONCE "httprequest.inc"
    #INCLUDE ONCE "ole2utils.inc"
    ...
    FUNCTION GetURL (strName AS STRING,strVal AS STRING) AS STRING
        LOCAL pWHttp AS IWinHttpRequest
        LOCAL strBuffer AS STRING
        LOCAL strToken STRING
        LOCAL sURL AS STRING
        LOCAL iSucceeded AS INTEGER
    
        strToken ="xxmmfkfh58547n5ign"
        strURL = url = "https://api.xx.xx/api?auth-token=" & strToken & "&key=" & strName & "&value=" & strVal
    
        pWHttp = NEWCOM "WinHttp.WinHttpRequest.5.1"
        IF ISNOTHING(pWHttp) THEN EXIT FUNCTION
        TRY
            pWHttp.Open "GET", strURL
            pWHttp.Send
            iSucceeded = pWHttp.WaitForResponse(5)
            buffer = pWHttp.Responsetext
        CATCH
            OleShowErrorInfo OBJRESULT
        END TRY
        FUNCTION = strBuffer
    END FUNCTION
    '
    A forum search on "MsXml2.ServerXMLHTTP.6.0 will also give you a number of code examples using that rather than the WinHttp.WinHttpRequest.5.1 above.

    Comment


    • #3
      Thanks, Stuart. The code is complete. The URL refers to a trading website where I have an account. I pass on a unique token + Key + value and the website executes a trade mentioned in the key. The value is usually 1- for buy, 2 for sell. Thank you for your help. I will work on it.

      Comment


      • #4
        I tried searching for these 2 files but did not locate them in the forum. Please give me directions on where to get them
        "httprequest.inc"
        "ole2utils.inc"

        Okay, I got these files from the Roca download section in this forum.

        Comment


        • #5
          Originally posted by Sudarshan Sukhani View Post
          Thanks, Stuart. The code is complete. The URL refers to a trading website where I have an account. I pass on a unique token + Key + value and the website executes a trade mentioned in the key. The value is usually 1- for buy, 2 for sell. Thank you for your help. I will work on it.
          In that case, it should really be using a POST, not a GET!

          You need to read the details of GET and POST here:
          W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

          Comment


          • #6
            I picked up some Python code from the web and it worked the first time. The code sent the appropriate message to the trading website, which did the required paper trade.

            Code:
            import urllib.request
            token="dummy"
            varname = "dummy"
            val = "2"
            myurl = "https://api.website.com/api?auth-token="+token+"&key="+varname+"&value="+val
            webUrl = urllib.request.urlopen(myurl)
            print ("result code: " + str(webUrl.getcode()))
            '========================================================================

            the following PB code gives error: URL does not use an authorized protocol

            Code:
            FUNCTION GetURL (strName AS STRING,strVal AS STRING) AS STRING
            LOCAL pWHttp AS IWinHttpRequest
            LOCAL strBuffer AS STRING
            LOCAL strToken AS STRING
            LOCAL strURL AS STRING
            LOCAL iSucceeded AS INTEGER
            
            strToken="dummy"
            
            strURL = "https://api.website.com/api?auth-token=" & strToken & "&key=" & strName & "&value=" & strVal
            'strURL = "http://powerbasic.com"
            
            pWHttp = NEWCOM "WinHttp.WinHttpRequest.5.1"
            IF ISNOTHING(pWHttp) THEN EXIT FUNCTION
            TRY
            pWHttp.Open "POST", strURL ' tried with GET also. same error
            pWHttp.Send
            iSucceeded = pWHttp.WaitForResponse(5)
            strbuffer = pWHttp.Responsetext
            CATCH
            OleShowErrorInfo OBJRESULT
            END TRY
            FUNCTION = strBuffer
            END FUNCTION
            ===================================================================

            An example given in the samples section also gives an error:

            Code:
            FUNCTION PBMAIN() AS LONG
            
            LOCAL Buffer$, Site$, File$
            
            LOCAL Entire_page$, Htmlfile$, Link$
            
            LOCAL Pos&, Length&
            
            LOCAL token AS STRING, varname AS STRING, myval AS STRING, myurl AS STRING
            
            token="Dummy"
            varname = "Dummy"
            myval = "1"
            
            myurl = "https://api.website.com/api?auth-token=" & token & "&key=" & varname & "&value=" & myval
            
            Site$ = myurl
            
            ' Connecting...
            
            TCP OPEN "http" AT Site$ AS #1 TIMEOUT 60000
            
            ' Could we connect to site?
            
            IF ERR THEN
            
            MSGBOX "error"
            
            EXIT FUNCTION
            
            END IF
            
            MSGBOX "opened"
            
            
            ' Close the TCP/IP port...
            
            TCP CLOSE  #1
            
            END FUNCTION
            ==============================================================

            I am assuming I am making some mistakes since I do not know internet programming at all.

            One solution is to make a PB DLL which will call the python script. The DLL will be called by my trading software.

            There must be a direct call to PB possible. If any help on the code is possible, please provide.

            Thanks a ton!

            Comment


            • #7
              TCP OPEN "http" AT Site$ AS PowerBASIC TIMEOUT 60000
              Opens port 80, but URL is to HTTPS: which is port 443 (if I remember correctly) It also adds a security layer. (edited after next post)
              There must be a direct call to PB possible.
              No, but there is an API call that is not difficult from PB. (I've never done it, someone will jump in with it)

              Cheers,
              Last edited by Dale Yarker; 27 Jul 2022, 10:11 AM.
              Dale

              Comment


              • #8
                Originally posted by Dale Yarker View Post
                Opens port 80, but URL is to HTTPS: which is port 443 (if I remember correctly) It adds a security layer.
                ,
                There's a lot more th HTTPS than just changing the oprt. The system needs to establish a secure, encrypted connection before exchanging any other data. There are a number of other issues with that TCP code that I won't bother to go into since it won't work with HTTPS

                As for POST v GET, you can't just replace th GET with POST in the previous code. If you read the link in Post5, you will see that you need to handle POST ini an entirely idfferent way - the paramters are NOT part of the URL with a POST.

                "URL does not use an authorized protocol" suggests that the website either required HTTP/2 or HTTP3 and or requires TLS1.2 and WInHTTP is connecting with a lower protocol What version of Windows are you using?

                Using WinHTTP, you probably need to use WINHTTP_OPTION_ENABLE_HTTP_PROTOCOL and/or WINHTTP_OPTION_SECURE_PROTOCOLS

                See https://docs.microsoft.com/en-us/win...nhttpsetoption
                and
                The following option flags are supported by [**WinHttpQueryOption**](/windows/win32/api/Winhttp/nf-winhttp-winhttpqueryoption) and [**WinHttpSetOption**](/windows/win32/api/Winhttp/nf-winhttp-winhttpsetoption).


                Alternatively, search the forums for one of the examples of using "MsXml2.ServerXMLHTTP.6.0"


                Comment


                • #9
                  Stuart, Thank you for your time and effort. I really appreciate it. It now appears that I cannot run a 32-bit DLL from my 64-bit charting software. Therefore, even if I did get PB to get the URL I would not be able to call it from my software.

                  Comment


                  • #10
                    Can your charting software read from the clipboard, a file or get a windows message?
                    Went from trading software to charting software. Also, it appears https security is needed.


                    The world is full of apathy, but who cares?

                    Comment


                    • #11
                      Originally posted by Mike Doty View Post
                      Can your charting software read from the clipboard, a file or get a windows message?
                      Went from trading software to charting software. Also, it appears https security is needed.
                      Also suddenly introduced DLL from a 64bit application

                      Comment


                      • #12
                        Charting software creates Signals -----> PB code receives the signals, uploads to web ------------------> Trading software on web receives the signals, executes the trade.

                        How do I communicate from my charting software to PB? through a DLL (not possible) or through writing the signals in a file which PB can pick up.

                        The question I asked here was how to upload to the web using PB. This was achieved with a few lines of python code which I shared in my posts.

                        While Stuart did try to help a lot, my limited understanding could not grasp the subject. My fault.

                        I sincerely apologize for having asked what now appears to have been an incomplete or even foolish question.

                        Comment


                        • #13
                          Charting software creates Signals ......How do I communicate from my charting software to PB?
                          In what form are these signals available? Windows' events? Writes to a file? Issues a callback?

                          Michael Mattias
                          Tal Systems (retired)
                          Port Washington WI USA
                          [email protected]
                          http://www.talsystems.com

                          Comment


                          • #14
                            Michael, thank you for taking interest. I have been reading and following you here for many years!
                            The signals are available in the scripting language which is part of the charting software. I can write them to a file, or call a DLL.
                            This was never the problem.
                            I wished to know how to send an URL to the big web.
                            This is the python code that does that. (I gave this code earlier).
                            Code:
                            import urllib.request
                            token="dummy"
                            varname = "dummy"
                            val = "2"
                            myurl = "https://api.website.com/api?auth-token="+token+"&key="+varname+"&value="+val
                            webUrl = urllib.request.urlopen(myurl)
                            print ("result code: " + str(webUrl.getcode()))
                            I have made a workaround:
                            In the charting software I write to a file. This is done every 30 minutes.
                            A small python code reads the file every 30 minutes and then sends the URL using the code mentioned above.

                            I wanted to know how to send this URL in PB. I could not understand the answers.

                            added through edit: I use Windows 11. I am not windows programmer by any chance!

                            Thanks.

                            Comment


                            • #15
                              Earlier post by you: https://forum.powerbasic.com/forum/u...r-learn-python
                              Might send an email to Sockettools. They also have a test site which I stumbled upon the other day.

                              The closest thing I've seen to do without an extra library is on Jose Roca's site and I'll try to find it.
                              The world is full of apathy, but who cares?

                              Comment


                              • #16
                                Jose Roca WinHttp examples in his subforum on this board.
                                Flat api example are further down.
                                If you get your credentials to work should do it.
                                https://forum.powerbasic.com/forum/j...source-code-ab

                                Have code to tap into .NET from PowerBASIC, but haven't yet used since not a .NET person.
                                Probably shouldn't have mentioned this, but in the right hands could be very useful.
                                The world is full of apathy, but who cares?

                                Comment


                                • #17
                                  Originally posted by Mike Doty View Post
                                  Jose Roca WinHttp examples in his subforum on this board.
                                  Flat api example are further down.
                                  If you get your credentials to work should do it.
                                  https://forum.powerbasic.com/forum/j...source-code-ab

                                  Have code to tap into .NET from PowerBASIC, but haven't yet used since not a .NET person.
                                  Probably shouldn't have mentioned this, but in the right hands could be very useful.
                                  See Post 8 and :
                                  "URL does not use an authorized protocol" suggests that the website either required HTTP/2 or HTTP3 and or requires TLS1.2

                                  Then note from your link above:

                                  The following features have been added in version 5.1 of WinHTTP:
                                  • HTTP/1.0 protocol, including support for keep-alive (persistent) connections and session cookies.
                                  • HTTP/1.1 chunked transfer support for HTTP responses
                                  • ...
                                  • Secure Sockets Layer (SSL) functionality, including client certificates. Supported SSL protocols include the following: SSL 2.0, SSL 3.0, and Transport Layer Security (TLS) 1.0.
                                  IOW, the website is probably not going to work with WinHTTP.

                                  Comment


                                  • #18
                                    Thanks for the explanation.
                                    I remember not having any luck, but hoped somebody else would.
                                    I'd probably repurchase sockettools with a PowerBASIC example of client/server.
                                    I've wanted to put TLS support into SQLitening for years.
                                    The world is full of apathy, but who cares?

                                    Comment


                                    • #19
                                      "IOW, the website is probably not going to work with WinHTTP."

                                      It would be easier to check if we had the actual web site link, I can understand not giving the "token" and "varname", but we could get more precise info from the actual server. Now we are just guessing.

                                      Comment


                                      • #20
                                        The signals are available in the scripting language which is part of the charting software. I can write them to a file, or call a DLL.
                                        I would probably create a named Windows Event and signal it. You may have to create a DLL to hold a procedure to call the OpenEvent(), SetEvent() sequence but this is really straightforward. Now your PB program can set up a separate thread of execution to wait on that event being signaled.

                                        I'm sure I have demos in the source code forum. Search on my name as thread starter and "event" in the thread title and you should find some good starter code.
                                        Michael Mattias
                                        Tal Systems (retired)
                                        Port Washington WI USA
                                        [email protected]
                                        http://www.talsystems.com

                                        Comment

                                        Working...
                                        X