webservice gsoap 小记
參考?http://www.cs.fsu.edu/~engelen/soap.html
1. web service client application
> wsdl2h -s -o MyHead.h http://www.genivia.com/calc.wsdl
-s 不用 STL
> soapcpp2 -i -C MyHead.h
Option?-i?(and alternatively option?-j) indicates that we want C++ proxy and server objects that include the client (and server) code,?-C?indicates client-side only?
soapcpp2?generates both client and server stubs and skeletons by default
這兩步過程中可能會出錯,要把必要的文件加上: soap12.h ?stdsoap2.h ?stdsoap2.cpp
然后,We use the generated?soapcalcProxy?class and?calc.nsmap?XML namespace mapping table to access the Web service. The?soapcalcProxy?class is a proxy to invoke the service:
1 #include "soapWebService1SoapProxy.h" // 自動生成的頭文件 注:不用MyHead.h 2 #include "WebService1Soap.nsmap" 3 4 int main() 5 { 6 WebService1SoapProxy service; 7 _tempuri__GetSum a; 8 _tempuri__GetSumResponse result; 9 //a.soap = soap_new(); 10 a.a = 1; 11 a.b = 5; 12 //result.soap = soap_new(); 13 14 if (service.GetSum(&a, &result) == SOAP_OK) 15 std::cout << "the num of 1 and 5 is " << result.GetSumResult << std::endl; 16 else 17 service.soap_stream_fault(std::cerr); 18 service.destroy(); 19 20 getchar(); 21 return 0; 22 }編譯
如果遇到?stdsoap2.obj : error LNK2001: 無法解析的外部符號_namespaces
參考?http://blog.163.com/lyz_sea/blog/static/115586707201182432412677
在 stdsoap2.h,添加?
#ifndef WITH_NONAMESPACES
#define WITH_NONAMESPACES
#endif
?
2. Develop a Web Service (Stand-Alone Server)
1 // File: currentTime.h 2 //gsoap ns service name: currentTime 3 //gsoap ns service namespace: urn:currentTime 4 //gsoap ns service location: http://www.yourdomain.com/currentTime.cgi 5 int ns__currentTime(time_t& response);
> soapcpp2 -i -S currentTime.h
// File: currentTime.cpp #include "soapcurrentTimeService.h" // include the proxy declarations #include "currentTime.nsmap" // include the XML namespace mappings int main() {// create server and serve on CGI-based request: currentTimeService server;/*server.serve();server.destroy();*/while (server.run(65533) != SOAP_TCP_ERROR){server.soap_stream_fault(std::cerr);}return 0; }int currentTimeService::currentTime(time_t& response) {response = time(0);return SOAP_OK; }
Compile with ?currentTime.cpp ?soapC.cpp ?soapcurrentTimeService.cpp ?stdsoap2.cpp
run
然后可以由 wsdl 文件生成客戶端頭文件,and ...
?
3. C# 調用 gsoap 的 service
參考?http://blog.sina.com.cn/s/blog_4e7d38260100ade4.html
用 VS 自帶的 wsdl.exe (在 C 盤的某個角落)
> wsdl.exe currentTime.wsdl
生成 currentTime.cs
將 currentTime.cs 放到 C# 的工程中,研究下代碼結構,找到 currentTime 的初始化函數:
1 public picture() { 2 this.Url = "http://localhost:65432/picture.cgi"; // 改成你的地址 3 }OK,可以用了。
?
4. 如果要發送 二進制數據,可先將其進行編碼(暫時采用?base64 編碼,實際場景中應采用更合適的編碼算法),以 string 形式發送。接收端再進行相應解碼。
C++ gsoap service 端,讀取圖片:
1 int pictureService::picture(struct picdata &picture) 2 { 3 char *pic = NULL; 4 unsigned int length = 0; 5 6 std::ifstream is("chicken.jpg", std::ios::binary); 7 if (is) 8 { 9 is.seekg(0, is.beg); 10 is.seekg(0, is.end); 11 length = is.tellg(); 12 is.seekg(0, is.beg); 13 14 pic = new char[length]; 15 16 if (pic == NULL) 17 return SOAP_ERR; 18 19 is.read(pic, length); 20 is.close(); 21 } 22 23 picture.data = base64_encode((unsigned char*)pic, length); 24 25 if (pic != NULL) 26 { 27 delete pic; 28 pic = NULL; 29 } 30 31 return SOAP_OK; 32 }
?
C# 端,調用 webservice,獲取一張圖片,并保存:
1 picture pic = new picture(); 2 picture1 pic1 = new picture1(); 3 pictureResponse res = pic.Callpicture(pic1); 4 5 string strData = res.picture.data; 6 byte[] bytes = Convert.FromBase64String(strData); 7 8 FileStream fs = new FileStream("D:\\picture.jpg", FileMode.OpenOrCreate); 9 fs.Write(bytes, 0, bytes.Length); 10 fs.Close();?
? 以上代碼,有待優化。
?
轉載于:https://www.cnblogs.com/hangj/p/3620406.html
總結
以上是生活随笔為你收集整理的webservice gsoap 小记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: fzu 2154 YesOrNo
- 下一篇: Python xlrd 读取xls文件