-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClient.cpp
More file actions
57 lines (42 loc) · 820 Bytes
/
Copy pathHttpClient.cpp
File metadata and controls
57 lines (42 loc) · 820 Bytes
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
#include "HttpClient.h"
HttpClient::HttpClient(string address, string port)
{
connected = false;
host_address = address;
host_port = port;
}
HttpClient::~HttpClient(void)
{
Disconnect();
}
void HttpClient::Connect(void)
{
if (!connected)
{
tcp_stream.connect(host_address, host_port);
connected = true;
}
}
void HttpClient::Disconnect(void)
{
if (connected)
{
tcp_stream.close();
connected = false;
}
}
string HttpClient::Get(string uri)
{
Connect();
tcp_stream << "GET " << uri << " HTTP/1.0\r\n";
tcp_stream << "HOST: " << host_address << "\r\n";
tcp_stream << "\r\n";
tcp_stream.flush();
// Need to preserve whitespace
tcp_stream << std::noskipws;
istream_iterator<char> it(tcp_stream);
istream_iterator<char> end;
string results(it, end);
Disconnect();
return results;
}