How to Work with Web Service in Fore?

Description

PP.SOM service is mostly used to develop web application based systems. Somehow it may be required to address the web service from desktop application based assemblies, forms or units. The Fore language interacts with various network resources via POST/GET requests using the NetHttpService class of the Net assembly. The requests have the in SOAP format by default. If JSON requests are to be executed, add the header "Content-Type: application/json; charset=utf-8" to the INetHttpCommand.Headers collection and set the INetHttpCommand.OuterBody property to True.

To work with the web service:

  1. Create an instance of the NetHttpService class, specify in the Url property the web service URL and execute the Connect method. The rules for creating a web service URL are given in the Connecting to Web Service subsection.

  2. Cast the obtained object described by the INetCommand interface to the INetHttpCommand interface and determine settings to work with the web service: Command = NetHttpCommandType.Post.

  3. Create a SOAP request to execute the required operation. Description of operations and examples of their use are given in the Web Service Operations subsection.

  4. Send the created request to the Start method to send the request to the web service.

  5. The Result property will contain the web server response.

Example

Below are the examples of connecting to the PP.SOM web service and sending repository connection and disconnection requests. After the connection is established successfully, the development environment console will display text of the request, response and the moniker, via which all the further work with the repository is executed. After the work is finished, the repository is disconnected. Description identifier for the repository, to which the connection is established, and the user and password are sent in parameters of the specified procedures.

Sub TestSOAPRequest(DefinitionID: String; User: String; Password: String);
Var
    Service: INetHttpService;
    Cmd: INetCommand;
    HttpCmd: INetHttpCommand;
    Request: Variant;
    Response, Moniker: String;
Begin
    Service := New NetHttpService.Create;
    Service.Url := "http://localhost:9090/axis2/services/PP.SOM.Som";
    // Connect to web service
    Cmd := Service.Connect;
    HttpCmd := Cmd As INetHttpCommand;
    HttpCmd.Command := NetHttpCommandType.Post;
    HttpCmd.AcceptLanguage := "ru";
    // Connect to repository
    Request :=
        "<OpenMetabase>" +
            "<tDef><id>" + DefinitionID + "</id></tDef>" +
            "<tCreds><user><id>" + User + "</id></user><pass>" + Password + "</pass></tCreds>" +
        "</OpenMetabase>" ;
    Cmd.Start(Request);
    Response := Cmd.Result As String;
    Moniker := Response.SubString(Response.IndexOf("<id>") + 4, Response.IndexOf("</id>") - Response.IndexOf("<id>") - 4);
    // View request, response and obtained moniker
    ShowText("Request", Request);
    ShowText("Response", Response);
    ShowText("Moniker", Moniker);
    //...
    // Further work with repository
    //...
    // Disconnect from repository
    Request := "<CloseMetabase><tMb><id>" + Moniker + "</id></tMb></CloseMetabase>";
    Cmd.Start(Request);
    Response := Cmd.Result As String;
    // View request and response
    ShowText("Request", Request);
    ShowText("Response", Response);
End Sub TestSOAPRequest;

Sub ShowText(Title: String; Text: String);
Begin
    Debug.WriteLine("---" + Title + "---");
    Debug.Indent;
    Debug.WriteLine(Text);
    Debug.Unindent;
End Sub ShowText;

Sub TestJsonRequest(DefinitionID: String; User: String; Password: String);
Var
    Service: INetHttpService;
    Cmd: INetCommand;
    HttpCmd: INetHttpCommand;
    Request: Variant;
    Response, Moniker: String;
    Start, End_: Integer;
Begin
    Service := New NetHttpService.Create;
    Service.Url := "http://localhost:9090/axis2/services/PP.SOM.Som";
    // Connect to web service
    Cmd := Service.Connect;
    HttpCmd := Cmd As INetHttpCommand;
    HttpCmd.Command := NetHttpCommandType.Post;
    HttpCmd.AcceptLanguage := "ru";
    // Header for setting format of sent request
    HttpCmd.Headers.Add("Content-Type: application/json; charset=utf-8");
    // Cancel linking of mandatory SOAP headers
    HttpCmd.OuterBody := True;
    // Connect to repository
    Request := "{""OpenMetabase"":" +
        "{""tDef"":{""id"":""" + DefinitionID + """}," +
        """tCreds"":{""user"":{""id"":""" + User + """},""pass"":""" + Password + """},""tArg"":""""}}";
    Cmd.Start(Request);
    Response := Cmd.Result As String;
    Start := Response.IndexOf("""id"": ") + 7;
    End_ := Response.IndexOf("!M", Start);
    Moniker := Response.SubString(Start, End_ - Start + 2);
    // View request, response and obtained moniker
    ShowText("Request", Request);
    ShowText("Response", Response);
    ShowText("Moniker", Moniker);
    //...
    // Further work with repository
    //...
    // Disconnect from repository
    Request := "{""CloseMetabase"":{""tMb"":{""id"":""" + Moniker + """}}}";
    Cmd.Start(Request);
    Response := Cmd.Result As String;
    // View request and response
    ShowText("Request", Request);
    ShowText("Response", Response);
End Sub TestJsonRequest;

Sub ShowText(Title: String; Text: String);
Begin
    Debug.WriteLine("---" + Title + "---");
    Debug.Indent;
    Debug.WriteLine(Text);
    Debug.Unindent;
End Sub ShowText;

See also:

Questions and Answers