zoukankan
html css js c++ java
使用BOOST实现简单的HTTP网页下载
bool httpGet(/*out*/string& result, const string& host, uint16_t port, const string& url,boost::asio::io_service &_io) { try { string domain = host; boost::asio::ip::tcp::resolver resolver(_io); boost::asio::ip::tcp::resolver_query query(domain, IntToStr(port)); boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); boost::asio::ip::tcp::resolver::iterator end; //Try each endpoint until we successfully establish a connection. boost::asio::ip::tcp::socket socket(_io); 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. boost::asio::streambuf request; std::ostream request_stream(&request); request_stream << "GET " << url << " HTTP/1.0\r\n"; request_stream << "Host: " << socket.remote_endpoint().address() << "\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(4096); boost::asio::read_until(socket, response, "\r\n"); // Check that response is OK. 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::cerr << "[Get Address ]: Invalid response\n"; return false; } if (status_code != 200) { std::cerr << "[Get Address ]: Response returned with status code " << status_code << "\n"; return false; } // Read the response headers, which are terminated by a blank line. 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"){ } // Read until EOF, writing data to output as we go. while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error)) { ; } string temp; while(!response_stream.eof()){ std::getline(response_stream, temp); result.append(temp); } if (error != boost::asio::error::eof) throw boost::system::system_error(error); return true; }catch(std::excetion& e){std::cerr << e.what();}
return false; }
查看全文
相关阅读:
vmware下玩ubuntu总结
.Net Json 字典序列化
Flex Air TitleWindow 拖动范围控制
TimesTen 问题荟萃
TimesTen 时间戳(timestamp)用法
批量数据插入 (.Net, ODBC)
腾讯 360浏览器 调用js问题
[转]Android项目源码混淆问题解决方法
Intent调用大全
View实现涂鸦、撤销以及重做功能【转】
原文地址:https://www.cnblogs.com/k1988/p/2165584.html
最新文章
Python核心数据类型——概览
Python核心数据类型——数字
【原创】无法重新组织表 ; 的索引 (分区 1),因为已禁用页级锁定
【原创】利用typeperf工具收集SQL Server性能数据
【原创】SQL Server 2005复制(二.应用中遇到的问题)
【原创】SQL Server 2005复制(一.可用性测试评估)
【原创】SQL Server 生成脚本的一些参数说明
[linux_shell]linux下设置ls命令显示文件、文件夹颜色
[zz]配置你的 csh/tcsh
Linux终端常用快捷键
热门文章
[cygwin]mintty窗口中输出的中文是乱码
[Linux常用命令]bind
[Linux常用命令]touch
cygwin默认shell配置文件
[linux_shell]Bash通配符与特殊符号
[linux_shell]数据流重定向
[Linux常用命令]Unix & Linux 命令分类表
Des 加密解密 .NetFlex
Mongodb 数据库小知识点
Actionscript 里面怎么设置常见中文字体,例如黑体等
Copyright © 2011-2022 走看看