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 does not contain classes required to work with web services that is why the work can be organized only via the combination Fore.NET-Fore. The HttpWebRequest/HttpWebResponse (.NET Framework, System.NET namespaces) are used to send queries and get responses from web service. Below is the example of static function in Fore.NET that enables the user to send JSON requests and get web service responses.
Public Class SampleWebClient
Public Shared Function JsonHttpRequest(urlString: string; jsonContent: string): string;
Var
response: String;
wRequest: HttpWebRequest;
wResponse: HttpWebResponse;
enc: Encoding;
bytes: Array Of Byte;
requestStream, responseStream: Stream;
reader: StreamReader;
Begin
//Create an object for sending web service request and getting response
wRequest := HttpWebRequest.CreateHttp(urlString);
Try
bytes := System.Text.Encoding.ASCII.GetBytes(jsonContent);
//Setting parameters
wRequest.Method := "POST";
wRequest.ContentLength := bytes.Length;
wRequest.ContentType := "application/json;charset=UTF-8";
//Record request
requestStream := wRequest.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
//Send request and get response
wResponse := wRequest.GetResponse() As HttpWebResponse;
//If request is executed successfully, read response
If wResponse.StatusCode = HttpStatusCode.OK Then
responseStream := wResponse.GetResponseStream();
reader := New StreamReader(responseStream);
response := reader.ReadToEnd();
End If;
//Close object and free resources
wResponse.Close();
Except On we: WebException Do
Begin
System.Diagnostics.Debug.WriteLine(we.Message)
End;
On ex: Exception Do
Begin
System.Diagnostics.Debug.WriteLine(ex.Message)
End;
Finally
wResponse.Close();
End Try;
Return response;
End Function;
End Class;
The following code sends a JSON request for connecting to repository and displays web service response in the development environment console:
Public Shared Sub Main(Params: StartParams);
Var
request: string;
response: string;
Begin
request := "{ ""OpenMetabase"": { ""tDef"": { ""id"": ""PPREPOSITORY"" } , ""tCreds"": { ""user"": { ""id"": ""pprepository"" } , ""pass"": ""pprepository"" } } }";
response := JsonHttpRequest("http://localhost:9090/axis2/services/PP.SOM.Som", request);
System.Diagnostics.Debug.WriteLine(response);
End Sub;
To send a JSON request from the code in the Fore, use resources of the ForeNet assembly:
Sub TestJsonRequest;
Var
MB: IMetabase;
Run: IForeNETRuntime;
Asm: IForeNETRuntimeAssembly;
Typ: IForeNETRuntimeType;
response: Variant;
url, request: String;
Begin
url := "http://localhost:9090/axis2/services/PP.SOM.Som";
request := "{ ""OpenMetabase"": { ""tDef"": { ""id"": ""PPREPOSITORY"" } , ""tCreds"": { ""user"": { ""id"": ""pprepository"" } , ""pass"": ""pprepository"" } } }";
MB := MetabaseClass.Active;
Run := ForeNETAssemblyClass.Runtime;
Asm := Run.Assembly(MB.ItemById("SampleAssembly").Bind As IForeNETAssembly);
Typ := Asm.Type("SampleAssembly.SampleWebClient");
response := Typ.InvokeMethod("JsonHttpRequest", Null, url, request);
Debug.WriteLine(response);
End Sub TestJsonRequest;
See also: