使用gsoap访问java版本的SOAP服务器

本文介绍如何使用gSOAP框架访问AXIS2搭建的SOAP服务器,包括生成客户端代码的过程及具体实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

gsoap是C++版本的SOAP框架,我的服务器是AXIS2搭建的SOAP服务器。我将使用gsoap来访问我的服务器。

1.下载soap,并进入到下面文件夹(我下载到D盘)

D:\gsoap-2.8\gsoap\bin\win32

2.下载wsdl文件



我的在本地服务器,可以右键另存为 .xml文件。


3.将.xml文件生成C++版本的.h文件


4.生成SOAP的客户端C++代码


5.把生成的代码拷到VS工程下


6.编写客户端代码

WebService.cpp

/

#include "StdAfx.h"
#include <string>
#include <iostream>
#include "gsoap/ThothProductServiceSoap11Binding.nsmap"
#include "WebserviceClient.h"
#include "json/json.h"


#pragma comment(lib,"lib_json.lib")


const char server[] = "http://127.0.0.1:8888/axis2/services/ThothProductService";
std::string serverStr = "";

WebserviceClient::WebserviceClient()
{
	serviceProxy.soap_endpoint = server;
	DbgPrint("web server-> %s\n", server);
}

WebserviceClient::WebserviceClient(std::string server_ip, int port)
{
	serverStr = "http://";
	serverStr += server_ip;
	serverStr += ":";
	serverStr += std::to_string(port);
	serverStr += "/axis2/services/ThothProductService";

	serviceProxy.soap_endpoint = serverStr.data();

	DbgPrint("web server endpoint -> %s\n", serviceProxy.soap_endpoint);
}


WebserviceClient::~WebserviceClient()
{
	serviceProxy.destroy();
}

/************************************************************************
* 获取服务器版本                                                           
************************************************************************/
std::string WebserviceClient::getWebVersion()
{
	int res;
	std::string str;
	_ns1__getVersion req;
	_ns1__getVersionResponse resp;

	res = serviceProxy.getVersion(&req, resp);


	if (res == SOAP_OK)
	{
		str = resp.return_;
		//std::cout << "getVersion->" <<str<< std::endl;
		DbgPrint("getVersion-> %s\n", str.data());
	}
	else
	{
		str = "";
		//std::cout << "getVersion->nullptr" << std::endl;
		DbgPrint("getVersion->nullptr\n");
	}


	return str;
}

/************************************************************************
* 请求出厂编号
************************************************************************/
webservice_res WebserviceClient::requestSN(std::string user, std::string password, std::string client_no, std::string &batch_no, std::string &sn_no)
{
	int res;
	std::string str1;
	_ns1__getThothProductSN req;
	_ns1__getThothProductSNResponse resp;
	Json::CharReaderBuilder builder;
	Json::CharReader * reader(builder.newCharReader());
	Json::Value root;
	JSONCPP_STRING errs;

	//传递参数
	req.user =(char*) user.data();
	req.password = (char*)password.data();
	req.client_USCOREno = (char*)client_no.data();
	req.product_USCOREtype = 64;

	//--------------------------------------------------------
	res = serviceProxy.getThothProductSN(&req, resp);
	if (res == SOAP_OK)
	{
		str1 = resp.return_;

		
		DbgPrint("WebService Resop:%s\n", str1.data());

		if (str1.compare("[{\"status\":\"failed\"}]") == 0)
		{
			delete reader;
			return WEBSERVICE_RES_FAILED;
		}

		bool ok = reader->parse(str1.data(), str1.data() + str1.size(), &root, &errs);
		if (ok == true)
		{
			 batch_no = root["batch_no"].asString();
			 sn_no = root["sn"].asString();
			 //std::string client_no = root["client_no"].asString();

			//DbgPrint("$$$$$$$$% s--%s--%s\n", batch_no.data(), sn_no.data(), client_no.data());
			 delete reader;
			 return WEBSERVICE_RES_OK;
		}
		else
		{
			delete reader;
			return WEBSERVICE_RES_FAILED;
		}


		//return resp.return_;
	}
	else
	{
		return WEBSERVICE_RES_404;
	}
}

/************************************************************************
* 上传报表
************************************************************************/
webservice_res WebserviceClient::uploadReport(std::string user, std::string password, ReportMessage reportMessage)
{
	Json::Value root;
	root["gen_date"] = reportMessage.Gen_date();
	root["user"] = reportMessage.User();
	root["mac"] = reportMessage.Mac();
	root["sn_no"] = reportMessage.Sn_no();
	root["batch_no"] = reportMessage.Batch_no();
	root["client_no"] = reportMessage.Client_no();
	root["product_line_no"] = reportMessage.Product_line_no();
	root["cal_vol1"] = reportMessage.Cal_vol1();
	root["cal_adc1"] = reportMessage.Cal_adc1();
	root["cal_vol2"] = reportMessage.Cal_vol2();
	root["cal_adc2"] = reportMessage.Cal_adc2();
	root["cal_check_vol"] = reportMessage.Cal_check_vol();
	root["cal_check_temp"] = reportMessage.Cal_check_temp();
	root["result"] = reportMessage.Result();
	root["cal_cnt"] = reportMessage.Cal_cnt();

	std::string out = root.toStyledString();
	DbgPrint("json out: %s", out.data());

	///
	int res;
	std::string str1;
	_ns1__setProduct_USCORETTR03_USCOREReport req;
	_ns1__setProduct_USCORETTR03_USCOREReportResponse resp;

	req.user = (char*)user.data();
	req.password = (char*)password.data();
	req.json = (char*)out.data();

	res = serviceProxy.setProduct_USCORETTR03_USCOREReport(&req, resp);
	if (res == SOAP_OK)
	{
		str1 = resp.return_;
		if (str1.compare("{\"status\":\"success\"}") == 0)
		{
			return WEBSERVICE_RES_OK;
		}
		else
		{
			return WEBSERVICE_RES_FAILED;
		}

	}
	else
	{
		return WEBSERVICE_RES_404;
	}

}

WebService.h

#pragma once


#include "gsoap/soapThothProductServiceSoap11BindingProxy.h"
#include "ReportMessage.h"

enum webservice_res {
	WEBSERVICE_RES_OK = 0,
	WEBSERVICE_RES_404 ,
	WEBSERVICE_RES_FAILED
};


class WebserviceClient
{
public:
	WebserviceClient();
	WebserviceClient(std::string server_ip, int port);
	virtual ~WebserviceClient();

public:
	//请求出厂编号
	webservice_res requestSN(std::string user, std::string password, std::string client_no, std::string &batch_no, std::string &sn_no);
	//获取服务器版本
	std::string getWebVersion(void);
	
	//上传报表
	webservice_res uploadReport(std::string user, std::string password, ReportMessage reportMessage);

private:
	ThothProductServiceSoap11BindingProxy serviceProxy;

};


///

end~~~~~~






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搏哥聊技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值