Tech Articles


Call WCF Services Using HTTPClient or RESTClient Objects


Background

Prior to PowerBuilder 2017 R2, the SoapClient did not support calling WCF Services.

In PowerBuilder 2017 R2 and the latter versions, the HTTPClient object is available, through which you can call WCF Services.

In PowerBuilder 2019, the RESTClient object is strengthened, now you can also call the WCF Services.

 

How to use HTTPClient object to consume a WCF Service

      a. To get the methods and parameters to consume the WCF Service.

Normally when you open a WCF Service website, you will get the following page:

However, you would still need the documentation provided by the WCF Service and also need to verify if the current WCF Service supports XML format parameters in order to consume this WCF using URL or through a client. If the current service provider has provided the online help, you can use the WCF Service address + "/help" to obtain the corresponding API descriptions and the parameters as follows:

Note: If helpEnabled is not set or is set to false in the Web.config of the current WCF Service provider, then the help page won’t display.

Let’s take the GetDataUsingDataContract method as an example. Click the corresponding post method to open a new link shown in the screenshot below:

There is the example request XML body for us to consume this API.

Before we write PowerScript to call this method, we can also verify if we can successfully access it using a third-party tool (such as Postman), and see if the current parameters are valid.

The example request XML body content we copy is as follows:

<CompositeType xmlns="http://schemas.datacontract.org/2004/07/WelcomeWcfService">
  <BoolValue>true</BoolValue>
  <StringValue>String content</StringValue>
</CompositeType>

We can test this API in Postman first as shown in the screenshot below:

In the example, we use Postman to verify this API.

Note: You should replace the API content with your actual API content after you copy the XML.

For example: In the GetDataUsingDataContract method of this example, the StringValue parameter should be changed from “String content” to “New Test”.

      b. The PowerScript of using the HTTPClient object to call the API.

After you have verified the API using Postman, use the same protocol and content to implement it with the HTTPClient object from PowerBuilder.

Example code:

HttpClient 	lhc_client
Integer         li_ret, i , li_StatusCode
String 		ls_xml_request 
String 		ls_url, ls_body, ls_ret, ls_data
PBDOM_Builder 	lpbdom_builder 
PBDOM_Document 	lpbdom_doc
String 		ls_BoolValue, ls_StringValue
ls_url ="http://localhost:61869/WelcomeService.svc/GetDataUsingDataContract"
ls_xml_request ='<CompositeType xmlns="http://schemas.datacontract.org/2004/07/WelcomeWcfService">' + &
 ' <BoolValue>true</BoolValue>' + &
 ' <StringValue>New Test </StringValue>' + &
'</CompositeType>' 
lhc_client = Create HttpClient
lhc_client.SetRequestHeader("Content-Type", "application/xml")
lhc_client.SetRequestHeader("Content-Length",string(len(ls_xml_request) ))
ls_data = ""
li_ret = lhc_client.sendrequest("POST", ls_url,ls_xml_request )
li_StatusCode = lhc_client.GetResponseStatusCode()
ls_ret = lhc_client.GetResponseStatusText( )
li_ret = lhc_client.getresponsebody(  ls_data)
Destroy lhc_client

lpbdom_builder = Create PBDOM_Builder
lpbdom_doc = lpbdom_builder.BuildFromString(ls_data)
ls_BoolValue = lpbdom_doc.GetRootElement().GetChildElement( "BoolValue","", "http://schemas.datacontract.org/2004/07/WelcomeWcfService").GetText()
ls_StringValue = lpbdom_doc.GetRootElement().GetChildElement( "StringValue","", "http://schemas.datacontract.org/2004/07/WelcomeWcfService").GetText()
Destroy lpbdom_builder

How to use the RESTClient object to consume a WCF Service.

      a. To get the method and parameters to consume the WCF Service.

Normally when you open a WCF Service website, you will get the following page:

However, you would still need the documentation provided by the WCF Service and also need to verify if the current WCF Service supports JSON format parameters in order to consume this WCF using URL or through a client. If the current service provider has provided the online help, you can use the WCF Service address + "/help" to obtain the corresponding API descriptions, the parameters as follows:

Note: If helpEnabled is not set or is set to false in the Web.config of the current WCF Service provider, then the help page won’t display.

