ubuntu下gsoap的编译与使用

仅为记录,所有干货源自:https://blog.csdn.net/benkaoya/article/details/72424335,里面讲解清晰易懂。

下载gSOAP

开源代码:https://sourceforge.net/projects/gsoap2/
使用说明:https://www.genivia.com/dev.html

得到gsoap_2.8.91.zip文件。

编译gSOAP

sudo apt-get install bison
sudo apt-get install flex
sudo apt-get install openssl
sudo apt-get install libssl-dev
unzip gsoap_2.8.91.zip 
cd gsoap-2.8/
mkdir release
./configure --prefix=/home/chry/code/opensource/gsoap-2.8/release
make
make install

提取相关文件

创建gsoaptools文件夹,用于存放使用gsoap生成源码时常用的相关文件。
将以下文件拷贝到该文件夹:

gsoap-2.8/release/bin/soapcpp2
gsoap-2.8/release/bin/wsdl2h
gsoap-2.8/gsoap/stdsoap2.h
gsoap-2.8/gsoap/stdsoap2.c
gsoap-2.8/gsoap/stdsoap2.cpp
gsoap-2.8/gsoap/typemap.dat
gsoap-2.8/gsoap/import/
gsoap-2.8/gsoap/custom/
gsoap-2.8/gsoap/plugin/

这些都是接下来需要用到的文件,最终效果如下:

