gSOAP安裝配置+使用案例參考+參考鏈接

環境搭建與配置

gSOAP下載地址:https://sourceforge.net/projects/gsoap2/files/
相關配置參考:gsoap_2.8.33.zip安裝與編譯
配置完成後,根據官網文檔:https://www.genivia.com/dev.html
編寫一個hello進行測試

gSOAP的使用案例參考

新建文件 hello.h

// hello.h
int ns__hello(std::string name, std::string& greeting);

終端運行 soapcpp2 hello.h ,如果你不在此程序的路徑,則輸入完整路徑編譯
/usr/local/gSOAP/bin/soapcpp2 hello.h
在這裏插入圖片描述
然後我們新建 hello.cpp

// hello.cpp
#include "soapH.h"  // include the generated source code headers
#include "ns.nsmap" // include XML namespaces
int main()
{
  return soap_serve(soap_new());
}
int ns__hello(struct soap *soap, std::string name, std::string& greeting)
{
  greeting = "Hello " + name;
  return SOAP_OK;
}

終端運行 c++ -o hello.cgi hello.cpp soapC.cpp soapServer.cpp stdsoap2.cpp
報錯如下
在這裏插入圖片描述
沒有stdsoap2.cpp這個文件
這個文件可以在gSOAP解壓後的文件夾裏找到 gsoap-2.8/gsoap
在這裏插入圖片描述
我們將 stdsoap2.cppstdsoap2.h 這兩個文件拷貝到源程序的目錄,再次執行

c++ -o hello.cgi hello.cpp soapC.cpp soapServer.cpp stdsoap2.cpp
在這裏插入圖片描述
編譯完成 生成 hello.cgi


看到 cgi程序,我就想到了之前我用的BOA,那麼我們來試試看吧。
BOA相關參考:嵌入式web服務器BOA+CGI+HTML+MySQL項目實戰——Linux
老規矩 運行boa

sudo ./boa

將 cgi程序放在 /var/www/cgi-bin 目錄下
在這裏插入圖片描述
然後在瀏覽器訪問我們的網址 http://localhost:886/cgi-bin/hello.cgi
效果如下:
在這裏插入圖片描述
一個SOAP的XML,SOAP相關知識可以參考:SOAP菜鳥教程


如何實現和部署gSOAPWeb服務API

回到官方文檔 如何實現和部署gSOAPWeb服務API。其教程:傳送門

終端運行 wsdl2h -c -o calc.h http://www.genivia.com/calc.wsdl
同樣,我的是
/usr/local/gSOAP/bin/wsdl2h -c -o calc.h http://www.genivia.com/calc.wsdl
在這裏插入圖片描述
繼續根據教程走
終端運行 soapcpp2 -SL calc.h
同樣,我的是 /usr/local/gSOAP/bin/soapcpp2 -SL calc.h
在這裏插入圖片描述
很順利哈,我們繼續。
官方教程讓我們運行 cc -o calc.cgi calccgi.c soapClient.c soapC.c stdsoap2.c
Linux 下 的 cc 和 gcc
這cc是個啥?我運行後。。。
在這裏插入圖片描述
又缺少文件了 後者我還能理解,前者這個文件從哪來的?
在哪也找不到 calccgi.c 這個文件,後來看了文檔發現,官方提供了 此文件的超鏈接。。。
那麼我們 從 https://www.genivia.com/files/calccgi.c 複製代碼,自建一個 calccgi.c 文件。

