Gsoap: how to add info to SOAP header using gsoap)
Source: Internet
Author: User
Gsoap: how to add info to SOAP header using gsoapthere's some misleading info in gsoap's official documents ENTs 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 cocould be changed: 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_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 restart 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> Any questions, please let me know. Thanks. -Debora
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.