chry@chry-PC:~/code/opensource$ mkdir  gsoaptools
chry@chry-PC:~/code/opensource$ cp -rf gsoap-2.8/release/bin/*  gsoaptools/
chry@chry-PC:~/code/opensource$ cp -rf gsoap-2.8/gsoap/stdsoap2.*  gsoaptools/
chry@chry-PC:~/code/opensource$ cp -rf gsoap-2.8/gsoap/typemap.dat  gsoaptools/
chry@chry-PC:~/code/opensource$ cp -rf gsoap-2.8/gsoap/plugin/  gsoaptools/
chry@chry-PC:~/code/opensource$ cp -rf gsoap-2.8/gsoap/import/  gsoaptools/
chry@chry-PC:~/code/opensource$ cp -rf gsoap-2.8/gsoap/custom/  gsoaptools/
chry@chry-PC:~/code/opensource$ ls -l gsoaptools/
总用量 18756
drwxr-xr-x 2 chry chry     4096 9月  17 11:50 custom
drwxr-xr-x 2 chry chry     4096 9月  17 11:50 import
drwxr-xr-x 2 chry chry     4096 9月  17 11:50 plugin
-rwxr-xr-x 1 chry chry  1788536 9月  17 11:45 soapcpp2
-rw-r--r-- 1 chry chry   635235 9月  17 11:54 stdsoap2.c
-rw-r--r-- 1 chry chry   635235 9月  17 11:54 stdsoap2.cpp
-rw-r--r-- 1 chry chry   155732 9月  17 11:54 stdsoap2.h
-rw-r--r-- 1 chry chry    26260 9月  17 11:51 typemap.dat
-rwxr-xr-x 1 chry chry 15933544 9月  17 11:45 wsdl2h
chry@chry-PC:~/code/opensource$ 

可以看到stdsoap2.cstdsoap2.cpp两个文件是一毛一样的。我习惯使用前者,毕竟珍爱生命,远离C++

至此gsoap-2.8文件夹可以删掉了。

演练gSOAP

实例:国内手机号码归属地查询

新建outdir文件夹用于存放gSOAP最终生成的源码,路径随意,这里我是放在和gsoaptools目录下:

mkdir outdir

生成头文件

以下命令会在outdir目录生成mobilecode.h文件:

./wsdl2h -o outdir/mobilecode.h -c -s -t typemap.dat http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

其中-c为产生纯c代码,默认生成 c++代码;-s为不使用STL库,-t为typemap.dat的标识。详情可通过./wsdl2h -help查看帮助。
这里的WSDL文件,可以在wsdl2h命令中在线下载,也可以先下载到本地,然后引用本地WSDL文件,我这里是采用在线下载方式。

生成框架源码

使用soapcpp2工具,根据mobilecode.h头文件产生框架代码,执行以下命令:

./soapcpp2 -2 -C -c -L -x -I import outdir/mobilecode.h -d outdir

-2表示生成SOAP 1.2版本的代码,-C表示仅生成客户端的代码(服务端的不要),-c生成C语言代码,-L表示不产生soapClientLib.c和soapServerLib.c文件,-d指定代码生成路径,-I制定import目录路径,这里可以是没有空格的。详情可使用./soapcpp2 -help查看帮助。

代码会生成在outdir目录,同时拷贝gsoaptools目录下的stdsoap2.cstdsoap2.h文件到outdir目录,mobilecode.h文件可以删掉了。
最终效果如下:

chry@chry-PC:~/code/opensource/gsoaptools$ cp stdsoap2.c outdir/
chry@chry-PC:~/code/opensource/gsoaptools$ cp stdsoap2.h outdir/
chry@chry-PC:~/code/opensource/gsoaptools$ rm -rf outdir/mobilecode.h 
chry@chry-PC:~/code/opensource/gsoaptools$ ls -l outdir/
总用量 972
-rw-r--r-- 1 chry chry    683 9月  17 13:36 MobileCodeWSSoap.nsmap
-rw-r--r-- 1 chry chry  90553 9月  17 13:36 soapC.c
-rw-r--r-- 1 chry chry  12208 9月  17 13:36 soapClient.c
-rw-r--r-- 1 chry chry  63682 9月  17 13:36 soapH.h
-rw-r--r-- 1 chry chry  18962 9月  17 13:36 soapStub.h
-rw-r--r-- 1 chry chry 635235 9月  17 13:37 stdsoap2.c
-rw-r--r-- 1 chry chry 155732 9月  17 13:37 stdsoap2.h
chry@chry-PC:~/code/opensource/gsoaptools$ 

其中,soapStub.h文件列出了所有web services接口,soapC.csoapClient.c为SOAP协议通信过程的实现。

测试web services接口

新建main.c文件:

#include 
#include 
#include 
#include "soapStub.h"
#include "MobileCodeWSSoap.nsmap"

void getMobileCodeInfo(char *mobileCode)
{
    struct soap *soap = NULL;
    const char  *endpoint = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx";
    struct _ns1__getMobileCodeInfo          req;
    struct _ns1__getMobileCodeInfoResponse  resp;

    soap = soap_new();                      // allocate and initalize a context

    soap_set_mode(soap, SOAP_C_UTFSTRING);  // support multibyte string(for Chinese)

    memset(&req, 0x00, sizeof(req));
    req.mobileCode = mobileCode;
    req.userID     = NULL;

    if(SOAP_OK == soap_call___ns1__getMobileCodeInfo(soap, endpoint, NULL, &req, &resp)) 
    {
        if (NULL != resp.getMobileCodeInfoResult) 
        {
            printf("%s\n", resp.getMobileCodeInfoResult);
        }
    }

    soap_destroy(soap);     // delete deserialized objects
    soap_end(soap);         // delete allocated data
    soap_free(soap);        // free the soap struct context data
}

int main(int argc, char **argv)
{
    if (argc < 2) 
    {
        return 0;
    }

    getMobileCodeInfo(argv[1]);

    return 0;
}

编译运行:

chry@chry-PC:~/code/opensource/gsoaptools/outdir$ gcc -o getMobileCodeInfo *.c
chry@chry-PC:~/code/opensource/gsoaptools/outdir$ ./getMobileCodeInfo 18612345678
18612345678:北京 北京 北京联通GSM卡
chry@chry-PC:~/code/opensource/gsoaptools/outdir$ 

你可能感兴趣的:(Linux)