MRPT  2.0.1
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 /** \example comms_http_client/test.cpp */
11 
12 //! [example-http-get]
13 
14 #include <mrpt/comms/net_utils.h>
15 #include <mrpt/core/exceptions.h>
16 #include <iostream>
17 
18 using namespace mrpt;
19 using namespace mrpt::comms;
20 using namespace mrpt::comms::net;
21 using namespace std;
22 
23 string url = "http://www.google.es/";
24 
25 void Test_HTTP_get()
26 {
27  string content;
28  string errmsg;
30 
31  cout << "Retrieving " << url << "..." << endl;
32 
34  url, content, errmsg, 80, "", "", nullptr, nullptr, &out_headers);
35 
36  if (ret != net::erOk)
37  {
38  cout << " Error: " << errmsg << endl;
39  return;
40  }
41 
42  string typ = out_headers.has("Content-Type") ? out_headers["Content-Type"]
43  : string("???");
44 
45  cout << "Ok: " << content.size() << " bytes of type: " << typ << endl;
46  // cout << content << endl;
47 }
48 //! [example-http-get]
49 
50 int main(int argc, char** argv)
51 {
52  try
53  {
54  if (argc > 1) url = string(argv[1]);
55 
56  Test_HTTP_get();
57  return 0;
58  }
59  catch (const std::exception& e)
60  {
61  std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
62  return -1;
63  }
64 }
STL namespace.
ERRORCODE_HTTP http_get(const string &url, std::vector< uint8_t > &out_content, string &out_errormsg, int port=80, const string &auth_user=string(), const string &auth_pass=string(), int *out_http_responsecode=nullptr, mrpt::system::TParameters< string > *extra_headers=nullptr, mrpt::system::TParameters< string > *out_headers=nullptr, int timeout_ms=1000)
Perform an HTTP GET operation (version for retrieving the data as a std::vector<uint8_t>) ...
Definition: net_utils.cpp:386
void Test_HTTP_get()
ERRORCODE_HTTP
Possible returns from a HTTP request.
Definition: net_utils.h:31
bool has(const std::string &s) const
Definition: TParameters.h:69
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
const char * argv[]
std::string exception_to_str(const std::exception &e)
Builds a nice textual representation of a nested exception, which if generated using MRPT macros (THR...
Definition: exceptions.cpp:59
A set of useful routines for networking.
Definition: net_utils.h:23
const int argc
Serial and networking devices and utilities.
For usage when passing a dynamic number of (numeric) arguments to a function, by name.
Definition: TParameters.h:54



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020