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