This article is useful for those who have never worked with WebService call from VC++ and they want to know how to send a request and get a response? This is my first application which I have created for learning purposes.
I have referred to one CodeProject article. For WebService call, we require two things: one is that WebService must be created before calling it and the other is this VC++ application which calls it.
Using the Code
First of all, I would like to give you a brief on how to create a WebService. I am a System developer, that's why I don't have that good knowledge of creating a Webservice. You need to start Visual 2005, then File ->New-> website-> ASP.NET webservice. Then one default function will be created by the browser. Inside that function, you need to write some functionality that you want to take as request and return something on input basis. I have taken name as input and return address as per input name. That's why you can see Chaitanya name inside the XML code which I am posting on that link. Once the web service is created, you need to publish your site (which you can do using Inet manager).
Here is the code which I have used for posting a request and getting a response from WebService.
IXMLHttpRequestPtr httpReq( _uuidof(MyXMLHTTPRequest));
_bstr_t HTTPMethod ;
_variant_t noAsync = _variant_t( (bool)false );
CString strUserName = "mahesh"; VARIANT vUser;
vUser.vt = VT_BSTR;
vUser.bstrVal = strUserName.AllocSysString();
CString strPass = "gtl=11"; VARIANT vPassword;
vPassword.vt = VT_BSTR;
vPassword.bstrVal = strPass.AllocSysString();
HTTPMethod = _bstr_t( "POST" );
szRequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?> \
<HelloWorld xmlns=\"http://tempuri.org/\"> \
vRequest.vt = VT_BSTR;
vRequest.bstrVal = szRequest.AllocSysString();
httpReq->send(vRequest); BSTR strText;
_bstr_t bsResponse = httpReq->responseText;
One very important thing is that we need to import MSXML.dll which you can see in the code in stdafx.h and "
using namespace MSXML;" line in file.
Points of Interest
You can advise me on how to improve this article so that I can also learn new things. If you have any queries, you can post a message. I will try to solve it, and that way I will also learn new things in this area. You can mail me at firstname.lastname@example.org.
- 22nd June, 2007: Initial post