vs2005 vc++调用Web Service实例使用说明

1.假如要调用Web Service的接口实例,编写一个翻译工具:

http://fy.webxml.com.cn/webservices/EnglishChinese.asmx

所要准备的工具:

1.VS2005开发工具。

2.gsoap工具。

步骤:

1.首先了解工具使用方法:http://www.cs.fsu.edu/~engelen/soap.html 外文地址

2.下载gsoap : https://sourceforge.net/projects/gsoap2/

3.下载后在目录:gsoap_2.8.88\gsoap-2.8\gsoap\bin\win32 下可以看到文件soapcpp2.exe,wsdl2h.exe

4.为了解决中文兼容问题,新建wsmap.dat 文件,内容为:xsd__string = | std::wstring | wchar_t*

4.打开CMD命令行,到soapcpp2.exe,wsdl2h.exe所在目录运行以下命令:

使用命令: ./wsdl2h -o WebService.h -n WS -t wsmap.dat http://fy.webxml.com.cn/webservices/EnglishChinese.asmx?wsdl

生成过程:


PS F:\WebService\gsoap_2.8.88\gsoap-2.8\gsoap\bin\win32> ?./wsdl2h -o WebService.h -n WS -t wsmap.dat http://fy.webxml.com.cn/webservices/EnglishChinese.asmx?wsdl
Saving WebService.h


**  The gSOAP WSDL/WADL/XSD processor for C and C++, wsdl2h release 2.8.88
**  Copyright (C) 2000-2019 Robert van Engelen, Genivia Inc.
**  All Rights Reserved. This product is provided "as is", without any warranty.
**  The wsdl2h tool and its generated software are released under the GPL.
**  ----------------------------------------------------------------------------
**  A commercial use license is available from Genivia Inc., [email protected]
**  ----------------------------------------------------------------------------

Reading type definitions from type map "wsmap.dat"
Connecting to 'http://fy.webxml.com.cn/webservices/EnglishChinese.asmx?wsdl' to retrieve WSDL/WADL or XSD... connected, receiving...
Done reading 'http://fy.webxml.com.cn/webservices/EnglishChinese.asmx?wsdl'

Warning: 4 service bindings found, but collected as one service (use option -Nname to produce a separate service for each binding)

Warning: ignoring RESTful binding "EnglishChineseHttpGet" because of non-SOAP transport "" (use option -R to enable REST)

Warning: ignoring RESTful binding "EnglishChineseHttpPost" because of non-SOAP transport "" (use option -R to enable REST)

To finalize code generation, execute:
> soapcpp2 WebService.h
Or to generate C++ proxy and service classes:
> soapcpp2 -j WebService.h

PS F:\WebService\gsoap_2.8.88\gsoap-2.8\gsoap\bin\win32>

5.生成后会在目录下生成WebService.h 文件。

再运行:./soapcpp2 -C WebService.h -I  F:\WebService\gsoap_2.8.88\gsoap-2.8\gsoap\import

PS F:\WebService\gsoap_2.8.88\gsoap-2.8\gsoap\bin\win32> ./soapcpp2 -C WebService.h -I ?F:\WebService\gsoap_2.8.88\gsoap-2.8\gsoap\import

**  The gSOAP code generator for C and C++, soapcpp2 release 2.8.88
**  Copyright (C) 2000-2019, Robert van Engelen, Genivia Inc.
**  All Rights Reserved. This product is provided "as is", without any warranty.
**  The soapcpp2 tool and its generated software are released under the GPL.
**  ----------------------------------------------------------------------------
**  A commercial use license is available from Genivia Inc., [email protected]
**  ----------------------------------------------------------------------------

