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; }
查看全文
相关阅读:
14.使用ConfigMap管理应用配置
13.实战交付一套dubbo微服务到k8s集群(6)之交付dubbo服务的消费者集群到K8S
12.实战交付一套dubbo微服务到k8s集群(5)之交付dubbo-monitor到K8S集群
11.实战交付一套dubbo微服务到k8s集群(4)之使用Jenkins进行持续构建交付dubo服务的提供者
11.实战交付一套dubbo微服务到k8s集群(3)之dubbo微服务底包镜像制作
10.实战交付一套dubbo微服务到k8s集群(3)之二进制安装Maven
9.实战交付一套dubbo微服务到k8s集群(2)之Jenkins部署
《说透中台》读书笔记
消息队列高手课——04 | 如何利用事务消息实现分布式事务?
消息队列高手课——03 | 消息模型:主题和队列有什么区别?
原文地址:https://www.cnblogs.com/k1988/p/2165584.html
最新文章
VUE 网页 扫码登录(微信)
微信小程序 微信支付
小程序 大转盘 抽奖 canvas animation
小程序 生成自定义小程序二维码码 通过 canvas 生成海报 保存成图片
小程序自定义音频播放
小程序富文本 图片过大问题处理
小程序 商城多规格选择 购物车 sku 添加 简易demo
vue H5 公众号 授权分享
session + cookie 实现登录功能
常见错误检查
热门文章
PHP图像操作__验证码生成函数
登录注册信息检查
文件操作_遍历操作文件夹内所有文件
图像处理_等比例居中裁剪图片_可添加水印
验证滑块代码
元素操作工具类
自定义Ajax类
Python基础
内网scp传输慢问题分析
15.交付apollo-configservice到K8S
Copyright © 2011-2022 走看看