1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
/* +------------------------------------------------------------------------+
| Mobile Robot Programming Toolkit (MRPT) |
| https://www.mrpt.org/ |
| |
| Copyright (c) 2005-2023, Individual contributors, see AUTHORS file |
| See: https://www.mrpt.org/Authors - All rights reserved. |
| Released under BSD License. See: https://www.mrpt.org/License |
+------------------------------------------------------------------------+ */
/** \example comms_http_client/test.cpp */
//! [example-http-get]
#include <mrpt/comms/net_utils.h>
#include <mrpt/core/exceptions.h>
#include <iostream>
using namespace mrpt;
using namespace mrpt::comms;
using namespace mrpt::comms::net;
std::string url = "http://www.google.es/";
void Test_HTTP_get()
{
std::string content;
mrpt::comms::net::HttpRequestOptions httpOptions;
mrpt::comms::net::HttpRequestOutput httpOut;
std::cout << "Retrieving " << url << "..." << std::endl;
http_errorcode ret = http_get(url, content, httpOptions, httpOut);
if (ret != net::http_errorcode::Ok)
{
std::cout << " Error: " << httpOut.errormsg << std::endl;
return;
}
string typ = httpOut.out_headers.count("Content-Type")
? httpOut.out_headers.at("Content-Type")
: string("???");
std::cout << "Ok: " << content.size() << " bytes of type: " << typ
<< std::endl;
}
//! [example-http-get]
int main(int argc, char** argv)
{
try
{
if (argc > 1) url = string(argv[1]);
Test_HTTP_get();
return 0;
}
catch (const std::exception& e)
{
std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
return -1;
}
}
|