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 22 using namespace std; 23 24 #include <xmlrpc-c/girerr.hpp> 25 #include <xmlrpc-c/base.hpp> 26 #include <xmlrpc-c/client.hpp> 27 28 int main(int argc,char **)29main(int argc, char **) { 30 31 if (argc-1 > 0) { 32 cerr << "This program has no arguments" << endl; 33 exit(1); 34 } 35 36 try { 37 xmlrpc_c::clientXmlTransport_curl myTransport( 38 xmlrpc_c::clientXmlTransport_curl::constrOpt() 39 .timeout(10000) // milliseconds 40 .user_agent("sample_add/1.0")); 41 42 xmlrpc_c::client_xml myClient(&myTransport); 43 44 string const methodName("sample.add"); 45 46 xmlrpc_c::paramList sampleAddParms; 47 sampleAddParms.add(xmlrpc_c::value_int(5)); 48 sampleAddParms.add(xmlrpc_c::value_int(7)); 49 50 xmlrpc_c::rpcPtr myRpcP(methodName, sampleAddParms); 51 52 string const serverUrl("http://localhost:8080/RPC2"); 53 54 xmlrpc_c::carriageParm_curl0 myCarriageParm(serverUrl); 55 56 myRpcP->call(&myClient, &myCarriageParm); 57 58 assert(myRpcP->isFinished()); 59 60 int const sum(xmlrpc_c::value_int(myRpcP->getResult())); 61 // Assume the method returned an integer; throws error if not 62 63 cout << "Result of RPC (sum of 5 and 7): " << sum << endl; 64 65 } catch (exception const& e) { 66 cerr << "Client threw error: " << e.what() << endl; 67 } catch (...) { 68 cerr << "Client threw unexpected error." << endl; 69 } 70 71 return 0; 72 } 73