Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1524010
  • 博文数量: 226
  • 博客积分: 3997
  • 博客等级: 少校
  • 技术积分: 2369
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-19 17:26
个人简介

Never save something for a special occasion. Every day in your life is a special occasion.

文章分类

全部博文(226)

文章存档

2018年(5)

2017年(11)

2016年(1)

2015年(17)

2014年(14)

2013年(30)

2012年(5)

2011年(52)

2010年(107)

分类: 其他平台

2014-04-28 18:58:09


点击(此处)折叠或打开

  1. /*
  2. #filename: main.cpp

  3. prj: Qt编写WebService客户端
  4. date: 2014-4-28
  5. auth: pz

  6. 假设已经有一个ws服务端在运行,提供由用户id查询用户名的服务,下面直接使用其soap服务定义 .h 文件在qt中开发客户端。



  7. 1. 准备
  8. 1.1 create directory:
  9. soaptest/client/
  10. soaptest/gsoap/

  11. 1.2 DOWNLOAD gsoap_2.8.15.zip, extract following file/directory to soaptest/gsoap/:
  12. gsoap_2.8.15.zip\gsoap-2.8\gsoap\bin_win32\
  13. gsoap_2.8.15.zip\gsoap-2.8\{stdsoap2.h, stdsoap2.cpp}
  14. gsoap_2.8.15.zip\gsoap-2.8\import\

  15. 2. soapcpp2 编译
  16. 2.1 edit soap H file:
  17. soaptest\gsoap\test.h
  18. ====================================================
  19. //gsoap ns service name: cu
  20. //gsoap ns service namespace: http://localhost/cu.wsdl
  21. //gsoap ns service location: http://localhost/home/cu
  22. //gsoap ns service executable: cu.cgi
  23. //gsoap ns service encoding: encoded
  24. //gsoap ns schema namespace: urn:cu
  25. //gsoap ns schema form: unqualified


  26. struct ns__MyTestRequest
  27. {
  28.     char* id;
  29. };

  30. struct ns__MyTestResponse
  31. {
  32.     int Result;
  33.     char* name;
  34. };

  35. int ns__MyTest(struct ns__MyTestRequest *req, struct ns__MyTestResponse *rsp);
  36. ====================================================

  37. 2.2 soapcpp2
  38. E:\Qt\QtGuiEx\soaptest\gsoap> bin_win32\soapcpp2 -j -I"./import" test.h

  39. Note:
  40. -C generate webservice client
  41. -c pure c
  42. -j with proxy

  43. 3. 编译客户端
  44. 根据上一步 soapcpp2 是否带选项 -j,打开/关闭下面的宏定义,然后编译qt程序。
  45. //#define USING_PROXY -j

  46. 4. 运行结果
  47. 输入 id="pz"
  48. 输出 name = "pengzhen"

  49. */



  50. #include <QApplication>
  51. #include <QDebug>
  52. #include <QWidget>
  53. #include "../gsoap/cu.nsmap"

  54. //#define USING_PROXY -j
  55. const char *endpoint= "http://192.168.250.101:9000/cu";

  56. #ifdef USING_PROXY

  57. // with proxy
  58. #include "../gsoap/soapcuProxy.h"
  59. int main(int argc, char*argv[])
  60. {
  61.     cuProxy soapX;

  62.     struct ns__MyTestRequest req = {"pz"};
  63.     struct ns__MyTestResponse res;



  64.   soapX.MyTest(endpoint, NULL, &req, &res);
  65.   qDebug()<< "result:" << res.Result;
  66.   if(res.Result == 0)
  67.   {
  68.       qDebug() << "name of " << req.id << ":" << res.name;
  69.   }

  70. return 0;
  71. }

  72. #else

  73. // without proxy
  74. #include "../gsoap/soapH.h"

  75. int main(int argc, char*argv[])
  76. {
  77.     struct soap soapX;
  78.     soap_init(&soapX);
  79.     //soap_set_namespaces(&soapX, cu_namespaces);
  80.     soap_set_mode(&soapX, SOAP_C_UTFSTRING);

  81.     struct ns__MyTestRequest req = {"pz"};
  82.     struct ns__MyTestResponse res;

  83.   soap_call_ns__MyTest(&soapX, endpoint, NULL, &req, &res);
  84.   qDebug()<< "result:" << res.Result;
  85.   if(res.Result == 0)
  86.   {
  87.     qDebug() << "name of " << req.id << ":" << res.name;
  88.   }



  89. return 0;
  90. }
  91. #endif
qt工程文件:

点击(此处)折叠或打开

  1. # filename: cli.pro
  2. LIBS += -lwsock32

  3. SOURCES += \
  4.     main.cpp \
  5.     ..\gsoap\soapC.cpp \
  6.     ..\gsoap\soapClient.cpp \
  7.     ..\gsoap\stdsoap2.cpp

  8. # for proxy
  9. SOURCES += \
  10.     ..\gsoap\soapcuProxy.cpp



阅读(5905) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~