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; }
查看全文
相关阅读:
Restful API
Vue之指令
Scrapy框架
爬虫提高性能:串行、线程进程、异步非阻塞
MongoDB
Beautifulsoup模块
请求库之selenium
php 正则匹配中文
Javascript的"预编译"思考
PHP程序员面试技巧之口试题分享
原文地址:https://www.cnblogs.com/k1988/p/2165584.html
最新文章
[Balkan 2007] Mokia
[ZJOI 2013] K大数查询
【数学 技巧】2.14计数
【上下界网络流 二分】bzoj2406: 矩阵
【上下界网络流】bzoj2502: 清理雪道
【数学 技巧】divisor
Codeforces Global Round 1
【费用流】 ICPC 2016 China Final J. Mr.Panda and TubeMaster
【费用流】bzoj1877: [SDOI2009]晨跑
【二分 最大流】bzoj1532: [POI2005]Kos-Dicing
热门文章
【最大流】bzoj1711: [Usaco2007 Open]Dining吃饭
【二分 最小割】cf808F. Card Game
初涉网络流[EK&dinic]
【主席树 启发式合并】bzoj3123: [Sdoi2013]森林
【点分治】luoguP2664 树上游戏
【线段树合并】bzoj3545: [ONTAK2010]Peaks
【dsu || 线段树合并】bzoj4756: [Usaco2017 Jan]Promotion Counting
Rest_framework-2
Rest_framework-1
Rest Framework源码流程解析
Copyright © 2011-2022 走看看