Saving soapStub.h annotated copy of the source interface header file
Saving soapH.h serialization functions to #include in projects
Using WS1 service name: EnglishChineseSoap
Using WS1 service style: document
Using WS1 service encoding: literal
Using WS1 service location: http://fy.webxml.com.cn/webservices/EnglishChinese.asmx
Using WS1 schema namespace: http://WebXml.com.cn/
Saving EnglishChineseSoap.Translator.req.xml sample SOAP/XML request
Saving EnglishChineseSoap.Translator.res.xml sample SOAP/XML response
Saving EnglishChineseSoap.TranslatorString.req.xml sample SOAP/XML request
Saving EnglishChineseSoap.TranslatorString.res.xml sample SOAP/XML response
Saving EnglishChineseSoap.TranslatorReferString.req.xml sample SOAP/XML request
Saving EnglishChineseSoap.TranslatorReferString.res.xml sample SOAP/XML response
Saving EnglishChineseSoap.TranslatorSentenceString.req.xml sample SOAP/XML request
Saving EnglishChineseSoap.TranslatorSentenceString.res.xml sample SOAP/XML response
Saving EnglishChineseSoap.SuggestWord.req.xml sample SOAP/XML request
Saving EnglishChineseSoap.SuggestWord.res.xml sample SOAP/XML response
Saving EnglishChineseSoap.GetMp3.req.xml sample SOAP/XML request
Saving EnglishChineseSoap.GetMp3.res.xml sample SOAP/XML response
Saving EnglishChineseSoap.Translator.req.xml sample SOAP/XML request
Saving EnglishChineseSoap.Translator.res.xml sample SOAP/XML response
Saving EnglishChineseSoap.TranslatorString.req.xml sample SOAP/XML request
Saving EnglishChineseSoap.TranslatorString.res.xml sample SOAP/XML response
Saving EnglishChineseSoap.TranslatorReferString.req.xml sample SOAP/XML request
Saving EnglishChineseSoap.TranslatorReferString.res.xml sample SOAP/XML response
Saving EnglishChineseSoap.TranslatorSentenceString.req.xml sample SOAP/XML request
Saving EnglishChineseSoap.TranslatorSentenceString.res.xml sample SOAP/XML response
Saving EnglishChineseSoap.SuggestWord.req.xml sample SOAP/XML request
Saving EnglishChineseSoap.SuggestWord.res.xml sample SOAP/XML response
Saving EnglishChineseSoap.GetMp3.req.xml sample SOAP/XML request
Saving EnglishChineseSoap.GetMp3.res.xml sample SOAP/XML response
Saving EnglishChineseSoap.nsmap namespace mapping table
Saving soapClient.cpp client call stub functions
Saving soapClientLib.cpp client stubs with serializers (use only for libs)
Saving soapC.cpp serialization functions

Compilation successful

6.新建的工程,添加文件:gsoap_2.8.88\gsoap-2.8\gsoap\stdsoap2.h gsoap_2.8.88\gsoap-2.8\gsoap\stdsoap2.cpp, 和生成的文件:soapC.cpp soapClient.cpp soapClientLib.cpp soapH.h soapStub.h WebService.h EnglishChineseSoap.nsmap

7. 调用gsoap功能:

#include "EnglishChineseSoap.nsmap"
.
.
.



	struct soap soap;
	soap_init(&soap);
	const char * endPoints = "http://fy.webxml.com.cn/webservices/EnglishChinese.asmx";
	const char soap_action=1;

	_WS1__TranslatorString WS1__Translator;
	string key1 = GbkToUtf8(m_key.GetBuffer());
	string encode = UrlEncode(key1);
	wstring encode1 = string2wstring(encode);

	wchar_t* key = (wchar_t*)encode1.c_str();//m_key.GetBuffer();

	WS1__Translator.wordKey = key;
	//
	//	soap_send___WS1__TranslatorString_(&as,&soap_endpoint,&soap_action, &AwordKey);
	_WS1__TranslatorStringResponse WS1__TranslatorResponse;


	CString msg;	

	//	soap_recv___WS1__TranslatorString_(&as,aResponse);
	//	//std::vector rstring = aResponse.TranslatorStringResult->string; 

	//soap_call_ns__method1(&soap, ...); // 调用一个远程方法
	//SOAP_FMAC5 int SOAP_FMAC6 soap_call___WS1__Translator(struct soap *soap, const char *soap_endpoint, const char *soap_action, _WS1__Translator *WS1__Translator, _WS1__TranslatorResponse &WS1__TranslatorResponse)
	if (soap_call___WS1__TranslatorString(&soap,endPoints,NULL, &WS1__Translator, WS1__TranslatorResponse ) == SOAP_OK)
	{
		if(WS1__TranslatorResponse.TranslatorStringResult)
		{
			int id =0;

			for (std::vector::iterator it = WS1__TranslatorResponse.TranslatorStringResult->string.begin() ; it != WS1__TranslatorResponse.TranslatorStringResult->string.end(); ++it)
			{
				id++;
				if(id==4) msg+=((*it).c_str());
			}

		}
		else
		{
			msg="no find";
		}

	}
	else
	{
		soap_print_fault(&soap, stderr);
	}


	soap_destroy(&soap);

	soap_end(&soap);

	soap_done(&soap);



	MessageBox(msg);

vs2005 vc++调用Web Service实例使用说明_第1张图片

源码下载: https://download.csdn.net/download/dongxinddd123/11461864

你可能感兴趣的:(vs2005 vc++调用Web Service实例使用说明)