forked from Skewjo/SysLat_Software
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTP_Client_Async.cpp
More file actions
102 lines (79 loc) · 2.91 KB
/
HTTP_Client_Async.cpp
File metadata and controls
102 lines (79 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "stdafx.h"
#include "HTTP_Client_Async.h"
http::response<http::string_body>* session::run(Json::Value dataToSend, char const* host, char const* port, char const* target, int version)
{
// Set up an HTTP POST request message
req_.version(version);
req_.method(http::verb::post);
req_.target(target);
req_.set(http::field::host, host);
req_.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
req_.set(beast::http::field::content_type, "application/json");
Json::StreamWriterBuilder builder;
string output = Json::writeString(builder, dataToSend);
DEBUG_PRINT("JSON VALUE IN HTTP " + output)
req_.body() = output;
std::ostringstream debugOut;
debugOut << req_ << std::endl;
DEBUG_PRINT(debugOut.str().c_str())
req_.prepare_payload();
// Look up the domain name
resolver_.async_resolve(
host,
port,
beast::bind_front_handler(
&session::on_resolve,
shared_from_this()));
return &res_;
}
void session::on_resolve(beast::error_code ec, tcp::resolver::results_type results)
{
if (ec)
return boostFail(ec, "resolve");
// Set a timeout on the operation
stream_.expires_after(std::chrono::seconds(30));
// Make the connection on the IP address we get from a lookup
stream_.async_connect(
results,
beast::bind_front_handler(
&session::on_connect,
shared_from_this()));
}
void session::on_connect(beast::error_code ec, tcp::resolver::results_type::endpoint_type)
{
if (ec)
return boostFail(ec, "connect");
// Set a timeout on the operation
stream_.expires_after(std::chrono::seconds(30));
// Send the HTTP request to the remote host
http::async_write(stream_, req_,
beast::bind_front_handler(
&session::on_write,
shared_from_this()));
}
void session::on_write(beast::error_code ec, std::size_t bytes_transferred) {
boost::ignore_unused(bytes_transferred);
if (ec)
return boostFail(ec, "write");
// Receive the HTTP response
http::async_read(stream_, buffer_, res_,
beast::bind_front_handler(
&session::on_read,
shared_from_this()));
}
void session::on_read(beast::error_code ec, std::size_t bytes_transferred) {
boost::ignore_unused(bytes_transferred);
if (ec)
return boostFail(ec, "read");
// Write the message to standard out - DEBUG_PRINT for Windows...
//std::cout << res_ << std::endl;
std::ostringstream debugOut;
debugOut << res_ << std::endl;
DEBUG_PRINT(debugOut.str().c_str())
// Gracefully close the socket
stream_.socket().shutdown(tcp::socket::shutdown_both, ec);
// not_connected happens sometimes so don't bother reporting it.
if (ec && ec != beast::errc::not_connected)
return boostFail(ec, "shutdown");
// If we get here then the connection is closed gracefully
}