runxinzhi.com
首页
百度搜索
使用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; }
相关阅读:
QT+Linux+FFmpeg+C/C++实现RTSP流存储为MP4视频文件
FFmpeg接收RTSP并将H264保存为MP4
第一次在此定义 多重定义 multiple definition of
无法定位程序输入点_ZdaPvj于动态链接库 Qt5Core.dl Qt5Gui.dll上
QObject::startTimer: QTimer can only be used with threads started with QThread
ONVIF开发编译时提示stdsoap2.o中multiple definition of 'namespaces'问题的解决
ONVIF开发(1)生成开发框架
VUE从入门到放弃(项目全流程)————VUE
UI库colorui的使用————小程序
超简单详细的搭建个人网站————静态网站
原文地址:https://www.cnblogs.com/k1988/p/2165584.html
最新文章
'Util.asciify' [undefined] is not a function错误
k-means聚类分析范例程序
unmix和conditional average:消混叠和条件均值
基于Opencv的梯度及其方向
C++中非数nan的定义与范例
Qt在控制台输出中文的解决办法(转载)
二叉树实例学习(五)——更新节点及祖辈节点高度
安装FCIS问题汇总
安装mask-rcnn问题汇总
GAN生成图像论文总结
热门文章
【转载】利用scipy.misc等库对jpg以及png等图像数据预处理(用于深度学习喂数据)
Ubuntu启用IPv6上google的方法
Ubuntu-Python2.7安装 scipy,numpy,matplotlib
·GAN·
ubuntu16.04安装teamviewer12
【转载】Caffe学习:运行caffe自带的两个简单例子
Stanford coursera Andrew Ng 机器学习课程第四周总结(附Exercise 3)
ubuntu16.04 install ros
QString转换为const char*后乱码
Linux内核完全注释读书笔记
Copyright © 2020-2023
润新知