/*
	calccgi.c

	Example calculator service in C

	Compilation in C (see samples/calc/calc.h):
	$ wsdl2h -c -o calc.h http://www.genivia.com/calc.wsdl
	$ soapcpp2 -SL calc.h
	$ cc -o calc.cgi calccgi.c stdsoap2.c soapC.c soapServer.c
	where stdsoap2.c is in the 'gsoap' directory, or use libgsoap:
	$ cc -o calc.cgi calccgi.c soapC.c soapServer.c -lgsoap

--------------------------------------------------------------------------------
gSOAP XML Web services tools
Copyright (C) 2001-2017, Robert van Engelen, Genivia, Inc. All Rights Reserved.
This software is released under one of the following two licenses:
GPL or Genivia's license for commercial use.
--------------------------------------------------------------------------------
GPL license.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA

Author contact information:
[email protected] / [email protected]
--------------------------------------------------------------------------------
A commercial use license is available from Genivia, Inc., [email protected]
--------------------------------------------------------------------------------
*/

#include "soapH.h"
#include "calc.nsmap"

int main(int argc, char **argv)
{
  struct soap soap;

  soap_init1(&soap, SOAP_XML_INDENT);

  soap_serve(&soap);	/* serve as CGI application */

  soap_destroy(&soap);
  soap_end(&soap);
  soap_done(&soap);

  return 0;
} 

int ns2__add(struct soap *soap, double a, double b, double *result)
{
  (void)soap;
  *result = a + b;
  return SOAP_OK;
} 

int ns2__sub(struct soap *soap, double a, double b, double *result)
{
  (void)soap;
  *result = a - b;
  return SOAP_OK;
} 

int ns2__mul(struct soap *soap, double a, double b, double *result)
{
  (void)soap;
  *result = a * b;
  return SOAP_OK;
} 

int ns2__div(struct soap *soap, double a, double b, double *result)
{
  if (b)
    *result = a / b;
  else
  {
    char *s = (char*)soap_malloc(soap, 1024);
    (SOAP_SNPRINTF(s, 1024, 100), "<error xmlns=\"http://tempuri.org/\">Can't divide %f by %f</error>", a, b);
    return soap_sender_fault(soap, "Division by zero", s);
  }
  return SOAP_OK;
} 

int ns2__pow(struct soap *soap, double a, double b, double *result)
{
  *result = pow(a, b);
  if (soap_errno == EDOM)	/* soap_errno is like errno, but compatible with Win32 */
  { char *s = (char*)soap_malloc(soap, 1024);
    (SOAP_SNPRINTF(s, 1024, 100), "<error xmlns=\"http://tempuri.org/\">Can't raise %f to %f</error>", a, b);
    return soap_sender_fault(soap, "Power function domain error", s);
  }
  return SOAP_OK;
} 

那麼現在我們還缺少 soapClient.c,我們之前生成了 soapClient.cpp 文件
打開項目文件夾,我發現一個很尷尬的事情,我們生成了server的c和cpp文件,但client卻只有c。
在這裏插入圖片描述
我無語了,那我就自建 soapClient.c,拷貝soapClient.cpp貼入soapClient.c。
再次運行 cc -o calc.cgi calccgi.c soapClient.c soapC.c stdsoap2.c
報錯如下:
在這裏插入圖片描述
果然沒有那麼容易,看到 std::string,這事情不簡單了。
我們重新執行 /usr/local/gSOAP/bin/soapcpp2 calc.h
成功生成了 soapClient.c 文件了
在這裏插入圖片描述
但爲什麼官方提供的命令是加個 -SL ?
我查看了下載來的gSOAP文檔:太平洋那下載的,自行斟酌
在這裏插入圖片描述
我們機翻一下看看, -S -L 是幹什麼的
在這裏插入圖片描述
只生成服務器端代碼,官方教程爲什麼要這麼做,不知道爲啥,既然我們現在生成好了,我們再編譯看看,終端運行 cc -o calc.cgi calccgi.c soapClient.c soapC.c stdsoap2.c
在這裏插入圖片描述

對‘soap_serve’未定義的引用

事情還是沒有那麼順利,又報錯了。
我們試試官網的 calc.h 的源碼貼入看看
再次執行 /usr/local/gSOAP/bin/soapcpp2 calc.h
在這裏插入圖片描述
沒有效果呀  ̄へ ̄
會看之前的hello.cpp 卻沒有報錯,區別就是c和cpp?
soapServer.cpp
在這裏插入圖片描述
soapServer.c
在這裏插入圖片描述
而 soapClient.c和cpp壓根沒這函數???
那我們加入 soapServer.c 一起編譯!

