Click here to Skip to main content
Click here to Skip to main content
Go to top

Invoking Web Services

, 31 May 2003
Rate this:
Please Sign up or sign in to vote.
How to invoke Web Services with Visual C++ .NET
<!-- Add the rest of your HTML here -->

Invoking a Web services with .NET

<!------------------------------- STEP 3 ---------------------------><!-- Add the article text. Please use simple formatting (

,

etc) -->

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.

dialog box to invoke BabelFish Web service

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:

// TODO: Add extra initialization here

// Add translation modes to the ComboBox
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 :

dialog box to invoke BabelFish Web service

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:

...
// Initialize COM
CoInitialize(NULL);

// Create a pointer to Babel Fish Service
CBabelFishService *fish = new CBabelFishService();
.../...
// invoke Web Service
HRESULT hr = fish->BabelFish(bstrMode,bstrSource,&bstrResult);
if (FAILED(hr))
.../...
else
{
    // transfert response to a CString variable
    str = bstrResult;
    // copy texte translated to the EditBox
    m_ctlResult.SetWindowText((LPCSTR)str);
}
... / ...
// deleting the pointer to Babel Fish Service
delete fish;
// Uninitialize COM
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

joel garel
Web Developer
France France
No Biography provided

Comments and Discussions

 
QuestionWeb Service Error : "The underlying connection was closed: The connection was closed unexpectedly." Pinmemberganesh_barhate28-Jul-10 23:03 
GeneralThanks, very simple and clear Pinmemberpvox5-Sep-07 4:53 
QuestionSOAPCLIENT_SEND_ERROR PinmemberAjith Sivakumar20-Sep-06 2:27 
NewsRe: SOAPCLIENT_SEND_ERROR PinmemberAjith Sivakumar27-Sep-06 2:12 
QuestionSOAPCLIENT_SEND_ERROR PinmemberDingos Pingo14-Sep-06 5:18 
AnswerRe: SOAPCLIENT_SEND_ERROR PinmemberAjith Sivakumar20-Sep-06 2:31 
QuestionSOAPCLIENT_SEND_ERROR PinmemberAblimit Haji23-May-06 23:54 
JokeRe: SOAPCLIENT_SEND_ERROR Pinmemberminisfu4-Jun-06 21:23 
AnswerRe: SOAPCLIENT_SEND_ERROR Pinmemberchangyj25-Jun-06 21:08 
AnswerRe: SOAPCLIENT_SEND_ERROR PinmemberMorten Fjeldstad26-Jul-07 14:19 
GeneralRe: SOAPCLIENT_SEND_ERROR Pinmemberymalik10-Oct-07 13:37 
GeneralRe: SOAPCLIENT_SEND_ERROR PinmemberMorten Fjeldstad10-Oct-07 20:51 
GeneralRe: SOAPCLIENT_SEND_ERROR PinmemberRob Staveley31-Oct-07 8:05 
GeneralAuthentication problem Pinmembermmagnani7120-Apr-05 23:30 
GeneralDeserializing SOAP Pinmemberawidjaja24-Mar-05 9:34 
GeneralRe: Deserializing SOAP Pinmembereyadalagha1-Oct-07 23:13 
GeneralSOAPCLIENT_PARSE_ERROR Pinmemberbionet2-Dec-04 0:59 
GeneralGet Data from user in Webservice PinmemberDoesnotMAtter13-Nov-04 18:38 
Generalerror SDL1013 Pinmemberbionet4-Nov-04 14:14 
GeneralRe: error SDL1013 Pinmemberjoel garel9-Nov-04 1:40 
GeneralRe: error SDL1013 Pinmemberjoel garel9-Nov-04 22:17 
GeneralRe: error SDL1013 Pinmemberbionet11-Nov-04 6:29 
Generalthe service is __gc ... PinsussAnonymous24-Sep-04 9:56 
Generalcalling Webservice from vc.net , invoking methods by creating cookies Pinmemberbaijumax13-Aug-04 20:10 
QuestionCan the SoapClient Run under win98/me? PinmemberYu_Matrix1-Apr-04 20:08 
QuestionDoes it require .Net frameware? PinmemberDavidi22-Feb-04 20:19 
AnswerRe: Does it require .Net frameware? PinmemberShail_Srivastav7-Feb-06 7:16 
Generalusing VC++ 6.0 Pinmembermattroos4-Dec-03 14:00 
GeneralRe: using VC++ 6.0 Pinmemberjoel garel6-Jan-04 4:37 
GeneralRe: using VC++ 6.0 PinsussAnonymous26-Oct-04 10:46 
QuestionCan i set dataset as the return value? PinmemberYu_Matrix4-Jul-03 23:54 
Questionhow to dynamic change the URL of the service? PinmemberYu_Matrix4-Jul-03 23:12 
GeneralATL Server, not .NET Framework PinmemberFazlul Kabir2-Jun-03 7:29 
Generalexciting PinmemberMajid Shahabfar2-Jun-03 3:37 
GeneralRe: exciting Pinmemberjoel garel2-Jun-03 23:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 1 Jun 2003
Article Copyright 2003 by joel garel
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid