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~~~~~~