Introduction
The .NET platform provides support for invoking web services. No additional tools or SDKs are needed. This platform is very easy to use.
Invoking a Web services with .NET
To show you how easy it is to build a web services consumer application. I am going to drive you in the various stages to build an application which is going to translate a text in a pre-defined language. The translation will be realized by a call to the Web service of Altavista "BabelFish". This sample is written with Visual Studio C++ .NET 2003.
First of all, create a new Visual C++ Project using MFC application template and on MFC application wizard choose a dialog based application and press Finish button.
Modify the new dialog box to have a control ComboBox and 2 EditBox as presented below.
Add 3 members variables associated to each control.
CComboBox m_ctlLang;
CString m_strSource;
CEdit m_ctlResult;
If you have used the dialog box "Add Member Variable Wizard" you should see the code following in the member function DoDataExchange()
.
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_COMBO1, m_ctlLang);
DDX_Text(pDX, IDC_EDIT1, m_strSource);
DDX_Control(pDX, IDC_EDIT2, m_ctlResult);
It is necessary to initialize the control ComboBox in the member function OnInitDialog()
in the following way:
m_ctlLang.AddString("en_fr");
m_ctlLang.AddString("en_it");
m_ctlLang.AddString("en_de");
m_ctlLang.AddString("en_pt");
m_ctlLang.AddString("en_es");
m_ctlLang.AddString("fr_en");
m_ctlLang.AddString("it_en");
m_ctlLang.AddString("de_en");
m_ctlLang.AddString("pt_en");
m_ctlLang.AddString("es_en");
m_ctlLang.AddString("ru_en");
m_ctlLang.SetCurSel(0);
Now we are going to integrate the Web service into our application. We select in the Project menu the choice "Add Web Reference...". In the field of the dialog box which appears type the following url: "http://www.xmethods.net/sd/BabelFishService.ws dl " and click refresh or go button. The result which you have to obtain must be close to this one :
Now click the "Add Reference" button
By verifying in the Solution Explorer View you will notice that a file in the name of WebService.h
was added in Header Files. This file contains a class and all the member functions describing the Web service.
We are going to add in our project all the necessities to use this service.
1st step : add the following code in the Header File of your dialog box:
#include "WebService.h"
using namespace BabelFishService;
Then Create an Event Handler on the button Translate, put in comment the call to the function OnOK()
and write the following code:
...
CoInitialize(NULL);
CBabelFishService *fish = new CBabelFishService();
.../...
HRESULT hr = fish->BabelFish(bstrMode,bstrSource,&bstrResult);
if (FAILED(hr))
.../...
else
{
str = bstrResult;
m_ctlResult.SetWindowText((LPCSTR)str);
}
... / ...
delete fish;
CoUninitialize();
.../...
Reference
The detail of the functioning of the service Web BabelFish is described on the www.xmethods.net Internet site. You will find so many others useful examples of Web services there.
Demo project
The demo project contains the complete code to invoke the BabelFish service that I covered in this article.