Call WCF Services Using HTTPClient or RESTClient Objects (2024)

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:

Call WCF Services Using HTTPClient or RESTClient Objects (1)

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:

Call WCF Services Using HTTPClient or RESTClient Objects (2)

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.

Call WCF Services Using HTTPClient or RESTClient Objects (3)

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.

Call WCF Services Using HTTPClient or RESTClient Objects (4)

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:

Call WCF Services Using HTTPClient or RESTClient Objects (5)

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.

Call WCF Services Using HTTPClient or RESTClient Objects (6)

Example code:

HttpClient lhc_clientInteger li_ret, i , li_StatusCodeString ls_xml_request String ls_url, ls_body, ls_ret, ls_dataPBDOM_Builder lpbdom_builder PBDOM_Document lpbdom_docString ls_BoolValue, ls_StringValuels_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 HttpClientlhc_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_clientlpbdom_builder = Create PBDOM_Builderlpbdom_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:

Call WCF Services Using HTTPClient or RESTClient Objects (7)

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:

Call WCF Services Using HTTPClient or RESTClient Objects (8)

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.

Call WCF Services Using HTTPClient or RESTClient Objects (9)

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.

Call WCF Services Using HTTPClient or RESTClient Objects (10)

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:

Call WCF Services Using HTTPClient or RESTClient Objects (11)

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.

Call WCF Services Using HTTPClient or RESTClient Objects (12)

Example Code:

Integer li_return, li_StatusCodeString ls_url, ls_ret, ls_data, ls_jsonString ls_Error, ls_valueRestClient lrc_RestClientJsonParser ljp_JsonParser Boolean lb_flagls_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 valueIf li_return = 1 Thenli_StatusCode = lrc_RestClient.GetResponseStatusCode()ls_ret = lrc_RestClient.GetResponseStatusText()ljp_JsonParser = Create JsonParserls_Error = ljp_JsonParser.LoadString(ls_data)If Len(ls_Error) = 0 Thenls_value = ljp_JsonParser.GetItemString(ljp_JsonParser.GetRootItem(), "StringValue")lb_flag = ljp_JsonParser.GetItemBoolean(ljp_JsonParser.GetRootItem(), "BoolValue")End if Destroy ljp_JsonParserElseMessageBox("Error", "Failed to retrieve data.")End IfDestroy lrc_RestClient
Call WCF Services Using HTTPClient or RESTClient Objects (2024)
Top Articles
Which Phrase Most Accurately Describes Metallic Bonding
About How Many People Moved Away From The Great Plains States During The Depression? 100,000 250,000
Wal-Mart 140 Supercenter Products
Best Internists In Ft-Lauderdale
How to Create a Batch File in Windows? - GeeksforGeeks
One Hour Rosemary Focaccia Bread
Louisville Kentucky Craigslist Cars And Trucks By Owner
Cold War Brainpop Answers
Rick Lee Oaklawn Park Picks Today
Elgin Il Building Department
80 For Brady Showtimes Near Brenden Theatres Kingman 4
Telegram X (Android)
Uhcs Patient Wallet
Chicken Coop Brookhaven Ms
Hongkong Doll在线观看
Nascar Espn Schedule
Tractorhouse Farm Equipment
Fingerfang Rock Conan
Xsammybearxox
Karz Insurance Quote
Water Leaks in Your Car When It Rains? Common Causes & Fixes
Shop - Mademoiselle YéYé
2Lookmovie
Pain Out Maxx Kratom
One Piece Chapter 1077 Tcb
Rite Aid Klein Transit
Closest Dollar Tree Store To My Location
Sprinter Tyrone's Unblocked Games
Bayada Bucks Catalog 2023
Buffalo Bills Football Reference
Knicks Tankathon 2.0: Five clicks and five picks in the NBA Draft
Community Q&A with Red Flight and the Combat Box server
Advance Auto.parts Near Me
Dom's Westgate Pizza Photos
About Us - Carrols Corporation
Theatervoorstellingen in Roosendaal, het complete aanbod.
Valentino Garavani Flip Flops
Winsipedia
Leesburg Regional Medical Center Medical Records
Persona 5 R Fusion Calculator
Dumb Money Showtimes Near Maya Cinemas Salinas
Glassbox Eyecare
Autozone Cercano
5Gomovies
Mygxo Gxo Com Employee Login
Ece 2300 Osu
Pge Set Up Service
Doomz.io Unblocked Games 76
Craigslist Garage Sales Schenectady Ny
11 Awesome Cities: Skylines Mods You Need To Try
Rune Factory 5 Dual Blade Recipes
EXTON: THE MOST BEAUTIFUL CHOCOLATE BOX VILLAGE IN RUTLAND
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 5641

Rating: 4.8 / 5 (48 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.