|
| 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 | +/** |
| 25 | + * \file |
| 26 | + * Multi demo using poll/wait |
| 27 | + * |
| 28 | + */ |
| 29 | + |
| 30 | +#include <iostream> |
| 31 | +#include <sstream> |
| 32 | +#include <cstdlib> |
| 33 | + |
| 34 | +#include <curlpp/Easy.hpp> |
| 35 | +#include <curlpp/Exception.hpp> |
| 36 | +#include <curlpp/Multi.hpp> |
| 37 | +#include <curlpp/Options.hpp> |
| 38 | +#include <curlpp/cURLpp.hpp> |
| 39 | + |
| 40 | +#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) |
| 41 | +#pragma comment(lib, "Ws2_32.lib") |
| 42 | +#endif // WIN32 |
| 43 | + |
| 44 | +int main(int argc, char *argv[]) { |
| 45 | + const char *url1 = "http://www.baidu.com"; |
| 46 | + const char *url2 = "https://cn.bing.com"; |
| 47 | + |
| 48 | + try { |
| 49 | + curlpp::initialize(); |
| 50 | + |
| 51 | + curlpp::Easy request1; |
| 52 | + curlpp::Easy request2; |
| 53 | + |
| 54 | + request1.setOpt(new curlpp::options::Url(url1)); |
| 55 | + std::ostringstream os1; |
| 56 | + curlpp::options::WriteStream ws1(&os1); |
| 57 | + request1.setOpt(ws1); |
| 58 | + |
| 59 | + std::list<std::string> headers1; |
| 60 | + headers1.push_back("content-type:text/html; charset=utf-8"); |
| 61 | + request1.setOpt(new curlpp::Options::HttpHeader(headers1)); |
| 62 | + request1.setOpt(new curlpp::options::TimeoutMs(1000)); |
| 63 | + |
| 64 | + |
| 65 | + request2.setOpt(new curlpp::options::Url(url2)); |
| 66 | + std::ostringstream os2; |
| 67 | + curlpp::options::WriteStream ws2(&os2); |
| 68 | + request2.setOpt(ws2); |
| 69 | + request2.setOpt(new curlpp::options::TimeoutMs(1000)); |
| 70 | + |
| 71 | + curlpp::Multi requests; |
| 72 | + requests.add(&request1); |
| 73 | + requests.add(&request2); |
| 74 | + |
| 75 | + /* we start some action by calling perform right away */ |
| 76 | + int nbLeft = 0; |
| 77 | + while (!requests.perform(&nbLeft)) { |
| 78 | + }; |
| 79 | + |
| 80 | + while (nbLeft) { |
| 81 | + // block until activity is detected on at least one of the handles or timeout_ms has passed |
| 82 | + requests.wait(100); |
| 83 | + while (!requests.perform(&nbLeft)) { |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + std::cout << "NB lefts: " << nbLeft << std::endl; |
| 88 | + |
| 89 | + /* See how the transfers went */ |
| 90 | + /* |
| 91 | + Multi::info returns a list of: |
| 92 | + std::pair< curlpp::Easy, curlpp::Multi::Info > |
| 93 | + */ |
| 94 | + const curlpp::Multi::Msgs msgs = requests.info(); |
| 95 | + for (auto pos = msgs.begin(); pos != msgs.end(); pos++) { |
| 96 | + if (pos->second.msg == CURLMSG_DONE) { |
| 97 | + /* Find out which handle this message is about */ |
| 98 | + if (pos->first == &request1) { |
| 99 | + std::cout << "First request completed with status " << pos->second.code << ",data:" << os1.str() << std::endl; |
| 100 | + } else if (pos->first == &request2) { |
| 101 | + std::cout << "Second request completed with status " << pos->second.code << ",data:" << os2.str() << std::endl; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + curlpp::terminate(); |
| 107 | + |
| 108 | + } catch (curlpp::LogicError &e) { |
| 109 | + std::cout << e.what() << std::endl; |
| 110 | + } catch (curlpp::RuntimeError &e) { |
| 111 | + std::cout << e.what() << std::endl; |
| 112 | + } |
| 113 | + |
| 114 | + return EXIT_SUCCESS; |
| 115 | +} |
0 commit comments