GSOAP对Header的支持远没有其站点说的那么好,当然如果WSDL描述的严密,其工具会自动生成相应的Header处理代码,但如果WSDL写的不好,需要人为添加对Header的处理,那写起来就是灾难~
经过四处查找资料,随意踩着前人的肩膀,终于形成了如下的代码,供大家来踩~
对SOAPHeader定义的改正,添加所需数据:
#ifndef SOAP_TYPE_SOAP_ENV__Header
#define SOAP_TYPE_SOAP_ENV__Header (86)
/* SOAP Header: */
struct SOAP_ENV__Header
{
#ifdef WITH_NOEMPTYSTRUCT
private:
char dummy; /* dummy member to enable compilation */
#endif
public:
mms2__RequestSOAPHeader* requestHeader;
mms2__NotifySOAPHeader* notifyHeader;
};
#endif
添加SOAPHeader的in函数处理:
SOAP_FMAC3 struct SOAP_ENV__Header * SOAP_FMAC4 soap_in_SOAP_ENV__Header(struct soap *soap, const char *tag, struct SOAP_ENV__Header *a, const char *type)
{
if (soap_element_begin_in(soap, tag, 0, type))
return NULL;
a = (struct SOAP_ENV__Header *)soap_id_enter(soap, soap->id, a, SOAP_TYPE_SOAP_ENV__Header, sizeof(struct SOAP_ENV__Header), 0, NULL, NULL, NULL);
if (!a)
return NULL;
soap_default_SOAP_ENV__Header(soap, a);
int notifyheader = 1;
int requestheader = 1;
if (soap->body && !*soap->href)
{
for (;;)
{ soap->error = SOAP_TAG_MISMATCH;
if (soap->error == SOAP_TAG_MISMATCH)
{
if(notifyheader == 1)
{
a->notifyHeader = soap_in_mms2__NotifySOAPHeader(soap, "mms2:NotifySOAPHeader", a->notifyHeader, "");
if(a->notifyHeader)
{
notifyheader = 0;
continue;
}
}
else if(requestheader == 1)
{
a->requestHeader = soap_in_mms2__RequestSOAPHeader(soap, "mms2:RequestSOAPHeader", a->requestHeader, "");
if(a->requestHeader)
{
requestheader = 0;
continue;
}
}
soap->error = soap_ignore_element(soap);
}
if (soap->error == SOAP_NO_TAG)
break;
if (soap->error)
return NULL;
}
if (soap_element_end_in(soap, tag))
return NULL;
}
else
{ a = (struct SOAP_ENV__Header *)soap_id_forward(soap, soap->href, (void*)a, 0, SOAP_TYPE_SOAP_ENV__Header, 0, sizeof(struct SOAP_ENV__Header), 0, NULL);
if (soap->body && soap_element_end_in(soap, tag))
return NULL;
}
return a;
}
添加SOAPHeader的out函数处理:
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)
{
if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, a, SOAP_TYPE_SOAP_ENV__Header), type))
return soap->error;
if(a != NULL)
{
if(a->requestHeader != NULL)
a->requestHeader->soap_out(soap, "mms2:RequestSOAPHeader", -1, "");
else if(a->notifyHeader != NULL)
a->notifyHeader->soap_out(soap, "mms2:NotifySOAPHeader", -1, "");
}
return soap_element_end_out(soap, tag);
}
并不确认上面的代码是否还有什么错误,还没有来得及仔细测试,就来了一次request调用,所需数据正常到达,就已经19:50了,只能下班回家了~现在GSOAP还有两个主要问题:MIME附件和SOAPFault的处理。
熟悉GSOAP和使用的WSDL的应该很好理解上面的代码,不熟悉这个WSDL的,麻烦多猜猜了,这个WSDL虽然不是什么绝密,但BOSS跟人签了“保密”协议,这里就不乱说了,省的留下把柄~