User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

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: