should be all fine.
In particular, the loop that checks for the end of the endpoint list
is fine because, as it seems, those iterators get automatically set to
mean "end" if you don't assign them to anything - it works differently
from, say, a std::list, where you have to explicitly refer to the
end() method of a list instantiation.
The first problem with your code is where you send the server the
"Host" header. You should replace "localhost" with the domain name you
want to read from - in this case:
request_stream << "Host:www.nytimes.com\r\n";
Then we have the (missing) loop to retrieve the data.
The function "read_until" that you are calling will throw when the
socket has no more data to read, and consider also that all overloads
of that function return a size_t with the amount of bytes that it has
transferred to the buffer.
Seems like you have to intercept the throw, in order to know when to
stop calling it. Another option is to use the "read_until" overload
that doesn't throw (it takes an error_code argument, instead) and
maybe check if the returned size_t is not null - then you would break
So far we're just filling the buffer. For printing it out you have to
build an std::istream out of it and get the data out through the
Try to read_until "\r\n", not _until "\r\n\r\n", then getline on the
istream to a string.
If you want I'll post my (working?) code, but since I've learned a lot
by digging my way, I think you can take advantage of doing the same.
Have good coding and feel free to ask further details if you want -
heck, reading boost's template declarations is not very good time...
(don't exclude the fact that I could have said something wrong, it's
something new for me too, I hope to be corrected by more experienced
users out there, in such case)
FSChttp://userscripts.org/scripts/show/59948-Nascondi testo citato
- Mostra testo citato -
Ok, so far my shortest result
void error(const char* p1, const char* p2 = "")
std::cerr << p1 << ' ' << p2 << '\n';
int main(int argc, char* argv)
if (argc != 2) error("Wrong number of arguments!");
boost::asio::ip::tcp::iostream s(host, "http");
s << "GET / HTTP/1.0\r\n";
s << "Host: " << host;
s << "\r\n\r\n" << std::flush;
// std::cout << s.rdbuf();
while (std::getline(s, line))
std::cout << line << std::endl;
Now, I'm wondering how to handle the connection through a proxy. Any
Uh... we can handle a socket as a simple iostream in Boost? Very nice
groups (and using search engines as well, of course).