適合描述最簡單的事情:
Live On Coliru
#include <boost/asio.hpp>
#include <iostream>
int main() {
boost::system::error_code ec;
using namespace boost::asio;
// what we need
io_service svc;
ip::tcp::socket sock(svc);
sock.connect({ {}, 8087 }); // http://localhost:8087 for testing
// send request
std::string request("GET /newGame?name=david HTTP/1.1\r\n\r\n");
sock.send(buffer(request));
// read response
std::string response;
do {
char buf[1024];
size_t bytes_transferred = sock.receive(buffer(buf), {}, ec);
if (!ec) response.append(buf, buf + bytes_transferred);
} while (!ec);
// print and exit
std::cout << "Response received: '" << response << "'\n";
}
這收到完整的響應。你可以用虛擬服務器進行測試:
(也Live On Coliru):
netcat -l localhost 8087 <<< '"status":"okay", "id":"game-23", "letter":2'
這將顯示接收到請求和響應將通過我們的客戶端代碼上面寫出來。
注意,對於更多的想法,你可以看一下例子http://www.boost.org/doc/libs/release/doc/html/boost_asio/examples.html(雖然他們專注於異步通信,因爲這是短耳庫的主題),可能做的任何方式
注意的嚴重的企業(主要是現有的Web服務器分塊編碼,壓縮,保持活動,重定向響應等),你會想要使用庫http://curl.haxx.se/libcurl/ – sehe 2014-11-05 16:33:19