GSoap: How to add info to SOAP Header using gSOAP

Source: Internet
Author: User
There's some misleading info in gSOAP's official documents in SOAP Header Processing Part.

This article leads you to the right way and can make your program work. The use case is:

Client needs to pass user name and password to Server Side to get authenticated.

The username and password info shocould be embeded in SOAP Header. Steps:

1. Edit struct SOAP_ENV _ Header in soapStub. h file which is generated by gSOAP's soapcpp2 compiler

Add the neccesary info to this struct

For example: The original one is:
Struct SOAP_ENV _ Header

{

Public:

Void * dummy;/* transient */

};
This shoshould be changed:
Struct SOAP_ENV _ Header

{

Public:

Void * dummy;/* transient */

Char * username;

Char * password;

}; 2. Edit function soap_out_SOAP_ENV _ Header in soapC. cpp file which is also generated by gSOAP

Add statements to serialize those info into SOAP Header.

For example: The original one is:
SOAP_FMAC3 int SOAP_FMAC4 soap_out_SOAP_ENV _ Header (struct soap * soap, const char * tag, int id, construst SOAP_ENV _ Header * a, const char * type)

{

Soap_element_begin_out (soap, tag, soap_embedded_id (soap, id, a, SOAP_TYPE_SOAP_ENV _ Header), type );

/* Transient dummy skipped */

Soap_element_end_out (soap, tag );

Return SOAP_ OK;

} This coshould be changed to: SOAP_FMAC3 int SOAP_FMAC4 soap_out_SOAP_ENV _ Header (struct soap * soap, const char * tag, int id, const struct SOAP_ENV _ Header *, const char * type)

{

Soap_element_begin_out (soap, tag, soap_embedded_id (soap, id, a, SOAP_TYPE_SOAP_ENV _ Header), type );

/* Transient dummy skipped */

Soap_out_string (soap, "headerNS: username", 1, & (a-> username ),"");

Soap_out_string (soap, "headerNS: password", 2, & (a-> password), ""); soap_element_end_out (soap, tag );

Return SOAP_ OK;

} 3. Add the namespace mapping to namespaces array in. nsmap file.

{"HeaderNS", "http://customeheader.test.com ",
NULL, NULL}, 4. Set the header before invoking Web Service Method. This part you can also refer to the gSOAP's official implements enthttp: // www.cs.fsu.edu /~ Engelen/soapdoc2.html # tth_sEc12. struct soap;

Soap_init (& soap );

...

Soap-> header = (SOAP_ENV _ Header *) soap_malloc (soap, sizeof (SOAP_ENV _ Header ));

Soap-> header-> username = (char *) malloc (MAX_NAME_SIZE * sizeof (char ));

Soap-> header-> password = (char *) malloc (MAX_NAME_SIZE * sizeof (char ));

Strcpy (soap-> header-> username, username );

Strcpy (soap-> header-> password, passwd );

Soap_call_method (& soap,...); // the SOAP Header will be in the request

... 5. Compile

6. Run.

The SOAP Message cocould be
...

<SOAP-ENV: Envelope xmlns: headerNS = "http://customeheader.vpamws.com">

<SOAP-ENV: Header>

<HeaderNS: username> admin
<HeaderNS: password> default
</SOAP-ENV: Header>

SOAP-ENV: Body>

...

SOAP-ENV: Body>

SOAP-ENV: Envelope>

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.