-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpClient.cpp
66 lines (57 loc) · 1.97 KB
/
httpClient.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
#include "httpClient.h"
#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <boost/asio/ip/tcp.hpp>
#include "common.h"
int HttpClient::getPage(const char* host, const char* path, string& page)
{
boost::asio::ip::tcp::iostream httpStream;
// Establish a connection to the server.
httpStream.connect(host, "http");
if (!httpStream)
{
//std::cout << "Unable to connect: " << httpStream.error().message()<< "\n";
LogErr("Unable to connect: %s:\n", host);
return 1;
}
// Send the request. We specify the "Connection: close" header so that the
// server will close the socket after transmitting the response. This will
// allow us to treat all data up until the EOF as the content.
httpStream << "GET " << path << " HTTP/1.0\r\n";
httpStream << "Host: " << host << "\r\n";
httpStream << "Accept: */*\r\n";
httpStream << "Connection: close\r\n\r\n";
// By default, the stream is tied with itself. This means that the stream
// automatically flush the buffered output before attempting a read. It is
// not necessary not explicitly flush the stream at this point.
// Check that response is OK.
std::string http_version;
httpStream >> http_version;
unsigned int status_code;
httpStream >> status_code;
std::string status_message;
std::getline(httpStream, status_message);
if (!httpStream || http_version.substr(0, 5) != "HTTP/")
{
LogErr("Invalid response\n");
return 1;
}
if (status_code != 200)
{
LogErr("Response returned with status code:%d\n", status_code);
return 1;
}
// Process the response headers, which are terminated by a blank line.
std::string header;
while (std::getline(httpStream, header) && header != "\r")
{
//std::cout << header << "\n";
}
ostringstream oss;
oss << httpStream.rdbuf();
page = oss.str();
LogDbg("HttpSuccess:%s%s\n", host, path);
return 0;
}