cc -o calc.cgi calccgi.c soapClient.c soapC.c stdsoap2.c soapServer.c
在這裏插入圖片描述
(⊙ˍ⊙) 錯誤消失了。。。 我服了
pow函數大家應該都比較熟悉,鏈接 pow 所在的數學庫 libm

cc -o calc.cgi calccgi.c soapClient.c soapC.c stdsoap2.c soapServer.c -lm
在這裏插入圖片描述
總算過去了! ╥﹏╥…
拋到BOA看看
在這裏插入圖片描述
和hello.cgi 一樣。

回到官方教程

部署CGI服務

我事先已經裝好了Apache,那我們繼續往下走
要編譯C服務器:

cc -o calcserver.cgi calcserver.c soapC.c soapServer.c stdsoap2.c
恩,calcserver.c 這文件又沒有。同樣官方給了鏈接,進去看看:傳送門
在這裏插入圖片描述
typemap.dat 在gSOAP中可以搜到,我們先手動建個 calc.h

//gsoap ns service name:            calc Simple calculator service described at https://www.genivia.com/dev.html
//gsoap ns service protocol:        SOAP
//gsoap ns service style:           rpc
//gsoap ns service encoding:        encoded
//gsoap ns service namespace:       http://websrv.cs.fsu.edu/~engelen/calc.wsdl
//gsoap ns service location:        http://websrv.cs.fsu.edu/~engelen/calcserver.cgi

//gsoap ns schema namespace:        urn:calc

//gsoap ns service method: add Sums two values
int ns__add(double a, double b, double *result);

//gsoap ns service method: sub Subtracts two values
int ns__sub(double a, double b, double *result);

//gsoap ns service method: mul Multiplies two values
int ns__mul(double a, double b, double *result);

//gsoap ns service method: div Divides two values
int ns__div(double a, double b, double *result);

//gsoap ns service method: pow Raises a to b
int ns__pow(double a, double b, double *result);

其他都不管,來到client

爲客戶端應用程序構建步驟

新建 calcclient.c

#include "soapH.h"
#include "calc.nsmap"

/* the Web service endpoint URL */
const char server[] = "http://websrv.cs.fsu.edu/~engelen/calcserver.cgi";

int main(int argc, char **argv)
{
  struct soap *soap = soap_new1(SOAP_XML_INDENT); /* new context */
  double a, b, result;
  if (argc < 4)
  {
    fprintf(stderr, "Usage: [add|sub|mul|div|pow] num num\n");
    exit(1);
  }
  a = strtod(argv[2], NULL);
  b = strtod(argv[3], NULL);
  switch (*argv[1])
  {
    case 'a':
      soap_call_ns__add(soap, server, "", a, b, &result);
      break;
    case 's':
      soap_call_ns__sub(soap, server, "", a, b, &result);
      break;
    case 'm':
      soap_call_ns__mul(soap, server, "", a, b, &result);
      break;
    case 'd':
      soap_call_ns__div(soap, server, "", a, b, &result);
      break;
    case 'p':
      soap_call_ns__pow(soap, server, "", a, b, &result);
      break;
    default:
      fprintf(stderr, "Unknown command\n");
      exit(1);
  }
  if (soap->error)
    soap_print_fault(soap, stderr);
  else
    printf("result = %g\n", result);
  soap_destroy(soap); /* delete deserialized objects */
  soap_end(soap);     /* delete heap and temp data */
  soap_free(soap);    /* we're done with the context */
  return 0;
}

使用以下方法爲客戶端生成服務和數據綁定接口:

soapcpp2 -c -r -CL calc.h
在這裏插入圖片描述
有產生警告,暫時不管。

構建示例客戶端應用程序