Let’s take the GetDataUsingDataContract method as an example. Click the corresponding post method to open a new link shown in the screenshot below:

There is the example request body for us to consume this API.

Note: When using the RESTClient object to access the GetDataUsingDataContract method, we have modified the returned value in WCF Service to support JSON instead of the previous default XML content. (The example in the previous HTTPClient object method uses XML format returned value.)

Before we write PowerScript to call this method, we can also verify if we can successfully access it using a third-party tool (such as Postman), and see if the current parameters are valid.

The example request JSON body content we copy is as follows:

{
	"BoolValue":true,
	"StringValue":"String content"
}

We can test this API in Postman first as shown below:

Then we can see that the current code works correctly.

       b. The PB code of using the RESTClient object to call the API.

After you have verified the API with Postman, you can use the same protocol and content to implement it with the RESTClient object from PowerBuilder.

Please note that the current code has been verified in PowerBuilder 2019 Build 2170.

Example Code:

Integer 	li_return, li_StatusCode
String 		ls_url, ls_ret, ls_data, ls_json
String 		ls_Error, ls_value
RestClient 	lrc_RestClient
JsonParser 	ljp_JsonParser 
Boolean 	lb_flag

ls_json = '{"BoolValue":true,	"StringValue":"String content "}'
ls_url = "http://localhost:61869/WelcomeService.svc/GetDataUsingDataContract"

lrc_RestClient = Create RestClient
// Construct a POST request (supports all headers)
lrc_RestClient.SetRequestHeader("Content-Type", "application/json;charset=UTF-8")
li_return = lrc_RestClient.SendPostRequest(ls_url, ls_json, ls_data)
// Check the return value
If li_return = 1 Then
	li_StatusCode = lrc_RestClient.GetResponseStatusCode()
	ls_ret = lrc_RestClient.GetResponseStatusText()

	ljp_JsonParser = Create JsonParser
	ls_Error = ljp_JsonParser.LoadString(ls_data)
	If Len(ls_Error) = 0 Then
		ls_value = ljp_JsonParser.GetItemString(ljp_JsonParser.GetRootItem(), "StringValue")
		lb_flag = ljp_JsonParser.GetItemBoolean(ljp_JsonParser.GetRootItem(), "BoolValue")
	End if 
	Destroy ljp_JsonParser
Else
	MessageBox("Error", "Failed to retrieve data.")
End If

Destroy lrc_RestClient

 

Comments (0)
There are no comments posted here yet

Find Articles by Tag

SQL TortoiseGit Platform Authentication XML Windows OS WebBrowser Graph SqlModelMapper DataWindow JSON OLE Database Object Syntax File TFS Mobile Web Service Proxy Source Control Text GhostScript Export Service Validation WinAPI SqlExecutor TLS/SSL License Oracle Resize Performance .NET Assembly DLL PDFlib Database Profile JSON External Functions Configuration Database Painter Import JSON Azure Expression PowerBuilder (Appeon) Authorization Model OrcaScript PowerBuilder Compiler Excel HTTPClient DragDrop Encoding Window Jenkins Event Handler Git .NET Std Framework Transaction Source Code 64-bit Testing Database Connection RESTClient Debugging Application API Database Table PowerBuilder Interface Event Linux OS CrypterObject Bug Sort CI/CD Debug PostgreSQL ODBC driver BLOB PBDOM SDK ODBC OAuth 2.0 SOAP Filter PowerScript (PS) CoderObject DevOps InfoMaker Stored Procedure .NET DataStore Icon Outlook Web API MessageBox Error Windows 10 Icons Array RibbonBar Builder SQL Server Event Handling PostgreSQL Database Table Data SVN Android UI Menu Migration PowerServer Web Charts Data OAuth UI Modernization Encryption RibbonBar PowerServer Mobile Automated Testing PFC 32-bit SnapDevelop Debugger IDE DataWindow REST Database Table Schema Installation JSONGenerator Import C# UI Themes Design Deployment Repository Messagging NativePDF COM Export JSON Open Source SnapObjects ActiveX JSONParser TreeView Class DataType Branch & Merge iOS RichTextEdit Control Trial Database Variable PDF Script Elevate Conference