example01.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) <2002-2005> <Jean-Philippe Barrette-LaPierre>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (curlpp), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. /**
  24. * \file
  25. * Setting and copying options.
  26. *
  27. */
  28. #include <string>
  29. #include <sstream>
  30. #include <iostream>
  31. #include <curlpp/cURLpp.hpp>
  32. #include <curlpp/Easy.hpp>
  33. #include <curlpp/Options.hpp>
  34. namespace
  35. {
  36. const long MyPort = 80;
  37. }
  38. /**
  39. * This example is made to show you how you can use the Options.
  40. */
  41. int main(int, char **)
  42. {
  43. try
  44. {
  45. curlpp::Cleanup myCleanup;
  46. // First easy example.
  47. {
  48. // The first easiest example is to retreive the content of
  49. // a web page and put it in a stream.
  50. std::cout << curlpp::options::Url("http://example.com");
  51. // You don't need to use just the standard outputs. You
  52. // can use any stream:
  53. std::ostringstream os;
  54. os << curlpp::options::Url("http://example.com");
  55. }
  56. // More elaborate example.
  57. {
  58. // What the previous example done there was simply
  59. // to create a curlpp::Easy class, which is the basic
  60. // object in cURLpp, and then set the Url option.
  61. // curlpp::options classes are the primitives that allow to specify
  62. // values to the requests.
  63. curlpp::options::Url myUrl(std::string("http://example.com"));
  64. curlpp::Easy myRequest;
  65. myRequest.setOpt(myUrl);
  66. // Now that all the options we wanted to set are there, we need to
  67. // actually do the request. the "perform" method does actually that.
  68. // With that call, the request will be done and the content of that URL
  69. // will be printed in std::cout (which is the default).
  70. myRequest.perform();
  71. // If we wanted to put the content of the URL within a string stream
  72. // (or any type of std::ostream, for that matter), like the first example,
  73. // we would use the WriteStrem option like this:
  74. std::ostringstream os;
  75. curlpp::options::WriteStream ws(&os);
  76. myRequest.setOpt(ws);
  77. myRequest.perform();
  78. // There is some shorcut within curlpp that allow you to write shorter code
  79. // like this:
  80. os << myRequest;
  81. // That would do exactly what the previous code was doing.
  82. }
  83. // Creation of the URL option.
  84. curlpp::options::Url myUrl(std::string("http://example.com"));
  85. // Copy construct from the other URL.
  86. curlpp::options::Url myUrl2(myUrl);
  87. // Creation of the port option.
  88. curlpp::options::Port myPort(MyPort);
  89. // Creation of the request.
  90. curlpp::Easy myRequest;
  91. // Creation of an option that contain a copy of the URL option.
  92. curlpp::OptionBase *mytest = myUrl.clone();
  93. myRequest.setOpt(*mytest);
  94. // You can reuse the base option for other type of option
  95. // and set the option to the request. but first, don't forget
  96. // to delete the previous memory. You can delete it since the
  97. // option is internally duplicated for the request.
  98. delete mytest;
  99. mytest = myPort.clone();
  100. myRequest.setOpt(*mytest);
  101. delete mytest;
  102. // You can clone an option directly to the same type of
  103. // option.
  104. curlpp::options::Url *myUrl3 = myUrl.clone();
  105. myRequest.setOpt(myUrl3);
  106. // Now myUrl3 is owned by the request we will NOT use
  107. // it anymore.
  108. // You don't need to declare an option if you just want
  109. // to use it once.
  110. myRequest.setOpt(curlpp::options::Url("example.com"));
  111. // Note that the previous line wasn't really efficient
  112. // because we create the option, this option is duplicated
  113. // for the request and then the option destructor is called.
  114. // You can use this instead:
  115. myRequest.setOpt(new curlpp::options::Url("example.com"));
  116. // Note that with this the request will use directly this
  117. // instance we just created. Be aware that if you pass an
  118. // Option pointer to the setOpt function, it will consider
  119. // the instance has its own instance. The Option instance
  120. // will be deleted when the request will be deleted, so
  121. // don't use the instance further in your code.
  122. // Doing the previous line is efficient as this:
  123. myRequest.setOpt(myUrl.clone());
  124. // You can retreive the value of a specific option.
  125. std::cout << myUrl2.getValue() << std::endl;
  126. // Perform the transaction with the options set.
  127. myRequest.perform();
  128. }
  129. catch( curlpp::RuntimeError &e )
  130. {
  131. std::cout << e.what() << std::endl;
  132. }
  133. catch( curlpp::LogicError &e )
  134. {
  135. std::cout << e.what() << std::endl;
  136. }
  137. return 0;
  138. }