cc -o calcclient calcclient.c stdsoap2.c soapC.c soapClient.c
在這裏插入圖片描述
沒有問題。

實現CGI服務器應用程序

新建 calcserver.c

#include "soapH.h"
#include "calc.nsmap"

int main()
{
  struct soap *soap = soap_new(); /* new context */
  soap_serve(soap);   /* serve CGI request */
  soap_destroy(soap); /* delete deserialized objects */
  soap_end(soap);     /* delete heap and temp data */
  soap_free(soap);    /* we're done with the context */
  return 0;
} 
/* service operation function implementation */
int ns__add(struct soap *soap, double a, double b, double *result)
{
  *result = a + b;
  return SOAP_OK;
} 
/* service operation function implementation */
int ns__sub(struct soap *soap, double a, double b, double *result)
{
  *result = a - b;
  return SOAP_OK;
} 
/* service operation function implementation */
int ns__mul(struct soap *soap, double a, double b, double *result)
{
  *result = a * b;
  return SOAP_OK;
} 
/* service operation function implementation */
int ns__div(struct soap *soap, double a, double b, double *result)
{
  if (b)
    *result = a / b;
  else
    return soap_sender_fault(soap, "Division by zero", NULL);
  return SOAP_OK;
} 
/* service operation function implementation */
int ns__pow(struct soap *soap, double a, double b, double *result)
{
  *result = pow(a, b);
  if (soap_errno == EDOM) /* soap_errno is like errno, but portable */
    return soap_sender_fault(soap, "Power function domain error", NULL);
  return SOAP_OK;
}

官方教程說
在這裏插入圖片描述
那我們繼續往下走。

實現獨立的服務器應用程序

此示例展示了一個獨立的迭代服務器,該服務器接受主機端口上的傳入請求。該程序與CGI服務相同,但在循環中通過套接字進行服務請求調度除外:

#include "soapH.h"
#include "calc.nsmap"
#include "plugin/threads.h"

int port = 8080;

void *process_request(void *arg)
{
  struct soap *soap = (struct soap*)arg;
  THREAD_DETACH(THREAD_ID);
  if (soap)
  {
    soap_serve(soap);
    soap_destroy(soap);
    soap_end(soap);
    soap_free(soap);
  }
  return NULL;
}

int main()
{
  SOAP_SOCKET m;                                    /* master socket */
  struct soap *soap = soap_new1(SOAP_IO_KEEPALIVE); /* new context with HTTP keep-alive enabled */
  soap->send_timeout = soap->recv_timeout = 5;      /* 5 sec socket idle timeout */
  soap->transfer_timeout = 30;                      /* 30 sec message transfer timeout */
  m = soap_bind(soap, NULL, port), 100);
  if (soap_valid_socket(m))
  {
    while (soap_valid_socket(soap_accept(soap)))
    {
      THREAD_TYPE tid;
      void *arg = (void*)soap_copy(soap);
      /* use updated THREAD_CREATE from plugin/threads.h https://www.genivia.com/files/threads.zip */
      if (arg)
        while (THREAD_CREATE(&tid, (void*(*)(void*))process_request, arg))
          sleep(1);
    }
  }
  soap_print_fault(soap, stderr);
  soap_destroy(soap); /* delete deserialized objects */
  soap_end(soap);     /* delete heap and temp data */
  soap_free(soap);    /* we're done with the context */
  return 0;
}
...
/* service operation functions */
...

這個暫且忽略,我們往下走

爲服務器應用程序構建步驟

使用以下方法爲服務器端生成服務和數據綁定接口:

soapcpp2 -c -r -SL calc.h

在這裏插入圖片描述
同樣有警告
在這裏插入圖片描述
我們繼續往下
構建示例服務器應用程序:

cc -o calcserver calcserver.c stdsoap2.c soapC.c soapServer.c
在這裏插入圖片描述
修改爲 cc -o calcserver calcserver.c stdsoap2.c soapC.c soapServer.c -lm
在這裏插入圖片描述
官方教程接近尾聲

