Standard C/A + + program calls WebService via gSOAP

Source: Internet
Author: User

I. Environmental preparedness

In this article, all programs are developed under Linux and tested to work properly.

In the development process, we need to use gSOAP, can be downloaded from the following Web site: http://www.cs.fsu.edu/~engelen/soap.html

I downloaded the gsoap_2.7.12.tar.gz.

Download down to decompress, according to the normal installation process to compile, install.

# tar ZXVF gsozp_2.7.12.tar.gz

# CD gsoap_2.7.12

#./CONFIGURE–-PREFIX=/USR/LOCAL/GSOAP (Specify Installation path)

# make

# make Install

Second, the production of related documents

1, through the WSDL document, the production of C + + header files

# /usr/local/gSOAP/bin/wsdl2h –c –o TestHeader.h http://192.168.0.151:8080/services/TestWS?wsdl

2, copy gSOAP source code in the Import folder under the Stlvector.h file to TestHeader.h the same directory, if the WSDL document when parsing the use of the-s parameter, that is, do not use STL, you do not need to copy this file.

3, parse TestHeader.h file, generate stub program

# /usr/local/gSOAP/bin/soapcpp2 –c –C TestHeader.h

The parameter-C in the command generates a standard C program and, without this parameter, generates a C + + program.

The parameter-C in the command generates only the client program, and without this parameter, the client and server-side programs are generated by default.

Third, carry out related development

1. New Project

Create a new C project and copy the file you just generated to the project, set the engineering properties, include the gSOAP source code directory, link properties, need to include the file Libgsoap.a (developed using C) or LIBGSOAP++.A (developed using C + +).

2. Code Development

WebService can refer to the generated soapStub.h file as a specific invocation.

Demo code (call WebService's AddUser):

#include "soapH.h"
#include "TestHeaderHttpBinding.nsmap"
int main()
{
    struct soap clientSOAP;
    struct _ns1__addUser addUserMsg;
    struct _ns1__addUserResponse addUserResponse;
    soap_init(&clientSOAP);
    addUserMsg.in0 = "test";
    addUserMsg.in1 = "test";
    addUserMsg.in2 = "test";
    if(soap_call___ns1__addUser(&clientSOAP, NULL, NULL, &addUserMsg, &addUserResponse) == SOAP_OK)
    {
        printf("%s\n", *addUserResponse.out);
    }
    else
    {
        printf("Error\n");
    }
    
    soap_destroy(&clientSOAP);
    soap_end(&clientSOAP);
    soap_done(&clientSOAP);
    return 0;
}

Normally, you can see the processing results returned by the WebService at the console after the build runs.

In this code, _ns1__adduser, _ns1__adduserresponse, Soap_call___ns1__adduser are all available in soapStub.h, and specific applications may generate different code depending on the server.

This article from the "Xuanwu" blog, please be sure to retain this source http://commandos.blog.51cto.com/154976/130652

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.