vc 调用webservice

snoap下载地址 

https://sourceforge.net/projects/gsoap2/?source=typ_redirect

新建vc工程并添加源文件

./snoap/bin/win32/soapcpp2.exe拷贝到工程目录

cmd 运行soapcpp2.exe  会生成一大堆文件

我们先在源文件中声明一个接口

int plus(int num_1, int num_2, int *sum);

编译再运行soapcpp2.exe


添加文件

stdsoap2.h

stdsoap2.cpp

这两个文件在下载包中

soapH.h

soapC.cpp

soapStub.h

soapServer.cpp

到工程目录

其中soapH.h和soapC.cpp是具体的实现,soapStub.h为代理的声明

编译出现了两个无法解析的外部符号

1>------ 已启动生成:  项目: ClipBoard, 配置: Debug Win32 ------
1>soapServer.obj : error LNK2019: 无法解析的外部符号 "int __cdecl plus(struct soap *,int,int,int *)" (?plus@@YAHPAUsoap@@HHPAH@Z),该符号在函数 "int __cdecl soap_serve_plus(struct soap *)" (?soap_serve_plus@@YAHPAUsoap@@@Z) 中被引用
1>stdsoap2.obj : error LNK2001: 无法解析的外部符号 _namespaces
1>D:\MyCode\SpasvoProc\Output\Debug\ClipBoard.exe : fatal error LNK1120: 2 个无法解析的外部命令
========== 生成:  成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========

第一个是因为只声明没有实现

将soapStub.h文件最后的函数放在另个文件中实现

SOAP_FMAC5 int SOAP_FMAC6 plus(struct soap*, int num_1, int num_2, int *sum)
{
 *sum = num_1 + num_2;
 printf("sum = %d", sum);
 return SOAP_OK;
}

解决了一个问题

1>------ 已启动生成:  项目: ClipBoard, 配置: Debug Win32 ------
1>  TestWebServer.cpp
1>stdsoap2.obj : error LNK2001: 无法解析的外部符号 _namespaces
1>D:\MyCode\SpasvoProc\Output\Debug\ClipBoard.exe : fatal error LNK1120: 1 个无法解析的外部命令
========== 生成:  成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
百度一下

在工程中添加 WITH_NONAMESPACES宏//运行时会出错

#include "soap.nsmap"

编译通过

源文件中添加代码

#include "TestWebServer.h"
#include "soapStub.h"
#include "soap.nsmap"

SOAP_FMAC5 int SOAP_FMAC6 plus(struct soap*, int num_1, int num_2, int *sum)
{
	*sum = num_1 + num_2;
	printf("sum = %d", sum);
	return SOAP_OK;
}


int main()
{
	struct soap math_service;
	soap_init(&math_service);
	
	if (soap_bind(&math_service, NULL, 8696, 0) == SOAP_ERR)
	{
		printf("%s", "bind false\n");
		return -1;
	}
	printf("%s", "start server .....\n");
	while (true)
	{
		int client = (int)soap_accept(&math_service);
		if (client < 0){
			printf("%s", "accept client false!");
			return -2;
		}
		printf("%s", "connect success\n");
		soap_serve(&math_service);
		soap_end(&math_service);
	}

	return 0;
}


运行之后浏览器输入http://localhost:8696/

显示




客户端新建一个工程项目,可以添加服务器生成的文件,也可以重新生成,

在客户端源文件中添加代码

#include "soap.nsmap"
#include "soapStub.h"
#include <stdlib.h>

int main()
{
	struct soap math_client;
	soap_init(&math_client);
	int sum = 0;
	const char *p_webservice = "http://127.0.0.1:8696";
	soap_call_plus(&math_client, p_webservice, "", 60, 30, &sum);
	if (math_client.error){
		soap_print_fault(&math_client, stderr);
		return -1;
	}
	printf("webService : 60 + 30 = %d", sum);
	soap_end(&math_client);
	soap_done(&math_client);
	getchar();
	return 1;
}

运行结果

客户端:


服务器端:



在会遇到中文乱码的情况下,typename.dat加上xsd::string = std::wstring | wchar_t*


Couldn't find a matching Java operation for WSDD operation

webService 函数名小写




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值