gsoap开发webservice 服务端.md

无WSDL文件

1.编写头文件websever2.h:

 1 //gsoap ns service name: calc
 2 //gsoap ns service style: rpc
 3 //gsoap ns service encoding: encoded
 4 //gsoap ns service namespace: http://127.0.0.1:8089/calc.wsdl
 5 //gsoap ns service location: http://127.0.0.1:8089/cal
 6 //gsoap ns schema  namespace:    urn:calc
 7 int ns__add(double a, double b, double *result);
 8 int ns__sub(double a, double b, double *result);
 9 int ns__mul(double a, double b, double *result);
10 int ns__div(double a, double b, double *result);
11 int ns__pow(double a, double b, double *result);

2.生成文件:

1 C:\Users\admin>e:
2 E:\>cd E:\4.0projet\webserver2\gsoap_2.9.65\gsoap-2.8\gsoap\bin\win32
3 E:\4.0projet\webserver2\gsoap_2.9.65\gsoap-2.8\gsoap\bin\win32>soapcpp2.exe -i -2 webserver2.h

3.将以下文件拷如项目:

前6个路径:
E:\4.0project\webserver2\gsoap_2.8.65\gsoap-2.8\gsoap\bin\win32

cal.nsmap
soapC.cpp
soapcalService.cpp(名称不固定,以service结尾)
soapcalcService.h(名称不固定,以service结尾)
soapH.h
soapStub.h

以下文件在路径:
E:\4.0project\webserver2\gsoap_2.8.65\gsoap-2.8\gsoap

stdsoap2.h
stdsoap2.cpp

分别添加到Header Files、Resource Files、Source Files内

4.编写gSOAPService.cpp:

  1 #define _CRT_SECURE_NO_WARNINGS   **//一定要添加上**
  2 #include "calc.nsmap" 
  3 #include"soapcalcService.h"
  4 #include "iostream"  //控件问提只能写“”
  5 
  6 using namespace std;
  7 
  8 //很重要
  9 int   http_get(struct   soap   *soap)
 10 {
 11     FILE*fd = NULL;
 12     fd = fopen("E:\\4.0project\\webserver2\\gsoap_2.8.65\\gsoap-2.8\\gsoap\\bin\\win32\\calc.wsdl", "rb"); //open WSDL file to copy
 13 
 14     if (!fd)
 15     {
 16         return 404; //return HTTP not found error
 17     }
 18     soap->http_content = "text/xml";  //HTTP header with text /xml content
 19     soap_response(soap, SOAP_FILE);
 20     for (;;)
 21     {
 22         size_t r = fread(soap->tmpbuf, 1, sizeof(soap->tmpbuf), fd);
 23         if (!r)
 24         {
 25             break;
 26         }
 27         if (soap_send_raw(soap, soap->tmpbuf, r))
 28         {
 29             break; //cannot send, but little we can do about that
 30         }
 31     }
 32     fclose(fd);
 33     soap_end_send(soap);
 34     return SOAP_OK;
 35 }
 36 int main(int argc, char *argv[])
 37 {
 38     calcService cal;
 39     cal.fget = http_get;
 40     while (1)
 41     {
 42         if (cal.run(8089))
 43         {
 44             cal.soap_stream_fault(std::cerr);
 45         }
 46     }
 47     return 0;
 48 }
 49 
 50 //自动生成了calcService类,自己重写add等函数
 51 /*加法的具体实现*/
 52 int calcService::add(double num1, double num2, double* result)
 53 {
 54     if (NULL == result)
 55     {
 56         printf("Error:The third argument should not be NULL!\n");
 57         return SOAP_ERR;
 58     }
 59     else
 60     {
 61         (*result) = num1 + num2;
 62         return SOAP_OK;
 63     }
 64     return SOAP_OK;
 65 }
 66 
 67 /*减法的具体实现*/
 68 int calcService::sub(double num1, double num2, double* result)
 69 {
 70     if (NULL == result)
 71     {
 72         printf("Error:The third argument should not be NULL!\n");
 73         return SOAP_ERR;
 74     }
 75     else
 76     {
 77         (*result) = num1 - num2;
 78         return SOAP_OK;
 79     }
 80     return SOAP_OK;
 81 }
 82 
 83 /*乘法的具体实现*/
 84 int calcService::mul(double num1, double num2, double* result)
 85 {
 86     if (NULL == result)
 87     {
 88         printf("Error:The third argument should not be NULL!\n");
 89         return SOAP_ERR;
 90     }
 91     else
 92     {
 93         (*result) = num1 * num2;
 94         return SOAP_OK;
 95     }
 96     return SOAP_OK;
 97 }
 98 
 99 /*除法的具体实现*/
100 int calcService::div(double num1, double num2, double* result)
101 {
102     if (NULL == result || 0 == num2)
103     {
104         return soap_senderfault("Square root of negative value", "I can only compute the square root of a non-negative value");
105         return SOAP_ERR;
106     }
107     else
108     {
109         (*result) = num1 / num2;
110         return SOAP_OK;
111     }
112     return SOAP_OK;
113 }
114 
115 int calcService::pow(double num1, double num2, double* result)
116 {
117     if (NULL == result || 0 == num2)
118     {
119         printf("Error:The second argument is 0 or The third argument is NULL!\n");
120         return SOAP_ERR;
121     }
122     else
123     {
124         (*result) = num1 / num2;
125         return SOAP_OK;
126     }
127     return SOAP_OK;
128 }

测试:浏览器输入: http://127.0.0.1:8089/calc.wsdl
可以查看wsdl文件 soapUI,新建soap工程,选择对应的wsdl,可以测试使用

已有wsdl文件

部分情况下,客户端已开发完成,需要根据已有wsdl生成.h文件,再通过.h文件生成服务端代码。 根据已有wsdl文件,生成.h文件

D:\JHC00144512\gsoap_2.8.65\gsoap-2.8\gsoap\bin\win32>wsdl2h.exe calc.wsdl

修改wsdl地址时,主要修改wsdl文件内最后一部分中的:

<soap:address location="http://127.0.0.1:8089/macWS"/> 内location信息。


 

上一篇:linux-gsoap链接错误


下一篇:c – gSOAP多线程