asio学习笔记-简单的http客户端
2009-02-26 16:48
447 查看
// asion_simple.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <istream> #include <ostream> #include <string> #include <boost/asio.hpp> using boost::asio::ip::tcp; int main(int argc, char* 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; } // 定义一个 io_service boost::asio::io_service io_service; // Get a list of endpoints corresponding to the server name. // 定义一个域名解析器 tcp::resolver resolver(io_service); //定义一个查询query // tcp::resolver::query query("www.baidu.com", "http"); tcp::resolver::query query("127.0.0.1", "8000"); //解析query,获得节点 但是不明白为什么需要迭代器 //节点应该只有0个或者1个 tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); tcp::resolver::iterator end; // Try each endpoint until we successfully establish a connection. //定义socket并且连接 tcp::socket socket(io_service); boost::system::error_code error = boost::asio::error::host_not_found; while (error && endpoint_iterator != end) { socket.close(); socket.connect(*endpoint_iterator++, error); } if (error) throw boost::system::system_error(error); // Form 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. //定义一个字符流,编辑HTTP 查询命令 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. //读取查询结果 boost::asio::streambuf response; //读取http的文件头,然后返回 boost::asio::read_until(socket, response, "/r/n"); // Check that response is OK. //获得http状态码 std::istream response_stream(&response); std::string http_version; response_stream >> http_version; unsigned int status_code; response_stream >> status_code; std::string status_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 != 200) { std::cout << "Response returned with status code " << status_code << "/n"; return 1; } // Read the response headers, which are terminated by a blank line. //读取http头 boost::asio::read_until(socket, response, "/r/n/r/n"); // Process the response headers. std::string header; while (std::getline(response_stream, header) && header != "/r") std::cout << header << "/n"; std::cout << "/n"; // Write whatever content we already have to output. if (response.size() > 0) std::cout << &response; // Read until EOF, writing data to output as we go. //读取显示其他内容 while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error)) std::cout << &response; if (error != boost::asio::error::eof) throw boost::system::system_error(error); } catch (std::exception& e) { std::cout << "Exception: " << e.what() << "/n"; } system("pause"); return 0; }
相关文章推荐
- 图解HTTP协议 第2章 简单的HTTP协议学习笔记
- zeromq学习笔记2——简单的客户端和服务端测试程序
- 【UNP学习笔记】一个简单的服务器/客户端程序
- 分布式学习笔记1通过Java自己实现简单的HTTP RPC框架
- Netty学习笔记之一(Netty解析简单的Http Post Json 请求)
- restlet2.1 学习笔记(九)简单验证客户端用户名与密码
- 学习笔记之 WCF安全(5) 数字证书+ 客户端证书验证(wshttpBinding)
- 学习笔记之 WCF安全(1) SSL安全,无客户端验证(wsHttpBinding)
- ASIO学习三:asioftp (FTP客户端的简单实现)
- UNIX环境编程学习笔记-----编程实例------一个简单的服务器端和客户端程序
- node.js学习笔记之HTTP获取客户端请求信息
- ROS学习笔记十:用C++编写一个简单的服务和客户端
- 学习笔记之 WCF安全(6) 数字证书 自定义证书验证+ 客户端 证书验证(wshttpBinding)
- asio学习之三:asio实现简单的服务器和客户端
- twisted 学习笔记二:创建一个简单TCP客户端
- 图解http学习笔记2.简单http协议
- Node.js学习笔记(4)——除了HTTP(服务器和客户端)部分
- boost_asio学习笔记[2] - 客户端异步通讯
- 学习笔记之-----OkHttp之使用与简单封装
- zookeeper学习笔记1-zk简单安装及zkCli客户端命令