sample_add_client_complex.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*=============================================================================
  2. sample_add_client_complex.cpp
  3. ===============================================================================
  4. This is an example of an XML-RPC client that uses XML-RPC for C/C++
  5. (Xmlrpc-c).
  6. In particular, it uses the complex lower-level interface that gives you
  7. lots of flexibility but requires lots of code. Also see
  8. xmlrpc_sample_add_server, which does the same thing as this program,
  9. but with much simpler code because it uses a simpler facility of
  10. Xmlrpc-c.
  11. This program actually gains nothing from using the more difficult
  12. facility. It is for demonstration purposes.
  13. =============================================================================*/
  14. #include <cassert>
  15. #include <cstdlib>
  16. #include <string>
  17. #include <iostream>
  18. #include <xmlrpc-c/girerr.hpp>
  19. #include <xmlrpc-c/base.hpp>
  20. #include <xmlrpc-c/client.hpp>
  21. using namespace std;
  22. int
  23. main(int argc, char **) {
  24. if (argc-1 > 0) {
  25. cerr << "This program has no arguments" << endl;
  26. exit(1);
  27. }
  28. try {
  29. xmlrpc_c::clientXmlTransport_curl myTransport(
  30. xmlrpc_c::clientXmlTransport_curl::constrOpt()
  31. .no_ssl_verifyhost(true)
  32. .user_agent("sample_add/1.0"));
  33. xmlrpc_c::client_xml myClient(&myTransport);
  34. string const methodName("sample.add");
  35. xmlrpc_c::paramList sampleAddParms;
  36. sampleAddParms.add(xmlrpc_c::value_int(5));
  37. sampleAddParms.add(xmlrpc_c::value_int(7));
  38. xmlrpc_c::rpcPtr myRpcP(methodName, sampleAddParms);
  39. string const serverUrl("http://localhost:8080/RPC2");
  40. xmlrpc_c::carriageParm_curl0 myCarriageParm(serverUrl);
  41. myRpcP->call(&myClient, &myCarriageParm);
  42. assert(myRpcP->isFinished());
  43. int const sum(xmlrpc_c::value_int(myRpcP->getResult()));
  44. // Assume the method returned an integer; throws error if not
  45. cout << "Result of RPC (sum of 5 and 7): " << sum << endl;
  46. } catch (exception const& e) {
  47. cerr << "Client threw error: " << e.what() << endl;
  48. } catch (...) {
  49. cerr << "Client threw unexpected error." << endl;
  50. }
  51. return 0;
  52. }