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; }
查看全文
相关阅读:
创建应用程序菜单与菜单融合
FastReport 内置函数的用法与注意
Visual Basic 2005 中的程式語言加強功能
写作关键用词及短语汇总
序列化FastReport
Only Time(惟有时光)
bcd
【分享】微软产品全部序列号,盖茨会哭的~~~
TADOQuery parameter对象被不正确地定义。提供了不一致或不完整的信息
两相四线步进电机驱动代码
原文地址:https://www.cnblogs.com/k1988/p/2165584.html
最新文章
Linux 服务器安全配设置
Java多线程编程经验谈
反向Ajax,第1部分:Comet介绍
VS2010解决方案转换到VS2008 Jimmy
短述抽象类abstract和虚方法virtual Jimmy
委托Delegate +事件Event Jimmy
js两种显示日期方法 Jimmy
接口Interface Jimmy
JSON记录 Jimmy
系统架构系列丛书 Jimmy
热门文章
highcharts使用教程
java开发常用jar包
linux的hostname修改详解
linux修改selinux
iptables详解
MARC与DC
oracle取某几行
java内调用perl、python、dll、cmd
你是合格的程序员吗?(欢迎大家自测)
BOM的学习
Copyright © 2011-2022 走看看