2012年10月14日 星期日

gSoap -- 小技巧分享

紀錄使用 gSoap 開發 ONVIF 時所用的一些小技巧。

1. 取出 Client 所送的 HTTP data。
若需要存取HTTP的原始資料,可以直接使用 zsoap->buf, zsoap->buflen 這兩個資料結構。



2. 將輸出由 stdout 導向某一個 buffer
作法是針對 fsend 註冊一個新的callback 函數,在此函數內將輸出導向buffer。例子如下:
int mysend(struct soap *soap, const char *s, size_t n) { if (len1 + n > sizeof(buf)) return SOAP_EOF; strcpy(buf + len1, s); len1 += n; return SOAP_OK; } main() { char buf[10000]; // XML buffer int len1 = 0; // #chars written int len2 = 0; // #chars read // mysend: put XML in buf[] struct soap *soap=malloc(sizeof(struct soap)); soap_init(soap); soap->fsend = mysend; struct __tds__GetServices soap_tmp___tds__GetServices; struct _tds__GetServicesResponse tds__GetServicesResponse; soap_default__tds__GetServicesResponse(soap, &tds__GetServicesResponse); soap_default___tds__GetServices(soap, &soap_tmp___tds__GetServices); soap->encodingStyle = NULL; soap_tmp___tds__GetServices.tds__GetServices = (struct _tds__GetServices *)TV_MALLOC(sizeof(struct _tds__GetServices)); soap_tmp___tds__GetServices.tds__GetServices->IncludeCapability = 1; soap->error = __tds__GetServices(soap, soap_tmp___tds__GetServices.tds__GetServices, &tds__GetServicesResponse); if (soap->error) return soap->error; soap_serialize__tds__GetServicesResponse(soap, &tds__GetServicesResponse); soap_put__tds__GetServicesResponse(soap, &tds__GetServicesResponse, "tds:GetServicesResponse", NULL); fprintf(stderr, "\n GetServicesResponse buf=\n%s\n", buf); }

3. MTOM 的使用方法
請參考這裡

4. 使用 "/>" 代替 closing tag 的方法
一般直接使用 gSoap 會產生如下xml
<myTag v="value"></myTag> 
若試圖產生下列xml,則需要在 gsoap init 時加上 SOAP_XML_NIL 的 flag
<myTag v="value"/>