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; }
查看全文
相关阅读:
djongo 前端页面展示自定义api返回的列表数据,并拼接到table上
ou are trying to add a non-nullable field 'address' to person without a default; we can't do that (the database needs something to populate existing rows).
python string 类型的公钥转换类型并解密
Django 禁止访问403,CSRF验证失败,相应中断
springboot async
此博客可能不再更新,往后博文将发布在 GitHub 中
css 中 transition 需要注意的问题
学习笔记(九)
微信小程序 drawImage 问题
学习笔记(八)
原文地址:https://www.cnblogs.com/k1988/p/2165584.html
最新文章
一图看懂join、left join、right join、fulljoin间的区别
判断iframe页面是否是顶层页面
关于YII2中编辑页面全局变量冲突问题
jq限制字符个数
判断字符串是否有包含关系(包括中文,中文注意编码格式)
线程的状态
你真的了解volatile吗,关于volatile的那些事
任务调度系统-任务依赖的设计
sql查询调优之where条件排序字段以及limit使用索引的奥秘
加密和签名方案
热门文章
bcd 8421码
分享我的开源项目-springmore
spring+mybatis实现读写分离
FTP+SFTP工具类封装-springmore让开发更简单
springmore-让编程更容易
RuntimeError: Model class apps.settings.licensefile.LicenseFile doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
SHELL脚本执行mongodb 添加用户权限
MongoClient连接数据库
Error: Cannot find module 'bug-versions/package.json'
windows编写linux执行的脚本,并更改格式
Copyright © 2011-2022 走看看