1 Star 0 Fork 33

韦锴祖/avhttp

forked from Jack.arain/avhttp 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
simplehttp.cpp 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
Jack.arain 提交于 2013-02-01 19:49 . Initial commit
#include <string>
#include <memory>
#include <iostream>
#include <stdio.h>
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/write.hpp>
using boost::asio::ip::tcp;
#define ENABLE_LOGGER
#define LOGGING_COMPRESS_LOGS
#include "logging.hpp"
template<class ... T> inline constexpr bool always_false = false;
#include "simple_http.hpp"
using namespace avhttp;
std::string fetch(const std::string& url, const std::string& proxy)
{
std::string context;
boost::asio::io_context io_context;
boost::asio::spawn(io_context,
[&](boost::asio::yield_context yield) mutable
{
simple_http http{ io_context.get_executor() };
http_request req;
req.set(boost::beast::http::field::user_agent, AVHTTP_VERSION_STRING);
http_response res;
boost::system::error_code ec;
if (proxy.empty())
res = http.async_perform(url, req, yield[ec]);
else
res = http.async_perform(url, proxy, req, yield[ec]);
if (res.result() != boost::beast::http::status::ok) {
std::ostringstream oss;
oss << res << ", error: " << ec.message();
if (proxy.empty())
LOG_DBG << "打开Url '" << url << "' 失败" << oss.str();
else
LOG_DBG << "通过代理 '" << proxy << "' 打开Url '" << url << "' 失败" << oss.str();
return;
}
context = boost::beast::buffers_to_string(res.body().data());
LOG_DBG << "URL: '" << url << "', PROXY: '" << proxy << "', 获取到内容:\n" << context;
});
io_context.run();
return context;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/weikaizu/avhttp.git
git@gitee.com:weikaizu/avhttp.git
weikaizu
avhttp
avhttp
master

搜索帮助