|
|
@ -1,5 +1,4 @@
|
|
|
|
#include <iostream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "webcc/http_client_session.h"
|
|
|
|
#include "webcc/http_client_session.h"
|
|
|
|
#include "webcc/logger.h"
|
|
|
|
#include "webcc/logger.h"
|
|
|
@ -18,130 +17,99 @@ bool kSslVerify = true;
|
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
void GetBoostOrgLicense(HttpClientSession& session) {
|
|
|
|
void ExampleBasic() {
|
|
|
|
try {
|
|
|
|
HttpClientSession session;
|
|
|
|
auto r = session.Request(HttpRequestArgs{ "GET" }.
|
|
|
|
|
|
|
|
url("https://www.boost.org/LICENSE_1_0.txt").
|
|
|
|
|
|
|
|
ssl_verify(kSslVerify));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::cout << r->content() << std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (const webcc::Exception& e) {
|
|
|
|
|
|
|
|
std::cout << "Exception: " << e.what() << std::endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
void Test(HttpClientSession& session) {
|
|
|
|
|
|
|
|
HttpResponsePtr r;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
r = session.Request(HttpRequestArgs{ "GET" }.
|
|
|
|
auto r = session.Request(HttpRequestArgs{"GET"}
|
|
|
|
url("http://httpbin.org/get"). // moved
|
|
|
|
.url("http://httpbin.org/get")
|
|
|
|
parameters({ "key1", "value1", "key2", "value2" }). // moved
|
|
|
|
.parameters({"key1", "value1", "key2", "value2"})
|
|
|
|
headers({ "Accept", "application/json" }). // moved
|
|
|
|
.headers({"Accept", "application/json"})
|
|
|
|
buffer_size(1000));
|
|
|
|
.buffer_size(1000));
|
|
|
|
|
|
|
|
|
|
|
|
std::cout << r->content() << std::endl;
|
|
|
|
std::cout << r->content() << std::endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// If you want to create the args object firstly, there might be an extra
|
|
|
|
|
|
|
|
// call to its move constructor (maybe only for MSVC).
|
|
|
|
// If you want to create the args object firstly, there'll be an extra call
|
|
|
|
// - constructor: HttpRequestArgs{ "GET" }
|
|
|
|
// to its move constructor.
|
|
|
|
// - move constructor: auto args = ...
|
|
|
|
// - constructor: HttpRequestArgs{ "GET" }
|
|
|
|
void ExampleArgs() {
|
|
|
|
// - move constructor: auto args = ...
|
|
|
|
HttpClientSession session;
|
|
|
|
|
|
|
|
|
|
|
|
auto args = HttpRequestArgs{"GET"}.
|
|
|
|
|
|
|
|
url("http://httpbin.org/get").
|
|
|
|
|
|
|
|
parameters({ "key1", "value1", "key2", "value2" }).
|
|
|
|
|
|
|
|
headers({ "Accept", "application/json" }).
|
|
|
|
|
|
|
|
buffer_size(1000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
r = session.Request(std::move(args));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Use pre-defined wrappers.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
r = session.Get("http://httpbin.org/get",
|
|
|
|
auto args = HttpRequestArgs{"GET"}
|
|
|
|
{ "key1", "value1", "key2", "value2" },
|
|
|
|
.url("http://httpbin.org/get")
|
|
|
|
{ "Accept", "application/json" },
|
|
|
|
.parameters({"key1", "value1", "key2", "value2"})
|
|
|
|
HttpRequestArgs{}.buffer_size(1000));
|
|
|
|
.headers({"Accept", "application/json"})
|
|
|
|
|
|
|
|
.buffer_size(1000);
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Note the std::move().
|
|
|
|
// HTTPS is auto-detected from the URL schema.
|
|
|
|
session.Request(std::move(args));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Use pre-defined wrappers.
|
|
|
|
r = session.Post("httpt://httpbin.org/post", "{ 'key': 'value' }", true,
|
|
|
|
void ExampleWrappers() {
|
|
|
|
{"Accept", "application/json"},
|
|
|
|
HttpClientSession session;
|
|
|
|
HttpRequestArgs{}.ssl_verify(false).buffer_size(1000));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::cout << r->status() << std::endl << r->content() << std::endl;
|
|
|
|
session.Get("http://httpbin.org/get", {"key1", "value1", "key2", "value2"},
|
|
|
|
|
|
|
|
{"Accept", "application/json"},
|
|
|
|
|
|
|
|
HttpRequestArgs{}.buffer_size(1000));
|
|
|
|
|
|
|
|
|
|
|
|
} catch (const webcc::Exception& e) {
|
|
|
|
session.Post("http://httpbin.org/post", "{ 'key': 'value' }", true,
|
|
|
|
std::cout << "Exception: " << e.what() << std::endl;
|
|
|
|
{"Accept", "application/json"});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TestKeepAlive2(HttpClientSession& session) {
|
|
|
|
// HTTPS is auto-detected from the URL scheme.
|
|
|
|
try {
|
|
|
|
void ExampleHttps() {
|
|
|
|
auto r = session.Request(webcc::HttpRequestArgs("GET").
|
|
|
|
HttpClientSession session;
|
|
|
|
url("https://api.github.com/events").
|
|
|
|
|
|
|
|
ssl_verify(false).buffer_size(1500));
|
|
|
|
|
|
|
|
} catch (const Exception& e) {
|
|
|
|
|
|
|
|
std::cout << "Exception: " << e.what() << std::endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TestKeepAlive3(HttpClientSession& session) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
auto r = session.Request(webcc::HttpRequestArgs("GET").
|
|
|
|
|
|
|
|
url("https://www.boost.org/LICENSE_1_0.txt").
|
|
|
|
|
|
|
|
ssl_verify(false));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//std::cout << r->content() << std::endl;
|
|
|
|
auto r = session.Request(HttpRequestArgs{"GET"}
|
|
|
|
|
|
|
|
.url("https://httpbin.org/get")
|
|
|
|
|
|
|
|
.parameters({"key1", "value1", "key2", "value2"})
|
|
|
|
|
|
|
|
.headers({"Accept", "application/json"})
|
|
|
|
|
|
|
|
.ssl_verify(kSslVerify));
|
|
|
|
|
|
|
|
|
|
|
|
} catch (const Exception& e) {
|
|
|
|
std::cout << r->content() << std::endl;
|
|
|
|
std::cout << "Exception: " << e.what() << std::endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TestKeepAlive4(HttpClientSession& session) {
|
|
|
|
void ExampleKeepAlive(const std::string& url) {
|
|
|
|
try {
|
|
|
|
HttpClientSession session;
|
|
|
|
auto r = session.Request(webcc::HttpRequestArgs("GET").
|
|
|
|
|
|
|
|
url("https://www.google.com").
|
|
|
|
|
|
|
|
ssl_verify(false));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//std::cout << r->content() << std::endl;
|
|
|
|
// Keep-Alive
|
|
|
|
|
|
|
|
session.Request(webcc::HttpRequestArgs("GET").url(url).
|
|
|
|
|
|
|
|
ssl_verify(kSslVerify));
|
|
|
|
|
|
|
|
|
|
|
|
} catch (const Exception& e) {
|
|
|
|
// Close
|
|
|
|
std::cout << "Exception: " << e.what() << std::endl;
|
|
|
|
session.Request(webcc::HttpRequestArgs("GET").url(url).
|
|
|
|
}
|
|
|
|
ssl_verify(kSslVerify).
|
|
|
|
|
|
|
|
headers({ "Connection", "Close" }));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Keep-Alive
|
|
|
|
|
|
|
|
session.Request(webcc::HttpRequestArgs("GET").url(url).
|
|
|
|
|
|
|
|
ssl_verify(kSslVerify));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
void Sleep(int seconds) {
|
|
|
|
|
|
|
|
if (seconds > 0) {
|
|
|
|
|
|
|
|
LOG_INFO("Sleep %d seconds...", seconds);
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(seconds));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
int main() {
|
|
|
|
WEBCC_LOG_INIT("", LOG_CONSOLE);
|
|
|
|
WEBCC_LOG_INIT("", LOG_CONSOLE);
|
|
|
|
|
|
|
|
|
|
|
|
HttpClientSession session;
|
|
|
|
// Note that the exception handling is mandatory.
|
|
|
|
|
|
|
|
|
|
|
|
// TEST keep-alive.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
// Keep-Alive by default
|
|
|
|
//ExampleBasic();
|
|
|
|
session.Get("http://httpbin.org/get");
|
|
|
|
//ExampleArgs();
|
|
|
|
|
|
|
|
//ExampleWrappers();
|
|
|
|
|
|
|
|
//ExampleHttps();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Boost.org doesn't support persistent connection so always includes
|
|
|
|
|
|
|
|
// "Connection: Close" header in the response.
|
|
|
|
|
|
|
|
|
|
|
|
session.Get("http://httpbin.org/get", {}, { "Connection", "Close" });
|
|
|
|
// Both Google and GitHub support persistent connection but they don't like
|
|
|
|
|
|
|
|
// to include "Connection: Keep-Alive" header in the responses.
|
|
|
|
|
|
|
|
|
|
|
|
session.Get("http://httpbin.org/get");
|
|
|
|
//ExampleKeepAlive("http://httpbin.org/get");
|
|
|
|
|
|
|
|
//ExampleKeepAlive("https://www.boost.org/LICENSE_1_0.txt");
|
|
|
|
|
|
|
|
//ExampleKeepAlive("https://www.google.com");
|
|
|
|
|
|
|
|
//ExampleKeepAlive("https://api.github.com/events");
|
|
|
|
|
|
|
|
|
|
|
|
} catch (const Exception& e) {
|
|
|
|
} catch (const Exception& e) {
|
|
|
|
std::cout << "Exception: " << e.what() << std::endl;
|
|
|
|
std::cout << "Exception: " << e.what() << std::endl;
|
|
|
|