源码下载地址:http://download.csdn.net/detail/biboheart/6314985
在前面两篇博文中,我们建立了jee和vc++的webservice服务端程序。现在,我们开始通过gsoap完成webservice客户端vc++项目。
前两篇:
《gsoap CXF2.7.5 ssh vc++ webservice应用实例(一)》;
《gsoap CXF2.7.5 ssh vc++ webservice应用实例(二)》
创建一个win32控制台应用程序,项目名称为HelloWorldClient,在项目目录中建立一个文件夹hello。
还是使用在(二)中使用gsoap生成的文件,不同的是,这次我们使用soapHelloWorldSoapBindingProxy.cpp和soapHelloWorldSoapBindingProxy.h
而不使用soapHelloWorldSoapBindingService.cpp和soapHelloWorldSoapBindingService.h
把9个文件复制到hello文件夹中,如图
把stdsoap2.h;stdsoap2.cpp;soapHelloWorldSoapBindingProxy.cpp;soapC.cpp;四个文件添加到项目中。
设置3个.cpp文件的属性为不使用预编译头。
HelloWorldClient.cpp代码如下
#include "stdafx.h"
#include "hello\soapHelloWorldSoapBindingProxy.h"
#include "hello\HelloWorldSoapBinding.nsmap"
int _tmain(int argc, _TCHAR* argv[])
{
cout << "test client:" << endl;
HelloWorldSoapBindingProxy *helloService = new HelloWorldSoapBindingProxy();
helloService->soap_endpoint = "http://localhost:8080/CXFServerDemo/webServices/HelloWorld";
ns1__sayHello request;
ns1__sayHelloResponse response;
request.name = "biboheart";
if(helloService->sayHello(&request, &response) != SOAP_OK)
{
cout << "请求失败" << endl;
}
else
{
string message = response.return_;
cout << "response:" << message << endl;
}
getchar();
return 0;
}
注意:
“helloService->soap_endpoint = "http://localhost:8080/CXFServerDemo/webServices/HelloWorld";”
这行是设置服务端地址的,这里测试cxf建立的webservice服务端,服务端是发布在tomcat中。默认为8080端口。如果是请求(二)中用gsoap创建的服务,这里的端口就相应修改,因为2中我们设置的9001端口监听。像下面这样设置。
“helloService->soap_endpoint = "http://localhost:9001/CXFServerDemo/webServices/HelloWorld";”
如果服务端搭建在网络中的计算机中,那么“localhost”就换成相应的ip地址
编译项目。
因为这里设置的是请求8080,我们启动tomcat的CXFServerDemo项目服务。
运行客户端程序,结果如下图。
运行(二)中生编译的程序,把端口改成9001,运行客户端。运行结果相同。
还剩一个CXF客户端项目。就可以完成互相调用了。