MRPT  2.0.4
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 }
mrpt::system::TParameters::has
bool has(const std::string &s) const
Definition: TParameters.h:69
exceptions.h
mrpt::comms
Serial and networking devices and utilities.
Definition: CClientTCPSocket.h:21
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: BaseAppDataSource.h:15
Test_HTTP_get
void Test_HTTP_get()
Definition: vision_stereo_rectify/test.cpp:25
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::comms::net::erOk
@ erOk
Definition: net_utils.h:33
mrpt::comms::net::ERRORCODE_HTTP
ERRORCODE_HTTP
Possible returns from a HTTP request.
Definition: net_utils.h:31
mrpt::system::TParameters
For usage when passing a dynamic number of (numeric) arguments to a function, by name.
Definition: TParameters.h:54
url
string url
Definition: vision_stereo_rectify/test.cpp:23
argv
const char * argv[]
Definition: RawlogGrabberApp_unittest.cpp:40
net_utils.h
argc
const int argc
Definition: RawlogGrabberApp_unittest.cpp:41
mrpt::comms::net::http_get
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
mrpt::exception_to_str
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
mrpt::comms::net
A set of useful routines for networking.
Definition: net_utils.h:23



Page generated by Doxygen 1.8.17 for MRPT 2.0.4 at Sat Jun 27 14:00:59 UTC 2020