不知道是WSDL描述還是GSOAP自身的問題,有其生成的代碼中在不支持Header的情況下,一樣不支持對FAULT處理,于是,照著處理Header的過程,添加了對FAULT的處理。
???? 為處理FAULT,需要事先知道SOAP報文中將FAULT信息放在哪里,當然了,一般自定義擴充都放在DETAIL里面。只是開始不太確定,于是還特意發了個錯誤報文給對端,收到如下回復,從而確認猜測是對的,這樣就可以修改了。
<?xml?version='1.0'?encoding='utf-8'?><soapenv:Envelope?xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header?/><soapenv:Body><soapenv:Fault><faultcode>soapenv:Client</faultcode><faultstring>Authentication?Failed?AccessCfgHelper.getProductInfor(productId)?is?empty?or?is?null..</faultstring><detail><ns2:ServiceException?xmlns:ns2="http://www.chinatelecom.com.cn/schema/ctcc/common/v2_1"><messageId>SVC0901</messageId><text>CP/SP?authenticate?access?failed:Authentication?Failed?AccessCfgHelper.getProductInfor(productId)?is?empty?or?is?null..</text></ns2:ServiceException></detail></soapenv:Fault></soapenv:Body></soapenv:Envelope>
??? 首先在Detail結構里面添加所需要的Fault類型:
#ifndef?SOAP_TYPE_SOAP_ENV__Detail
#define?SOAP_TYPE_SOAP_ENV__Detail?(90)
/*?SOAP-ENV:Detail?*/
struct?SOAP_ENV__Detail
{
public:
????//int?__type;????/*?any?type?of?element?<fault>?(defined?below)?*/
????//void?*fault;????/*?transient?*/
????//char?*__any;
????sms2__ServiceException?*serviceException;
????sms2__PolicyException?*policyException;
};
#endif
??? 然后就是一樣修改Detail的in和out函數了:
SOAP_FMAC3?int?SOAP_FMAC4?soap_out_SOAP_ENV__Detail(struct?soap?*soap,?const?char?*tag,?int?id,?const?struct?SOAP_ENV__Detail?*a,?const?char?*type)
{
????if?(soap_element_begin_out(soap,?tag,?soap_embedded_id(soap,?id,?a,?SOAP_TYPE_SOAP_ENV__Detail),?type))
????????return?soap->error;
????//if?(soap_putelement(soap,?a->fault,?"fault",?-1,?a->__type))
????//????return?soap->error;
????//soap_outliteral(soap,?"-any",?&a->__any,?NULL);
????if(a?!=?NULL)
????{
????????if(a->serviceException?!=?NULL)
????????????a->serviceException->soap_out(soap,?"sms2:ServiceException",?-1,?"");
????????else?if(a->policyException?!=?NULL)
????????????a->policyException->soap_out(soap,?"sms2:PolicyException",?-1,?"");
????}
????return?soap_element_end_out(soap,?tag);
}
SOAP_FMAC3?struct?SOAP_ENV__Detail?*?SOAP_FMAC4?soap_in_SOAP_ENV__Detail(struct?soap?*soap,?const?char?*tag,?struct?SOAP_ENV__Detail?*a,?const?char?*type)
{
????//size_t?soap_flag_fault?=?1;
????//size_t?soap_flag___any?=?1;
????if?(soap_element_begin_in(soap,?tag,?0,?type))
????????return?NULL;
????a?=?(struct?SOAP_ENV__Detail?*)soap_id_enter(soap,?soap->id,?a,?SOAP_TYPE_SOAP_ENV__Detail,?sizeof(struct?SOAP_ENV__Detail),?0,?NULL,?NULL,?NULL);
????if?(!a)
????????return?NULL;
????soap_default_SOAP_ENV__Detail(soap,?a);
????int?service?=?1;
????int?policy?=?1;
????if?(soap->body?&&?!*soap->href)
????{
????????for?(;;)
????????{????soap->error?=?SOAP_TAG_MISMATCH;
????????????//if?(soap_flag_fault?&&?soap->error?==?SOAP_TAG_MISMATCH)
????????????//????if?((a->fault?=?soap_getelement(soap,?&a->__type)))
????????????//????{????soap_flag_fault?=?0;
????????????//????????continue;
????????????//????}
????????????//if?(soap_flag___any?&&?(soap->error?==?SOAP_TAG_MISMATCH?||?soap->error?==?SOAP_NO_TAG))
????????????//????if?(soap_inliteral(soap,?"-any",?&a->__any))
????????????//????{????soap_flag___any--;
????????????//????????continue;
????????????//????}
????????????
????????????if?(soap->error?==?SOAP_TAG_MISMATCH)
????????????{
????????????????if(service?==?1)
????????????????{
????????????????????a->serviceException?=?soap_in_sms2__ServiceException(soap,?"sms2:ServiceException",?a->serviceException,?"");
????????????????????if(a->serviceException)
????????????????????{
????????????????????????service?=?0;
????????????????????????continue;
????????????????????}
????????????????}
????????????????else?if(policy?==?1)
????????????????{
????????????????????a->policyException?=?soap_in_sms2__PolicyException(soap,?"sms2:PolicyException",?a->policyException,?"");
????????????????????if(a->policyException)
????????????????????{
????????????????????????policy?=?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__Detail?*)soap_id_forward(soap,?soap->href,?(void*)a,?0,?SOAP_TYPE_SOAP_ENV__Detail,?0,?sizeof(struct?SOAP_ENV__Detail),?0,?NULL);
????????if?(soap->body?&&?soap_element_end_in(soap,?tag))
????????????return?NULL;
????}
????return?a;
}
??? 由此,SOAP接口的主要問題除了“附件”還沒有處理外,就都解決了,其他都是業務應用的問題了,處理起來就有底了~