////Sync_client.cpp// ~~~~~~~~~~~~~~~//HTTP client, synchronizing//Copyright (c) 2003-2013 Christopher M. Kohlhoff (Chris at kohlhoff dot com)////distributed under the Boost software License, Version 1.0. (See accompanying//file license_1_0.txt or copy atHttp://www.boost.org/LICENSE_1_0.txt)//#include<iostream>#include<istream>#include<ostream>#include<string>#include<boost/asio.hpp>usingboost::asio::ip::tcp;intMainintargcChar*argv[]) { Try { if(ARGC! =3) {Std::cout<<"usage:sync_client <server> <path>\n"; Std::cout<<"example:\n"; Std::cout<<"sync_client www.boost.org/license_1_0.txt\n"; return 1; } Boost::asio::io_service Io_service; //Get a list of endpoints corresponding to the server name. //resolves the domain name to get a list of servers. Using iterators to accesstcp::resolver Resolver (io_service); //http meaning uses 80 portTcp::resolver::query Query (argv[1],"http"); Tcp::resolver::iterator Endpoint_iterator=resolver.resolve (query); //Try Each endpoint until we successfully establish a connection.tcp::socket socket (io_service); //connection, there seems to be no traversal, just assume the first endpoint OKboost::asio::connect (socket, endpoint_iterator); //Form the request. We Specify the "Connection:close" header so, the//server would close the socket after transmitting the response. this would//Allow us to treat all data up until the EOF as the content. //Build HTTP Headers? Specify connectioin:close to close the socket immediately after the transmit response is reached. This will allow//We put all the pre-EOF data in front of the content//for Streambuf, refer to:http://www.boost.org/doc/libs/1_56_0/doc/html/boost_asio/reference/streambuf.html //Streambuf is derived from std::streambuf and is related to an input-output sequence of streambuf consisting of one or more character arrays. //seems to be able to read and write to the socket directly? BOOST::ASIO::STREAMBUF request; Std::ostream Request_stream (&request); Request_stream<<"GET"<< argv[2] <<"http/1.0\r\n"; Request_stream<<"Host:"<< argv[1] <<"\ r \ n"; Request_stream<<"Accept: */*\r\n"; Request_stream<<"connection:close\r\n\r\n"; //Send the request.boost::asio::write (socket, request); //Read the response status line. The response streambuf would automatically//grow to accommodate the entire line. The growth is limited by passing//a maximum size to the Streambuf constructor. //reads the response status line. The response streambuf will automatically grow until the entire line is complete. And the length of growth is limited to the max_size of Streambuf. //that is, you may not be able to read the Read_until by streambuf size limit once. BOOST::ASIO::STREAMBUF response; Boost::asio::read_until (socket, response,"\ r \ n"); //Check that response is OK. //Check the response message and read the data from the response message. Std::istream Response_stream (&response); STD::stringhttp_version; Response_stream>>http_version; unsignedintStatus_code; Response_stream>>Status_code; STD::stringStatus_message; Std::getline (Response_stream, status_message); if(!response_stream | | http_version.substr (0,5) !="http/") {Std::cout<<"Invalid response\n"; return 1; } if(Status_code! = $) {Std::cout<<"Response returned with status code"<< Status_code <<"\ n"; return 1; } //Read The response headers, which is terminated by a blank line. //Read the response header, ending with a blank line. Boost::asio::read_until (socket, response,"\r\n\r\n"); //Process the response headers. //Output Response HeaderSTD::stringheader; while(Std::getline (Response_stream, header) && header! ="\ r") Std::cout<< Header <<"\ n"; Std::cout<<"\ n"; //Write Whatever content we already has to output. if(Response.size () >0) Std::cout<< &response; //Read until EOF, writing data to output as we go. //read the socket continuously until the read is complete. //Transfer_at_least is a function object. That is, the specified number of bytes can be read to continue the next read and write. Boost::system::error_code error; while(Boost::asio::read (socket, response, Boost::asio::transfer_at_least (1) , error) Std::cout<< &response; if(Error! =boost::asio::error::eof)ThrowBoost::system::system_error (Error); } Catch(std::exception&e) {std::cout<<"Exception:"<< e.what () <<"\ n"; } return 0;}
ASIO example HTTP client, synchronous example Sync_client.cpp