gsoap使用

查了很多资料,主要还是参考了这个片文章

http://panxq0809.iteye.com/blog/709173

很多都是照猫画虎,主要是最后的乱码问题

 

调用的webservice连接是

http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

将网页另存为WeatherWebService.wsdl

 

运行命令

wsdl2h -o WeatherWebService.h WeatherWebService.wsdl

soapcpp2 -i -C -x WeatherWebService.h -ID:\gsoap-2.8\gsoap\import

这2个命令在 D:\gsoap-2.8\gsoap\bin\win32 下

 

将生成的文件一并添加到工程,可能有的提示无法生成,不用管,将gSoap下的stdsoap2.h,stdsoap2.cpp也添加到工程

最后将使用预编译头的选项去掉,生成。

 

 

废话不多说,直接上代码

 

// WeatherTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"

#include "WeatherWebServiceSoap.nsmap"
#include "soapWeatherWebServiceSoapProxy.h"

int _tmain(int argc, _TCHAR* argv[])
{
	_ns1__getSupportCity city;
	_ns1__getSupportCityResponse cityRespone;
	_ns1__getWeatherbyCityName weather;
	_ns1__getWeatherbyCityNameResponse weatherRespone;

	WeatherWebServiceSoapProxy proxy("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx", SOAP_C_UTFSTRING);

	//std::string arg1("54765");
	std::string arg1("");
	std::vector<std::string>::iterator iter;

	if(argc != 2 || _tcscmp(argv[1], _T("-h")) == 0 || _tcscmp(argv[1], _T("/h")) == 0){
		printf("使用方法: WeatherTest [城市名]\r\n");
		return 0;
	}

	char* pBuf = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 4096);
	if(pBuf == NULL){
		printf("申请缓冲区失败!\r\n");
		goto err_exit;
	}

	TCHAR* pBufT = (TCHAR*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 4096);
	if(pBufT == NULL){
		printf("申请缓冲区失败!\r\n");
		goto err_exit;
	}

	city.soap = &proxy;
	city.byProvinceName = &arg1;
	if(SOAP_OK != proxy.getSupportCity_(&city, &cityRespone)){
		printf("获取城市列表失败\r\n");
		goto err_exit;
	}

	for(iter = cityRespone.getSupportCityResult->string.begin();
		iter != cityRespone.getSupportCityResult->string.end();
		iter++)
	{
		size_t nn = iter->length();
		if(nn> 2040){
			HeapFree(GetProcessHeap(), 0, pBuf);
			pBuf = NULL;
			HeapFree(GetProcessHeap(), 0, pBufT);
			pBufT = NULL;

			pBuf = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nn+10);
			if(pBuf == NULL){
				printf("申请缓冲区失败!\r\n");
				return 0;
			}

			pBufT = (TCHAR*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nn+10);
			if(pBufT == NULL){
				printf("申请缓冲区失败!\r\n");
				return 0;
			}
		}

		memset(pBuf, 0, 4096);
		memset(pBufT, 0, 4096);
		MultiByteToWideChar(CP_UTF8, 0, iter->c_str(), nn, (WCHAR*)pBufT, nn+4);

		// 将UTF-8转换成UNICODE
		WideCharToMultiByte(CP_ACP, 0, (WCHAR*)pBufT, nn, (CHAR*)pBuf, nn+4, NULL, NULL);
		// 将UNICODE转换成GB2312还是GBK就不清楚了
		if(_tcsncmp(pBufT, argv[1], _tcslen(argv[1])) == 0){
			//printf(pBuf);
			int xx = strlen(pBuf);
			char* p1 = pBuf;
			char* p2 = pBuf;
			while(xx && *p2 != '('){
				p2++;
				xx--;
			}
			if(!xx || *p2 != '('){
				return 0;
			}
			p1 = p2+1;

			while(xx && *p2 != ')'){
				p2++;
				xx--;
			}
			if(!xx || *p2 != ')'){
				return 0;
			}
			*p2 = 0;

			arg1 = p1;// 找到城市对应码,以此来取得天气信息

			break;
		}
	}

	if(arg1 == ""){
		size_t nn = _tcslen(argv[1]);
		if(nn > 2040){
			nn = 2040;
		}
		memset(pBuf, 0, 4096);
		memset(pBufT, 0, 4096);
		WideCharToMultiByte(CP_ACP, 0, (WCHAR*)argv[1], nn, (CHAR*)pBuf, nn*2+2, NULL, NULL);
		// 将UNICODE转换成GB2312还是GBK就不清楚了

		printf("找不着城市:%s\r\n", pBuf);
		goto err_exit;
	}

	weather.soap = &proxy;
	weather.theCityName = &arg1;

	if(SOAP_OK != proxy.getWeatherbyCityName_(&weather, &weatherRespone)){
		printf("获取城市信息失败\r\n");
		goto err_exit;
	}

	for(iter = weatherRespone.getWeatherbyCityNameResult->string.begin();
		iter != weatherRespone.getWeatherbyCityNameResult->string.end();
		iter++)
	{
		size_t nn = iter->length();
		if(nn> 2040){
			HeapFree(GetProcessHeap(), 0, pBuf);
			pBuf = NULL;
			HeapFree(GetProcessHeap(), 0, pBufT);
			pBufT = NULL;

			pBuf = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nn+10);
			if(pBuf == NULL){
				printf("申请缓冲区失败!\r\n");
				return 0;
			}
			pBufT = (TCHAR*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nn+10);
			if(pBufT == NULL){
				printf("申请缓冲区失败!\r\n");
				return 0;
			}
		}

		memset(pBuf, 0, 4096);
		memset(pBufT, 0, 4096);
		MultiByteToWideChar(CP_UTF8, 0, iter->c_str(), nn, (WCHAR*)pBufT, nn+4);

		// 将UTF-8转换成UNICODE
		WideCharToMultiByte(CP_ACP, 0, (WCHAR*)pBufT, nn, (CHAR*)pBuf, nn+4, NULL, NULL);
		// 将UNICODE转换成GB2312还是GBK就不清楚了

		std::cout << pBuf << std::endl;
		//std::cout << *iter << std::endl;
	}

err_exit:

	if(pBuf){
		HeapFree(GetProcessHeap(), 0, pBuf);
		pBuf = NULL;
	}
	if(pBufT){
		HeapFree(GetProcessHeap(), 0, pBufT);
		pBufT = NULL;
	}
	return 0;
}

  

 

posted on 2016-08-22 14:56  litandy  阅读(352)  评论(0编辑  收藏  举报

导航