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

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