-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path01_get_robotstxt.cpp
More file actions
34 lines (33 loc) · 1.08 KB
/
01_get_robotstxt.cpp
File metadata and controls
34 lines (33 loc) · 1.08 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
// HTTP client to download and output the file theboostcpplibraries.com/robots.txt;
// intentionally calls blocking functions to "slow down" the program; motivation
// to execute the HTTP client in its own thread in the other sample programs
#include <boost/asio.hpp>
#include <string>
#include <sstream>
#include <iostream>
int main()
{
std::string host = "theboostcpplibraries.com";
using namespace boost::asio;
io_service ioservice;
ip::tcp::resolver resolver(ioservice);
ip::tcp::resolver::query query(host, "http");
auto it = resolver.resolve(query); // blocking
ip::tcp::socket socket(ioservice);
socket.connect(*it); // blocking
std::string request = "GET /robots.txt HTTP/1.1\r\nHost: " + host + "\r\n\r\n";
write(socket, buffer(request)); // blocking
streambuf response;
boost::system::error_code ec;
read(socket, response, transfer_all(), ec); // blocking
if (ec == error::eof)
{
std::ostringstream os;
os << &response;
std::string s = os.str();
std::string::size_type idx = s.find("\r\n\r\n");
if (idx != std::string::npos)
s.erase(0, idx + 4);
std::cout << s;
}
}