add info to SOAP Header using gSOAP

前段时间学习了WebService的客户端实现,今天碰到一个问题需要在soap消息中添加用户认证,在网上找到了一篇资料,与大家分享,转载自http://deborazhang.spaces.live.com/blog/cns!599DFA62740F949D!542.entry

 

感谢原作者镇长丁丁

gSoap: How to add info to SOAP Header using gSOAP
gSoap: How to add info to SOAP Header using gSOAP
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 should 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 should be changed to:
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, const struct 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 could 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 *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 document http://www.cs.fsu.edu/~engelen/soapdoc2.html#tth_sEc12.
    struct soap 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 could be
...
<SOAP-ENV:Envelope xmlns:headerNS="
http://customeheader.vpamws.com ">
<SOAP-ENV:Header>
<headerNS:username>admin</headerNS:username>
<headerNS:password>default</headerNS:password>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
...
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
 
Any Questions, Please let me know. Thanks.
 
-Debora
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值