cpp-httplib/example/client.cc

42 lines
975 B
C++
Raw Permalink Normal View History

2012-10-02 17:39:13 -07:00
//
// client.cc
//
2019-06-27 18:48:57 -07:00
// Copyright (c) 2019 Yuji Hirose. All rights reserved.
// MIT License
2012-10-02 17:39:13 -07:00
//
#include <httplib.h>
2012-10-03 17:11:22 -07:00
#include <iostream>
2012-10-02 17:39:13 -07:00
2019-05-07 13:41:33 -07:00
#define CA_CERT_FILE "./ca-bundle.crt"
2012-10-03 17:11:22 -07:00
using namespace std;
2012-10-02 17:39:13 -07:00
2019-04-11 05:13:31 -07:00
int main(void) {
2017-04-21 20:00:00 -07:00
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
2019-04-11 05:13:31 -07:00
httplib::SSLClient cli("localhost", 8080);
2019-05-07 13:41:33 -07:00
// httplib::SSLClient cli("google.com");
// httplib::SSLClient cli("www.youtube.com");
2019-05-07 13:41:33 -07:00
cli.set_ca_cert_path(CA_CERT_FILE);
cli.enable_server_certificate_verification(true);
2017-04-21 20:00:00 -07:00
#else
2019-04-11 05:13:31 -07:00
httplib::Client cli("localhost", 8080);
2017-04-21 20:00:00 -07:00
#endif
2012-10-02 17:39:13 -07:00
2020-08-08 17:50:24 -07:00
if (auto res = cli.Get("/hi")) {
2019-04-11 05:13:31 -07:00
cout << res->status << endl;
cout << res->get_header_value("Content-Type") << endl;
cout << res->body << endl;
2019-05-07 13:41:33 -07:00
} else {
2020-08-08 17:50:24 -07:00
cout << "error code: " << res.error() << std::endl;
2019-05-07 13:41:33 -07:00
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto result = cli.get_openssl_verify_result();
if (result) {
2019-05-07 18:46:15 -07:00
cout << "verify error: " << X509_verify_cert_error_string(result) << endl;
2019-05-07 13:41:33 -07:00
}
#endif
2019-04-11 05:13:31 -07:00
}
2012-10-02 17:39:13 -07:00
2019-04-11 05:13:31 -07:00
return 0;
2012-10-02 17:39:13 -07:00
}