運行示例

在這裏插入圖片描述
根據官方教程 我們執行 calcclient程序 ./calcclient add 2 3
在這裏插入圖片描述
成功計算出結果了。恩 這和 calcserver 有關係嗎?
我們刪了 calcserver 看看,恩 你會發現 還是能輸出結果。。。


回到前一個教程
要編譯C服務器:

cc -o calcserver.cgi calcserver.c soapC.c soapServer.c stdsoap2.c
在這裏插入圖片描述
同樣我們加上 -lm
cc -o calcserver.cgi calcserver.c soapC.c soapServer.c stdsoap2.c -lm
在這裏插入圖片描述
在這裏插入圖片描述
我們直接執行這條命令看看 ./calcserver.cgi < calc.add.req.xml
在這裏插入圖片描述
這將顯示自動生成的示例soap/xml消息的服務響應。calc.add.req.xml
那依照教程,將 cgi 放在cgi-bin 下,我的是BOA
在這裏插入圖片描述
我們提供瀏覽器訪問看看
在這裏插入圖片描述
效果還是一樣。
那差不多就這樣結束了,官方教程還有一大堆,可自行查閱:傳送門
目錄:

  • 瞭解XML SOAP、REST、WSDL和XML模式
  • 如何實現和部署gSOAPWeb服務
  • 如何鏈接多個C++服務類以接受一個服務器端口上的請求
  • 如何使獨立服務爲HTTPGET請求提供服務
  • 如何使獨立服務爲HTTP POST、PUT、修補和刪除請求提供服務
  • 如何在gSOAP中使用JSON和JSONPath
  • 如何通過HTTP代理連接並使用HTTP承載或基本/摘要身份驗證、NTLM身份驗證和WS-安全身份驗證
  • 如何用指數退避重試連接
  • 如何處理HTTP重定向
  • 如何啓用HTTP訪問控制(CORS)標頭
  • 如何添加自定義HTTP報頭
  • 如何在gSOAP客戶機中使用curl
  • 如何在客戶端和獨立的gSOAP服務器上使用HTTPS tls/ssl
  • 如何使用OpenSSL啓用FIPS 140-2
  • 如何使用OpenSSL和gSOAP創建自簽名證書
  • 如何將PEM格式的證書轉換爲MS Windows的CER格式
  • 如何使用GNUTLS創建自簽名證書
  • 如何通過超時和錯誤處理程序增強應用程序的健壯性
  • 如何設置和獲取SOAP頭
  • 如何設置和獲取SOAP故障
  • 如何通過包裝xml請求和響應元素從XSD創建新的SOAP服務操作

別人的案例

我們看看別人的案例試試 gsoap_2.8.33.zip安裝與編譯

新建 add.h

複製過來運行發現有問題,改爲

//gsoapopt cw
//gsoap ns2 schema namespace: urn:add
//gsoap ns2 schema form: unqualified
//gsoap ns2 service name: add
//gsoap ns2 service type: addPortType   
//add http://schemas.xmlsoap.org/soap/encoding/
//gsoap ns2  service method-action:     add ""
 
int ns2__add( int num1, int num2, int* sum );
int ns2__sub( int num1, int num2, int *sub);

然後 執行 /usr/local/gSOAP/bin/soapcpp2 -c add.h
在這裏插入圖片描述

服務端,創建 addserver.c

#include <string.h>
#include <stdio.h>
#include "soapH.h"
 
#include "add.nsmap"
 
int main(int argc, char **argv)
{
    int m, s;
    struct soap add_soap;
    soap_init(&add_soap);
    soap_set_namespaces(&add_soap, namespaces);
    if (argc < 2) {
        printf("usage: %s <server_port> /n", argv[0]);
        exit(1);
    } else {
    m = soap_bind(&add_soap, NULL, atoi(argv[1]), 100);
        if (m < 0) {
            soap_print_fault(&add_soap, stderr);
            exit(-1);
      }
     fprintf(stderr, "Socket connection successful: master socket = %d\n", m);
     for (;;) {
          s = soap_accept(&add_soap);
          if (s < 0) {
              soap_print_fault(&add_soap, stderr);
              exit(-1);
       }
     fprintf(stderr, "Socket connection successful: slave socket = %d\n", s);
     soap_serve(&add_soap);
     soap_end(&add_soap);
        }
    }
    return 0;
}
 
