`
bruce008
  • 浏览: 170244 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

在C++下使用gSoap 调用Java开发的Web Service

阅读更多

 

最近有一个需求要在C++ 里面调用Java 的Web Service。具体的环境是 VS2008, XP sp3.

这两个东东一组合就悲催了。 本来基于微软自己的技术有两个可以选。

   1 ATL Server, 悲催的微软啊, 从VS 2008 将ATL Server 从Visual Studio 中拿到了去所谓的开源了。从此在VS2008 里面选用 add web reference 也会出错的, sproxy.exe 也被拿掉了。 去下载了个 ATL Server 的源代码 也没有办法成功编译sproxy。 想想反正这个sproxy 生成的代码也会是一坨一坨的 template 代码。 咱不专业啊, 找其它的呗。

   2,微软还有个Windows Web Services API 不过这个更搞, 只有在windows 7, window 2008 下用。  如果XP 下用还得发email 给微软问它讨, 这个你还得是啥大客户啥的, 一年至少几万美刀级别的。 

幸好有啥, 开源。开源好啊, 找到个gSoap。 这个现在差不多也成了一个在c++下使用Web Service的事实标准了。 鉴于这个gSoap 的使用方法还得整理出来放到项目的wiki。 俺们就干脆用英语整理了,省的来两遍。

1,  gSoap is an open source Web Service /XML binding for  C/C++.  It supports  Windows and GCC operating system.

    go to the official web site 

    http://sourceforge.net/projects/gsoap2

    or goto download the http://sourceforge.net/projects/gsoap2/files/latest/download

        Currently the stable version is 2.8.8

     

     Of couse we can build the tools from scratch. There is VisualStudio2005 folder, under which the Readme is very importance.  


2,   We can just use the tools under gSoap to build the stub and proxy source code for us:

     there are two tools under gSoap, ther are wsdl2h and soapcpp2.

       1, wsdl2h -o  service.h  -n namespacename  URL or file name

          the generatd service.h file will be used in the next step. 

       2, soapcpp2  service.h -C -i -x -I F:\study_code\mfc\gsoap_2.8.8\gsoap-2.8\gsoap\import

        -I  sepecify the import folder under gSoap.

        soapcpp2 will generate the proxy, soaph.h, soapc.c files.  

 

3,  how to use the generated files.

    Normally, we just use the web service client.If we compile the client code, we need files

          soaph.h

          soapRSOWebserviceServiceSoapBindingProxy.h

          soapStub.h

          soapc.cpp

          soapRSOWebserviceServiceSoapBindingProxy.cpp

       and other two files :   stdsoap2.h, stdsoap2.cpp ( they are under the gSoap folder)

   The example code can be like:

 

#include "RSOWebserviceServiceSoapBinding.nsmap"
#include "soapRSOWebserviceServiceSoapBindingProxy.h"

const char endpoint[] = "http://localhost:8080/rso-ws-1.0.5/service";
int main(int argc, char **argv)
{ 
    RSOWebserviceServiceSoapBindingProxy  proxy(SOAP_XML_INDENT);

	std::string landingPath = "c";	/* optional element of type xsd:string */
	int customerId = 2;	/* optional element of type xsd:int */
	int clientId = 3;	/* optional element of type xsd:int */
	std::string groupExternalId = "groupExternalId";	/* optional element of type xsd:string */
	std::string memberId = "memberId";	/* optional element of type xsd:string */

	wstest1__getRSOToken wstest1__getRSOToken_;
	wstest1__getRSOToken_.clientId = &clientId;
	wstest1__getRSOToken_.customerId = &customerId;
	wstest1__getRSOToken_.groupExternalId = &groupExternalId;
	wstest1__getRSOToken_.landingPath = &landingPath;
	wstest1__getRSOToken_.memberId = &memberId;
    wstest1__getRSOTokenResponse wstest1__getRSOTokenResponse_;
	proxy.getRSOToken(endpoint, NULL, &wstest1__getRSOToken_, &wstest1__getRSOTokenResponse_);

	std::cout << *(wstest1__getRSOTokenResponse_.rsoToken) << std::endl;

	std::cout << "successful" << std::endl;
  
}    
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics