转gsoap使用总结
gsoap使用總結
>>用C實現WebService,gsoap是最好的選擇了.
>>快速開始
? 1. gsoap官網。遇到問題時,官網往往是最能提供幫助的地方。
?????http://gsoap2.sourceforge.net/
? 2. 幾個值得參考的鏈接。
???? GSoap使用心得:?http://www.cppblog.com/qiujian5628/archive/2008/10/11/54019.html
???? GSoap接口定義:?http://blog.sina.com.cn/s/blog_5ee9235c0100de3g.html
?????
>>測試代碼。
? 我是在linux下用C開發的,那就Makefile入手吧。至于服務端的代碼,和客戶端的代碼,可以參考《GSoap使用心得》,或者是gsoap自帶的例子。
???? a. 用soapcpp2生產gsoap相關代碼: $(GSOAP_BIN)/soapcpp2 -c -x ${WSNAME}.h
???? b. 服務端所需的代碼: soapC.c soapServer.c stdsoap2.c
???? c. 客戶端所需的代碼: soapC.c soapClient.c stdsoap2.c
???? d. 其中stdsoap2.c是從gsoap開發包中復制過來的,他的文件是(a)中命令產生的。
?????
-------------分隔符------------------------------------
GSOAP_BIN=/usr/local/gSOAP/bin
WSNAME0=soap
WSNAME=SmsWBS
SERVER_OBJS=$(WSNAME0)C.o $(WSNAME0)Server.o stdsoap2.o??? ${WSNAME}server.o
CLIENT_OBJS=$(WSNAME0)C.o $(WSNAME0)Client.o stdsoap2.o??? ${WSNAME}client.o
AA_OBJS=$(WSNAME0)C.o $(WSNAME0)Server.o $(WSNAME0)Client.o stdsoap2.o ${WSNAME}server.o ${WSNAME}client.o
INCLUDE=
LIBS=
CC=g++ -g -DWITH_NONAMESPACES
#LIBS=-lz -lc -lncurses -lssl -lcrypto
#CC=g++ -g -DWITH_NONAMESPACES -DWITH_OPENSSL
all:server
all:client
${WSNAME}.wsdl:${WSNAME}.h
??? $(GSOAP_BIN)/soapcpp2 -c -x ${WSNAME}.h
$(AA_OBJS):%.o:%.c
??? $(CC) -c $? $(INCLUDE)
server:Makefile ${WSNAME}.wsdl? $(SERVER_OBJS)
??? $(CC) $(SERVER_OBJS) $(LIBS) -o Smsserver -lpthread
client:Makefile ${WSNAME}.wsdl? $(CLIENT_OBJS)
??? $(CC) $(CLIENT_OBJS) $(LIBS) -o Smsclient
clean:
??? rm -f *.o *.xml *.a *.wsdl *.nsmap \
??? $(WSNAME0)H.h $(WSNAME0)C.c $(WSNAME0)Server.c $(WSNAME0)Client.c \
??? $(WSNAME0)Stub.* $(WSNAME)$(WSNAME)Proxy.* $(WSNAME)$(WSNAME)Object.* \
??? $(WSNAME0)ServerLib.c $(WSNAME0)ClientLib.c $(WSNAME)server ns.xsd $(WSNAME)test
clear:
??? rm -f *.o ns.xsd
-------------分隔符------------------------------------
>>接口定義,可參考《GSoap接口定義》。這里我將給出C#引用這個webserver所對應的接口形式。
? gsoap是根據我們定義好的.h文件,然后用工具產生了我們所需的.c文件。所以我們必須根據gsoap的要求編寫.h。
? 1. 單個參數的傳出:
???? int ns__add( int a, int b, int *c );
???? 需要說明的是,這里的ns__是必須的,必須以開始注釋中的ns加兩個下劃線開始。返回值必須是int。
???? 但是這里的int并不是接口的返回值,而是gsoap內部的返回值。真正的返回值是int *c。
?????
???? C#中對應的接口:? int add( int a, int b );? 返回值就是上述的int *c參數。
?????
? 2. 多個參數傳出,在接口中必須使用結構體
???? typedef char * xsd__string;
???? typedef long?? xsd__int;
???? struct ns__personResponse{
???????? xsd__int age;
???????? xsd__string name;
???????? xsd__string address;
???? };
???? int ns__person( xsd__string buf_in, struct ns__personResponse * buf_out );
?????
???? 在C#中,并不是我們所聲明的這樣。而是:int person( string buf_in, out string name, out string address );
???? 即,結構體中的第一個域會變成返回值,其他的變成一個個的輸出參數。
?????
? 3. 返回結構體。如果要返回結構圖,那么必須在結構體中再套一層結構體:
???? typedef char * xsd__string;
???? typedef long?? xsd__int;
???? struct ns__person{
???????? xsd__int age;
???????? xsd__string name;
???????? xsd__string address;?????
???? };
???? struct ns__personResponse{
???????? xsd__int ret;
???????? struct ns__person person;
???? };
???? int ns__person( xsd__string buf_in, struct ns__personResponse * buf_out );
?????
???? 那么在C#中,看到的接口是這樣的:int person( string buf_in, person對應的結構類 );
?????
? 4. 接口中的下劃線,如果接口中的交易名有下劃線,必須這么聲明:
???? int ns__echo_USCOREreverse( char * buf_in, char ** buf_out );
?????
???? 那么,C#中實際上的接口名就是:string echo_reverse( string buf_in );
???
>>gsoap中返回字符串
? 1. 下面是一個逆轉字符串的函數。
???? int ns__echo_USCOREreverse( char * buf_in, char ** buf_out );
???? int ns__echo_USCOREreverse( struct soap *add_soap, char *buf_in, char **buf_out )
???? {
??????? int i, j, len;
??????? printf( "ns__interface: in=[%s]\n", buf_in );
????
??????? len = strlen(buf_in);
??????? *buf_out = (char*)soap_malloc( add_soap, len+1 );
??????? for( i=len-1, j=0; i>=0; i--, j++ ){
??????????? (*buf_out)[j] = buf_in[i];
??????? }
??????? (*buf_out)[j] = 0;
??????? return 0;
???? }
?????
???? 其中調用soap_malloc申請空間,并且將他賦給返回參數buf_out。這個空間會在調用soap_end時被釋放。
?????
>>gsoap傳輸中文。我使用utf-8編碼格式來支持漢字的傳輸。
? 1. 設置gsoap為utf-8傳輸數據
???? soap_set_mode( &SmsWBS_soap, SOAP_C_UTFSTRING );??? //設置編碼
???? SmsWBS_soap.mode|=SOAP_C_UTFSTRING;
?????
? 2. 使用下面得函數轉換我們的傳輸內容,即將我們的數據轉成UTF-8編碼:
???? int conv_charset( const char *dest, const char *src, char *input, size_t ilen, char *output, size_t olen )
???? {
???????? int convlen = olen;
???????? iconv_t conv = iconv_open( dest, src );
???????? if( conv == (iconv_t) -1 )
???????????? return -1;
????
????????? memset( output, 0, olen );
???????? if( iconv( conv, &input, &ilen, &output, &olen ) ){
???????????? iconv_close(conv);
???????????? return -1;
???????? }
???????? iconv_close(conv);
???????? return convlen-olen;
???? }
???? 例子: conv_charset( "UTF-8", "GBK", "林學任.linxr", strlen("林學任.linxr"),? buf_out->name, 100 );
??
>>webserver發布
? 1. 在C#中,可以直接引用一個webserver,但是我們寫得webserver如何能用被其引用呢。其實只要實現gsoap的fget回調函數即可:
???? SmsWBS_soap.fget = http_get;
? 2. http_get函數實現
???? int http_get(struct soap * soap)
???? {?
???????? FILE *fd = NULL;
?????
???????? char *s = strchr( soap->path, '?' );
???????? if( !s || strcmp( s, "?wsdl" ) ){
???????????? return SOAP_GET_METHOD;
???????? }
?????
???????? fd = fopen( "SmsWBS.wsdl", "rb" );
???????? if (!fd){
???????????? return 404;
???????? }
?????
???????? soap->http_content = "text/xml";
???????? soap_response(soap, SOAP_FILE);
???????? for (;;){?
???????????? size_t r = fread(soap->tmpbuf, 1, sizeof(soap->tmpbuf), fd);
???????????? if( !r ){
???????????????? break;
???????????? }
???????????? if( soap_send_raw( soap, soap->tmpbuf, r) ){
???????????????? break;?
???????????? }
???????? }
?????
???????? fclose(fd);
???????? soap_end_send(soap);
?????
???????? return SOAP_OK;
???? }
?????
轉載于:https://www.cnblogs.com/gg-mm/p/4159686.html
總結
以上是生活随笔為你收集整理的转gsoap使用总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《BI那点儿事》三国人物智力分布状态分析
- 下一篇: iOS网络编程开发—HTTP协议