No WSDL file
1. Write a header file 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. Generate file:
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. Copy the following files such as project:
前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 ** // Be sure to add the ** 2 #include " calc.nsmap " 3 #include " soapcalcService.h " 4 #include " iostream " // Control asked to mention only write "" 5 6 a using namespace STD; . 7 . 8 // important . 9 int the http_get ( struct SOAP * SOAP) 10 { . 11 the FILE * FD = NULL; 12 is FD = the 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 * the argv []) 37 [ { 38 is calcService CAL; 39 cal.fget = the http_get; 40 the while ( . 1 ) 41 is { 42 is IF (cal.run ( 8089 )) 43 is { 44 is cal.soap_stream_fault ( :: cerr, STD); 45 } 46 is } 47 return 0 ; 48 } 49 50 // automatically generated calcService classes, override their add other functions 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 returnSOAP_ERR; 90 } 91 is the else 92 { 93 (Result *) * = num1 num2; 94 return SOAP_OK; 95 } 96 return SOAP_OK; 97 } 98 99 / * embodied division * / 100 int calcService :: div ( Double num1, Double num2, Double * Result) 101 { 102 IF (Result == NULL || 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 }
Test: browser and enter: http://127.0.0.1:8089/calc.wsdl
can view the wsdl file soapUI, the new soap project, select the corresponding wsdl, can be tested using
Existing wsdl file
In some cases, the client has been developed, based on existing needs wsdl generated .h file, and then generate the server code via .h file. According to the existing wsdl file to generate .h file
D:\JHC00144512\gsoap_2.8.65\gsoap-2.8\gsoap\bin\win32>wsdl2h.exe calc.wsdl
When modify wsdl address, the last part of the main changes in the wsdl file:
<soap:address location="http://127.0.0.1:8089/macWS"/>
In the location information.