-
Notifications
You must be signed in to change notification settings - Fork 295
/
Copy pathhttp_client.cpp
130 lines (114 loc) · 4.17 KB
/
http_client.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*!
\file http_client.cpp
\brief HTTP client example
\author Ivan Shynkarenka
\date 08.02.2019
\copyright MIT License
*/
#include "asio_service.h"
#include "server/http/http_client.h"
#include "string/string_utils.h"
#include <iostream>
int main(int argc, char** argv)
{
// HTTP server address
std::string address = "127.0.0.1";
if (argc > 1)
address = argv[1];
// HTTP server port
int port = 8080;
if (argc > 2)
port = std::atoi(argv[2]);
std::cout << "HTTP server address: " << address << std::endl;
std::cout << "HTTP server port: " << port << std::endl;
std::cout << std::endl;
// Create a new Asio service
auto service = std::make_shared<AsioService>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create a new HTTP client
auto client = std::make_shared<CppServer::HTTP::HTTPClientEx>(service, address, port);
std::cout << "Press Enter to stop the client or '!' to reconnect the client..." << std::endl;
try
{
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Reconnect the client
if (line == "!")
{
std::cout << "Client reconnecting...";
client->IsConnected() ? client->ReconnectAsync() : client->ConnectAsync();
std::cout << "Done!" << std::endl;
continue;
}
auto commands = CppCommon::StringUtils::Split(line, ' ', true);
if (commands.size() < 2)
{
std::cout << "HTTP method and URL must be entered!" << std::endl;
continue;
}
if (CppCommon::StringUtils::ToUpper(commands[0]) == "HEAD")
{
auto response = client->SendHeadRequest(commands[1]).get();
std::cout << response << std::endl;
}
else if (CppCommon::StringUtils::ToUpper(commands[0]) == "GET")
{
auto response = client->SendGetRequest(commands[1]).get();
std::cout << response << std::endl;
}
else if (CppCommon::StringUtils::ToUpper(commands[0]) == "POST")
{
if (commands.size() < 3)
{
std::cout << "HTTP method, URL and body must be entered!" << std::endl;
continue;
}
auto response = client->SendPostRequest(commands[1], commands[2]).get();
std::cout << response << std::endl;
}
else if (CppCommon::StringUtils::ToUpper(commands[0]) == "PUT")
{
if (commands.size() < 3)
{
std::cout << "HTTP method, URL and body must be entered!" << std::endl;
continue;
}
auto response = client->SendPutRequest(commands[1], commands[2]).get();
std::cout << response << std::endl;
}
else if (CppCommon::StringUtils::ToUpper(commands[0]) == "DELETE")
{
auto response = client->SendDeleteRequest(commands[1]).get();
std::cout << response << std::endl;
}
else if (CppCommon::StringUtils::ToUpper(commands[0]) == "OPTIONS")
{
auto response = client->SendOptionsRequest(commands[1]).get();
std::cout << response << std::endl;
}
else if (CppCommon::StringUtils::ToUpper(commands[0]) == "TRACE")
{
auto response = client->SendTraceRequest(commands[1]).get();
std::cout << response << std::endl;
}
else
std::cout << "Unknown HTTP method: " << commands[0] << std::endl;
}
}
catch (const std::exception& ex)
{
std::cerr << ex.what() << std::endl;
}
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}