PB.Ex Network (Windows)

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

PB.Ex Network (Windows)

Post by RSBasic »

Hello,

I have developed a DLL library to send a JSON string via POST to an HTTP and HTTPS website. For example, using Deepl.com to translate texts.
You can also complete and execute any form via POST.

Functions:
  • SendNetworkFormFields()
    • Syntax:

      Code: Select all

      Result = SendNetworkFormFields(URL$, Array, ArraySize, @Output$, @ErrorOutput)
    • Description: Sends an array with the specified fields to a form of a website via POST.
    • Parameter:
      1. URL$: The destination address of the website where the data is to be sent.
      2. Array: A string array with the individual fields is sent to the website. Field name and field value must be stored separately.
      3. ArraySize: Number of elements.
      4. @Output$: The return value from the website is stored in the string variable.
      5. @ErrorOutput: If an error occurs, the error message is stored in the string variable.
    • Return value:
      • 1: The process was successful.
    • Example:

      Code: Select all

      EnableExplicit
      
      Global PBEx_Network
      
      CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
        PBEx_Network = OpenLibrary(#PB_Any, "PB.Ex_Network_x86.dll")
      CompilerElseIf #PB_Compiler_Processor = #PB_Processor_x64
        PBEx_Network = OpenLibrary(#PB_Any, "PB.Ex_Network_x64.dll")
      CompilerEndIf
      
      If PBEx_Network
        Prototype SendNetworkFormFields(URL.p-Unicode, Array, ArraySize, Output, ErrorOutput)
        Global SendNetworkFormFields.SendNetworkFormFields = GetFunction(PBEx_Network, "SendNetworkFormFields")
        Prototype SendNetworkJSON(URL.p-Unicode, JSON.p-Unicode, Output, ErrorOutput)
        Global SendNetworkJSON.SendNetworkJSON = GetFunction(PBEx_Network, "SendNetworkJSON")
        
        Define Output$ = Space(100000)
        Define ErrorOutput$ = Space(1024)
        Define StartPosition
        Define EndPosition
        Dim MyArray.s(3)
        
        MyArray(0) = "firstname"
        MyArray(1) = "Pure"
        MyArray(2) = "lastname"
        MyArray(3) = "Basic"
        
        SendNetworkFormFields("https://www.w3schools.com/action_page.php", MyArray(), ArraySize(MyArray())+1, @Output$, @ErrorOutput$)
        StartPosition = FindString(Output$, "<h2>")
        EndPosition = FindString(Output$, "</div>")
        Debug Mid(Output$, StartPosition, EndPosition-StartPosition)
        
        CloseLibrary(PBEx_Network)
      EndIf
  • SendNetworkJSON()
    • Syntax:

      Code: Select all

      Result = SendNetworkJSON(URL$, JSON$, @Output$, @ErrorOutput)
    • Description: Sends a JSON string to a website via POST.
    • Parameter:
      1. URL$: The destination address of the website where the data is to be sent.
      2. JSON$: A string in JSON format is sent to the website.
      3. @Output$: The return value from the website is stored in the string variable.
      4. @ErrorOutput: If an error occurs, the error message is stored in the string variable.
    • Return value:
      • 1: The process was successful.
    • Example:

      Code: Select all

      EnableExplicit
      
      Global PBEx_Network
      
      CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
        PBEx_Network = OpenLibrary(#PB_Any, "PB.Ex_Network_x86.dll")
      CompilerElseIf #PB_Compiler_Processor = #PB_Processor_x64
        PBEx_Network = OpenLibrary(#PB_Any, "PB.Ex_Network_x64.dll")
      CompilerEndIf
      
      If PBEx_Network
        Prototype SendNetworkFormFields(URL.p-Unicode, Array, ArraySize, Output, ErrorOutput)
        Global SendNetworkFormFields.SendNetworkFormFields = GetFunction(PBEx_Network, "SendNetworkFormFields")
        Prototype SendNetworkJSON(URL.p-Unicode, JSON.p-Unicode, Output, ErrorOutput)
        Global SendNetworkJSON.SendNetworkJSON = GetFunction(PBEx_Network, "SendNetworkJSON")
        
        Define Output$ = Space(1024)
        Define ErrorOutput$ = Space(1024)
        Define JSON$
        Define CurrentJSON
        Define Sentence$ = "Hello world"
        Define FromLang$ = "EN"
        Define ToLang$ = "DE"
        
        JSON$ + ~"{"
        JSON$ + ~"   \"jsonrpc\":\"2.0\","
        JSON$ + ~"   \"method\":\"LMT_handle_jobs\","
        JSON$ + ~"   \"params\":{"
        JSON$ + ~"      \"jobs\":["
        JSON$ + ~"         {"
        JSON$ + ~"            \"kind\":\"Default\","
        JSON$ + ~"            \"raw_en_sentence\":\"" + Sentence$ + ~"\""
        JSON$ + ~"         }"
        JSON$ + ~"      ],"
        JSON$ + ~"      \"lang\":{"
        JSON$ + ~"         \"user_preferred_langs\":["
        JSON$ + ~"            \"EN\","
        JSON$ + ~"            \"PL\","
        JSON$ + ~"            \"NL\""
        JSON$ + ~"         ],"
        JSON$ + ~"         \"source_lang_user_selected\":\"" + FromLang$ + ~"\","
        JSON$ + ~"         \"target_lang\":\"" + ToLang$ + ~"\""
        JSON$ + ~"      },"
        JSON$ + ~"      \"priority\":-1"
        JSON$ + ~"   },"
        JSON$ + ~"   \"id\":15"
        JSON$ + ~"}"
        
        SendNetworkJSON("https://www.deepl.com/jsonrpc", JSON$, @Output$, @ErrorOutput$)
        If ParseJSON(1, Output$)
          CurrentJSON = JSONValue(1)
          CurrentJSON = GetJSONMember(CurrentJSON, "result")
          CurrentJSON = GetJSONMember(CurrentJSON, "translations")
          CurrentJSON = GetJSONElement(CurrentJSON, 0)
          CurrentJSON = GetJSONMember(CurrentJSON, "beams")
          CurrentJSON = GetJSONElement(CurrentJSON, 0)
          CurrentJSON = GetJSONMember(CurrentJSON, "postprocessed_sentence")
          Debug GetJSONString(CurrentJSON)
          
        EndIf
        
        CloseLibrary(PBEx_Network)
      EndIf
System requirements:
  • Windows XP or higher
  • .NET Framework 4 or higher
  • Unicode activation (standard from PB 5.50)
Since the required. NET Framework is preinstalled by default from Windows 7 and can be installed on XP and Vista, this shouldn't be a problem to use this version.
No assembly registration with regasm.exe with administrator rights is necessary as with COMatePLUS.

Licence: This DLL file is free of charge and may be used both privately and commercially.
The following copyright texts must be provided:
Copyright © 2019 RSBasic.de
Download: https://www.rsbasic.de/downloads/downlo ... etwork.zip
Image

I would be happy to receive feedback, suggestions for improvement, bug reports or requests. If you want to support me, you can also donate me a little something. Thank you :)
Image
Image
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: PB.Ex Network (Windows)

Post by infratec »

Why not with libcurl?
Then it would be cross-platform and no need for any other framework.
And it is, more or less, inbuild.

Bernd
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: PB.Ex Network (Windows)

Post by RSBasic »

PB.Ex Network 1.0.1.0 has been released.

Changelog:
  • Changed: The return value on error has been changed from -1 to 0.
  • Changed: The target .NET framework has been changed from 3.5 to 4.0.
  • Added: Parameter "ErrorOutput" was added.
Image
Image
Post Reply