int ns2__add(struct soap *add_soap, int num1, int num2, int *sum)
{
    *sum = num1 + num2;
    return 0;
}
int ns2__sub(struct soap *sub_soap, int num1, int num2, int *sub)
{
    *sub = num1 - num2;
    return 0;
}

客戶端,新建 addclient.c

#include <string.h>
#include <stdio.h>
#include "soapStub.h"
 
#include "add.nsmap"
 
int add(const char *server, int num1, int num2, int *sum);
int sub(const char *server, int num1, int num2, int *sub);
 
int main(int argc, char **argv)
{
    int result = -1;
    char server[128] = {0};
    int num1;
    int num2;
    int sum;
    if (argc < 4) {
        printf("usage: %s <ip:port> num1 num2 /n", argv[0]);
        exit(1);
    }
    strcpy(server,argv[1]);
    num1 = atoi(argv[2]);
    num2 = atoi(argv[3]);
    result = add(server, num1, num2, &sum);
    if (result != 0) {
        printf("soap error, errcode=%d\n", result);
    } else {
        printf("%d + %d = %d\n", num1, num2, sum);
    }
    result = sub(server, num1, num2, &sum);
    if (result != 0) {
        printf("soap error, errcode=%d\n", result);
    } else {
        printf("%d - %d = %d\n", num1, num2, sum);
    }
    return 0;
}
 
int add(const char *server, int num1, int num2, int *sum)
{
   struct soap add_soap;
    int result = 0;
    soap_init(&add_soap);
    soap_set_namespaces(&add_soap, namespaces);
    soap_call_ns2__add(&add_soap, server, NULL, num1, num2, sum);
    printf("server is %s, num1 is %d, num2 is %d\n", server, num1, num2);
    if (add_soap.error) {
        printf("soap error: %d, %s, %s\n", add_soap.error, *soap_faultcode(&add_soap), *soap_faultstring(&add_soap));
        result = add_soap.error;
    }
    soap_end(&add_soap);
    soap_done(&add_soap);
    return result;
}
 
int sub(const char *server, int num1, int num2, int *sub)
{
    struct soap add_soap;
    int result = 0;
    soap_init(&add_soap);
    soap_set_namespaces(&add_soap, namespaces);
    soap_call_ns2__sub(&add_soap, server, NULL, num1, num2, sub);
    printf("server is %s, num1 is %d, num2 is %d\n", server, num1, num2);
    if (add_soap.error) {
       printf("soap error: %d, %s, %s\n", add_soap.error, *soap_faultcode(&add_soap), *soap_faultstring(&add_soap));
        result = add_soap.error;
    }
    soap_end(&add_soap);
    soap_done(&add_soap);
    return result;
}

stdsoap2.cstdsoap2.h,在gSOAP裏面搜出來,拷貝過來,上文已經準備好了

編寫Makefile

新建 Makefile
直接複製過來多半是不行的,看到第一行 GSOAP_ROOT ,根目錄改成自己的,那我應該是如下

GSOAP_ROOT = /home/hlx/gsoap-2.8/gsoap
WSNAME = add
 
CC = g++ -g -DWITH_NONAMESPACES
INCLUDE = -I $(GSOAP_ROOT)
 
SERVER_OBJS =soapC.o stdsoap2.o soapServer.o $(WSNAME)server.o 
CLIENT_OBJS =soapC.o stdsoap2.o soapClient.o $(WSNAME)client.o 
all: server
server: $(SERVER_OBJS) 
	$(CC) $(INCLUDE) -o $(WSNAME)server $(SERVER_OBJS)    
