Wednesday, May 15, 2013

Calling a SOAP web service

SOAP web services, although not as common as they once were, are still readily available on the web. And they can be incredibly easy to use with your apps.

Here is a quick example of how you would use a SOAP web service to get the conversion rate between currencies using the SOAP web service described here:

http://www.webservicex.net/CurrencyConvertor.asmx

The above page describes the method call that is available to you and also provides information about the various currencies available for conversion.

Click the "ConversionRate" method to get more information on how you would call this. This opens a new page (scroll down) with a way to test the web service and a list of the various ways that it can be called.

In the SOAP 1.1 section, you can see that the function is called "ConversionRate" and that it takes two parameters: FromCurrency and ToCurrency, both of which are simply a string containing the currency code.

The last thing to determine is the actual URL to use. Often, web services use the "WSDL" query string to fetch the Web Service Description Language for the service. In the case of this web service, you will use this URL:

http://www.webservicex.net/CurrencyConvertor.asmx?WSDL

With this information, you can use the SOAPMethod class to call the method.

To get started, create a new project and add to the default Window these controls:

  •  two TextFields to provide the from and to currency codes (FromCurrencyField and ToCurrencyField)
  • a PushButton to do the conversion (ConvertButton)
  • a TextField to display the results (ConversionRateField)

Now you can put this code in the Action event handler of the PushButton:


  Dim sm As New SOAPMethod("http://www.webservicex.net/CurrencyConvertor.asmx?WSDL")
  
  Dim conversionRate As String
  conversionRate = sm.ConversionRate(FromCurrencyField.Text, ToCurrencyField.Text)
  
  ConversionRateField.Text = conversionRate


Run the app an enter two currency codes (USD and EUR) and then press the button to get the conversion rate (0.7761 as of today).
There are many public web services that are available for you to use with your apps. Here are two sources of many web services:

http://www.webservicex.net

http://www.xmethods.com

Keep in mind that like anything on the web, web services can go offline from time to time so be sure to check for an error condition or invalid result from any web service call.

You can download the sample project (which will be included with Xojo) here.

3 comments:

JOAQUIM ROVIRA said...

Hi,

The WSDL contains a list of supported currencies. It would be nice if those could be loaded in a ListBox, this way users can select from a list instead of typing in a TexField.
How do you achieve that with Rb SOAP?

Thanks

Nerd Herder said...

I would like to see a good example on how to create SOAP or AJAX web services that can be consumed by remote devices. That would work great in conjunction IOS mobile applications. I can do it in VB but I would prefer to have a good way to do it in RealBasic / Xojo.

Freche Lösungen said...

Hi, I want to do something similar. But I don't know how to pass a complexType as parameter to the sm (SOAPMethod).
You pass two simpletypes to the function "ConversionRate".
I need to pass one simplytype and one complexType. How to do this??
My function is "GetInfo" and I have to pass a complexType-paramtet "GetInfo" which consists of the two parameters "user" and "password. The second parameter is simpleType.

I tried
var_information = sm.GetInfo([UserField.Text, PasswordField.Text], TIDField.Text)
as well as:
var_information = sm.GetInfo((UserField.Text, PasswordField.Text), TIDField.Text)

But this does not work. How to put the two "subparameters" of the "userdata"-complexType to the GetInfo-function?