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; }
相关阅读:
easyui_1
JSONOBJECT
基础回顾—list遍历4种
<input type=file>上传唯一控件
window.open
poi--导入
java字符串的替换
一、IIS搭建前端静态模板_资源加载问题
一、ASP.NET Iframework_SignalR集线器类(v2)
一、ASP.NET Iframework_SignalR永久连接类(v2)
原文地址:https://www.cnblogs.com/k1988/p/2165584.html
最新文章
并发编程导论
HTTP1.0、HTTP 1.1、HTTP 2.0之间的主要区别
锚点定位且不改变url地址
scrollIntoView 与平滑滚动
JS 中的require 和 import 区别
webstorm配置内存参数,解决卡顿
node模块的分类
React中配置Sass引入.scss文件无效
js中的生成器函数
老男孩Python全栈开发(92天全)视频教程 自学笔记07
热门文章
老男孩Python全栈开发(92天全)视频教程 自学笔记06
老男孩Python全栈开发(92天全)视频教程 自学笔记05
老男孩Python全栈开发(92天全)视频教程 自学笔记04
老男孩Python全栈开发(92天全)视频教程 自学笔记03
老男孩Python全栈开发(92天全)视频教程 自学笔记02
老男孩Python全栈开发(92天全)视频教程 自学笔记01
老男孩最新Python全栈开发视频教程(92天全)重点内容梳理笔记 看完就是全栈开发工程师
easyui---基础组件:panel
jqueryui和easyui区别
easyui_2----messager
Copyright © 2020-2023
润新知