client: $(CLIENT_OBJS) 
	$(CC) $(INCLUDE) -o $(WSNAME)client $(CLIENT_OBJS)
clean:
	rm -f *.o *.xml *.a *.wsdl *.nsmap soap* $(WSNAME)Stub.* $(WSNAME)server ns.xsd $(WSNAME)test

可以 到相應目錄執行 pwd
在這裏插入圖片描述

編譯

終端執行

make server
make client

在這裏插入圖片描述
我make掛了,tab分割符,不能用空格,我們修改一下。
在這裏插入圖片描述
make完畢。編譯生成服務端執行程序 addserver 和客戶端執行程序 addclient

執行

終端執行 ./addserver 8888 運行服務端程序
在這裏插入圖片描述
終端打印出“Socket connection successful: master socket = 3”,那麼你的server已經在前臺run起來了
新建另一終端執行客戶端程序 ./addclient http://localhost:8888 99 22
在這裏插入圖片描述
成功計算了加減法,服務端也有所響應。
案例結束
引自:gsoap_2.8.33.zip安裝與編譯 作者:拿破崙的海闊天空

別人的博客2

大佬博客鏈接:ONVIF協議網絡攝像機(IPC)客戶端程序開發(4):使用gSOAP生成Web Services框架代碼
我新建了個code,大佬是在Windows下的,所以那2個exe我沒拷貝,其他同理拷貝,建立文件夾
在這裏插入圖片描述
然後我們終端執行

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

在這裏插入圖片描述
有警告,暫時忽略。生成了 mobilecode.h
在這裏插入圖片描述
其中-c爲產生純c代碼,默認生成 c++代碼;-s爲不使用STL庫,-t爲typemap.dat的標識。詳情可通過wsdl2h.exe -help查看幫助。
這裏的WSDL文件,可以在wsdl2h命令中在線下載,也可以先下載到本地,然後引用本地WSDL文件,這裏是採用在線下載方式。 —— 引自 https://blog.csdn.net/benkaoya/article/details/72452968

終端執行 /usr/local/gSOAP/bin/soapcpp2 -C -c -x -Iimport -Icustom mobilecode.h
在這裏插入圖片描述
custom、import、wsdl2h.exe、soapcpp2.exe、typemap.dat、mobilecode.h、soapClientLib.c 無用,可以刪除。刪除後
在這裏插入圖片描述
新建 main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}

大佬突然就運行了,我們Linux的話 編譯 gcc main.c stdsoap2.c soapC.c soapClient.c
然後 ./a.out 手機號碼
隨便百度個手機號碼看看
在這裏插入圖片描述
我們終端執行 ./a.out 18937777777
在這裏插入圖片描述
運行結果出來了,大佬的博客也結束了,大佬的ONVIF欄目 傳送門 大家可自行查閱。


恩 我們的文章到此結束了,真是漫長的旅程。各位有緣再見。O(∩_∩)O~~

相關鏈接

gsoap下載:https://sourceforge.net/projects/gsoap2/
gsoap主頁:http://www.cs.fsu.edu/~engelen/soap.html
gsoap官網: http://gsoap2.sourceforge.net/
官方教程:https://www.genivia.com/tutorials.html
gSOAP資料下載-太平洋下載中心(自行斟酌):https://dl.pconline.com.cn/download/1448138.html
gsoap_2.8.33.zip安裝與編譯:https://blog.csdn.net/hktkfly6/article/details/78321908
SOAP菜鳥教程:https://www.runoob.com/soap/soap-tutorial.html
WSDL 教程:https://www.w3school.com.cn/wsdl/index.asp
ONVIF協議解讀:https://blog.csdn.net/hktkfly6/article/details/78322154
ONVIF協議學習筆記:https://www.cnblogs.com/lsdb/p/9157302.html
ONVIF大佬博客:https://blog.csdn.net/benkaoya/category_6924052.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章