学习matlab(十七)——信号处理

什么是BIO

  返回  

gsoap开发webservice服务端

2021/8/20 21:31:11 浏览:

1 首先下载goap,下载地址 https://sourceforge.net/projects/gsoap2/files/

我下载的是 gsoap_2.8.116.zip,下载完成后解压

  2 编写头文件callService.h,定义一个callService方法,其中包含5个字符串类型的入参arg0,arg1,arg2,arg3,arg4,一个字符串类型的出参result,头文件内容如下(注意前5行不要省略)

//gsoap ns service name: callService
//gsoap ns service style: rpc
//gsoap ns service encoding: encoded
//gsoap ns service namespace: http://127.0.0.1:8090/callService.wsdl
//gsoap ns service location: http://127.0.0.1:8090/callService
//gsoap ns schema  namespace:    urn:callService
int ns__callService(std::string arg0, std::string arg1, std::string arg2, std::string arg3, std::string arg4, std::string* result);

3 进入gsoap目录soapcpp2.exe所在目录下,我的是E:\tools\gsoap\gsoap-2.8.116\gsoap\bin\win64,将callService.h拷贝到该目录下,在该目录打开命令行窗口,输入:soapcpp2.exe -i callService.h 后回车

 4 在该目录下生成12个文件

5 新建win32控制台工程callService,将该目录12个文件及E:\tools\gsoap\gsoap-2.8.116\gsoap目录下的stdsoap2.hstdsoap2.cpp文件,合计14个文件加入到工程中

callService.cpp文件代码

// callService.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "callService.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 唯一的应用程序对象

CWinApp theApp;

using namespace std;

#define _CRT_SECURE_NO_WARNINGS

#include "callService.nsmap"
#include "soapcallServiceService.h"

int http_get(struct   soap   *soap)
{
	FILE *fd = fopen("D:\\Projects\\YBsoft_1\\HIIP\\trunk\\JCPT\\vs2008\\callService\\gsoap\\callService.wsdl", "rb");//open WSDL file to copy

	if (NULL == fd)
	{
		return 404;//return HTTP not found error
	}

	soap->http_content = "text/xml";  //HTTP header with text /xml content
	soap_response(soap, SOAP_FILE);

	while (1)
	{
		size_t r = fread(soap->tmpbuf, 1, sizeof(soap->tmpbuf), fd);
		if (!r)
		{
			break;
		}
		if (soap_send_raw(soap, soap->tmpbuf, r))
		{
			break; //cannot send, but little we can do about that
		}
	}
	fclose(fd);
	soap_end_send(soap);
	return SOAP_OK;
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// 初始化 MFC 并在失败时显示错误
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: 更改错误代码以符合您的需要
		_tprintf(_T("错误: MFC 初始化失败\n"));
		nRetCode = 1;
	}
	else
	{
		// TODO: 在此处为应用程序的行为编写代码。
	}

	callServiceService service(SOAP_C_UTFSTRING, SOAP_C_UTFSTRING);
	service.fget = http_get;
	service.bind(NULL, 8090, 100);
	int socketid = 0;
	do
	{
		socketid = service.accept();
		if (socketid < 0)
		{
			service.soap_stream_fault(std::cerr);
		}
		else
		{
			CString strMsg;
			strMsg.Format("accepted connection from IP=%d.%d.%d.%d socket=%d\n", 
				(service.ip >> 24)&0xFF, (service.ip >> 16)&0xFF, (service.ip >> 8)&0xFF, service.ip&0xFF, socketid);
			cout << strMsg;
		}

		int s = service.serve();
		if (s != 0)
		{
			service.soap_stream_fault(std::cerr);
		}
	} while (1);

	service.reset();
	service.destroy();

	system("pause");

	return nRetCode;
}

 6 callService方法实现

6.1 在soapcallServiceService.h中已经声明了callService方法

virtual int callService(const std::string& arg0, const std::string& arg1, const std::string& arg2, const std::string& arg3, const std::string& arg4, std::string *result) SOAP_PURE_VIRTUAL;

6.2 需要在soapcallServiceService.cpp增加其实现,实现你想要的功能

int callServiceService::callService(const std::string& arg0, const std::string& arg1, const std::string& arg2, const std::string& arg3, const std::string& arg4, std::string *result) SOAP_PURE_VIRTUAL
{
	std::string received = UTF8ToGBK(arg0);
	std::cout << "接收到客户端arg0参数:" << received;
	std::string gbk = std::string("服务端响应:调个毛线!======") ;
	std::string utf = GBKToUTF8(gbk);
	(*result) = utf + arg0;
	return SOAP_OK;
}

7 编辑callSerivice.wsdl,可以把多余的无用信息给删除掉,整理为:

<?xml version="1.0" encoding="UTF-8"?>
<definitions name="callService"
  targetNamespace="http://127.0.0.1:8090/callService.wsdl"
  xmlns:tns="http://127.0.0.1:8090/callService.wsdl"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:ns="urn:callService"
  xmlns:SOAP="http://schemas.xmlsoap.org/wsdl/soap/"
  xmlns:HTTP="http://schemas.xmlsoap.org/wsdl/http/"
  xmlns:MIME="http://schemas.xmlsoap.org/wsdl/mime/"
  xmlns:DIME="http://schemas.xmlsoap.org/ws/2002/04/dime/wsdl/"
  xmlns:WSDL="http://schemas.xmlsoap.org/wsdl/"
  xmlns="http://schemas.xmlsoap.org/wsdl/">

<types>

  <schema targetNamespace="urn:callService"
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:ns="urn:callService"
    xmlns="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="unqualified"
    attributeFormDefault="unqualified">
    <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
  </schema>

</types>

<message name="callServiceRequest">
  <part name="arg0" type="xsd:string"/>
  <part name="arg1" type="xsd:string"/>
  <part name="arg2" type="xsd:string"/>
  <part name="arg3" type="xsd:string"/>
  <part name="arg4" type="xsd:string"/>
</message>

<message name="callServiceResponse">
  <part name="result" type="xsd:string"/>
</message>

<portType name="callServicePortType">
  <operation name="callService">
    <input message="tns:callServiceRequest"/>
    <output message="tns:callServiceResponse"/>
  </operation>
</portType>

<binding name="callService" type="tns:callServicePortType">
  <SOAP:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
  <operation name="callService">
    <SOAP:operation style="rpc" soapAction=""/>
    <input>
          <SOAP:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:callService"/>
    </input>
    <output>
          <SOAP:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:callService"/>
    </output>
  </operation>
</binding>

<service name="callService">
  <port name="callService" binding="tns:callService">
    <SOAP:address location="http://127.0.0.1:8090/callService"/>
  </port>
</service>

</definitions>

8 编译工程,并启动win32控制台程序

9 在浏览器输入http://127.0.0.1:8090/callService?wsdl,验证服务是否正常,此处显示的wsdl协议内容即步骤7中工程文件callSerivice.wsdl内容

10 通过soapui验证接口请求和返回结果

10.1 加载wsdl资源

10.2 请求callService接口

10.3 控制台接收

  到此,完毕! 

联系我们

如果您对我们的服务有兴趣,请及时和我们联系!

服务热线:18288888888
座机:18288888888
传真:
邮箱:888888@qq.com
地址:郑州市文化路红专路93号