日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

webservice gsoap 小记

發(fā)布時(shí)間:2024/9/20 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 webservice gsoap 小记 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

參考?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

  

  這兩步過(guò)程中可能會(huì)出錯(cuò),要把必要的文件加上: 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" // 自動(dòng)生成的頭文件 注:不用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: 無(wú)法解析的外部符號(hào)_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# 調(diào)用 gsoap 的 service

  參考?http://blog.sina.com.cn/s/blog_4e7d38260100ade4.html

  用 VS 自帶的 wsdl.exe (在 C 盤的某個(gè)角落)

  > wsdl.exe currentTime.wsdl

  生成 currentTime.cs

  將 currentTime.cs 放到 C# 的工程中,研究下代碼結(jié)構(gòu),找到 currentTime 的初始化函數(shù):

1 public picture() { 2 this.Url = "http://localhost:65432/picture.cgi"; // 改成你的地址 3 }

  OK,可以用了。

?

4. 如果要發(fā)送 二進(jìn)制數(shù)據(jù),可先將其進(jìn)行編碼(暫時(shí)采用?base64 編碼,實(shí)際場(chǎng)景中應(yīng)采用更合適的編碼算法),以 string 形式發(fā)送。接收端再進(jìn)行相應(yīng)解碼。

  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# 端,調(diào)用 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();

?

?  以上代碼,有待優(yōu)化。

?

轉(zhuǎn)載于:https://www.cnblogs.com/hangj/p/3620406.html

總結(jié)

以上是生活随笔為你收集整理的webservice gsoap 小记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。