Add a base class for http async clients; refine examples.

This commit is contained in:
Chunting Gu
2019-02-12 17:06:26 +08:00
parent 9ecec5de0b
commit 43eaf90621
43 changed files with 803 additions and 507 deletions
@@ -0,0 +1,11 @@
add_executable(http_ssl_async_client main.cc)
# TODO
set(SSL_LIBS ${OPENSSL_LIBRARIES})
if(WIN32)
set(SSL_LIBS ${SSL_LIBS} crypt32)
endif()
target_link_libraries(http_ssl_async_client webcc ${Boost_LIBRARIES})
target_link_libraries(http_ssl_async_client "${CMAKE_THREAD_LIBS_INIT}")
target_link_libraries(http_ssl_async_client ${SSL_LIBS})
+58
View File
@@ -0,0 +1,58 @@
#include <iostream>
#include "boost/asio/io_context.hpp"
#include "webcc/http_ssl_async_client.h"
#include "webcc/logger.h"
int main(int argc, char* argv[]) {
std::string host;
std::string url;
if (argc != 3) {
host = "www.boost.org";
url = "/LICENSE_1_0.txt";
} else {
host = argv[1];
url = argv[2];
}
std::cout << "Host: " << host << std::endl;
std::cout << "URL: " << url << std::endl;
std::cout << std::endl;
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
boost::asio::io_context io_context;
// Leave port to default value.
auto request = webcc::HttpRequest::Make(webcc::kHttpGet, url, host);
// Verify the certificate of the peer or not.
// See HttpSslClient::Request() for more details.
bool ssl_verify = false;
webcc::HttpSslAsyncClientPtr client{
new webcc::HttpSslAsyncClient{ io_context, 2000, ssl_verify }
};
// Response callback.
auto callback = [](webcc::HttpResponsePtr response, webcc::Error error,
bool timed_out) {
if (error == webcc::kNoError) {
std::cout << response->content() << std::endl;
} else {
std::cout << DescribeError(error);
if (timed_out) {
std::cout << " (timed out)";
}
std::cout << std::endl;
}
};
client->Request(request, callback);
io_context.run();
return 0;
}