基本上有一个代码可以对服务器进行http调用并处理响应,但我想保护数据的传输并进行呼叫https。
为此我有这个executor.hpp
class Executor
{
public :
Executor(Cli &cli):ssock(svc)
{
//! Get a list of endpoints corresponding to the server name.
boost::asio::ip::tcp::resolver resolver(svc);
boost::asio::ip::tcp::resolver::query query(cli.getIP(), "8080");
endpoint_iterator = resolver.resolve(query);
}
int connect();
int write(RequestCreator &requestcreator);
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> &getSocket() { return ssock; }
private :
boost::asio::io_service svc;
boost::asio::ssl::context ctx(svc, ssl::context::method::sslv23_client);
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssock(svc, ctx);
boost::asio::ip::tcp::resolver::iterator endpoint_iterator;
};
和这个executor.cpp
int Executor::connect()
{
boost::system::error_code ec ;
boost::asio::connect(ssock.lowest_layer(), endpoint_iterator );
ssock.handshake(boost::asio::ssl::stream_base::handshake_type::client);
if (ec)
{
ssock.lowest_layer().close();
svc.stop();
return 0;
}
return 1;
}
int Executor::write(RequestCreator &requestCreator)
{
boost::system::error_code ec ;
boost::asio::write(ssock, requestCreator.getRequest(), ec);
if (ec)
{
ssock.lowest_layer().close();
svc.stop();
return 0;
}
return 1;
}
及以下是编译器
的完整错误c:\cygwin64\home\admin\sandbox\webcli\executor.hpp(30) : error C2061: syntax error : identifier 'svc'
c:\cygwin64\home\admin\sandbox\webcli\executor.hpp(31) : error C2061: syntax error : identifier 'svc'
c:\cygwin64\home\admin\sandbox\webcli\executor.hpp(15) : error C2436: 'ssock' : member function or nested class in constructor initializer list
c:\cygwin64\home\admin\sandbox\webcli\executor.hpp(25) : error C3867: 'Executor::ssock': function call missing argument list; use '&Executor::ssock' to create a pointer to member
c:\cygwin64\home\admin\sandbox\webcli\executor.hpp(25) : error C2440: 'return' : cannot convert from 'boost::asio::ssl::stream<Stream> (__cdecl Executor::* )(void)' to 'boost::asio::ssl::stream<Stream> &'
with
[
Stream=boost::asio::ip::tcp::socket
]
答案 0 :(得分:1)
错误消息指的是您的代码中没有的类Executor
。你在编译正确的东西吗?
boost::asio::ssl::context ctx(svc, ssl::context::method::sslv23_client);
这不应该编译,除非svc
也是代码中的类型。使用大括号。
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssock(svc, ctx);
这绝对不会编译。同样的问题。
Connect(Data &data) : socket(ioService) {
ssock
)。ioService
未声明。它是什么? (假设你可能意味着svc
)。对比度
int Connect::write(CreatRequest &requestCreator) {
带
int write(RequestCreator &requestcreator);
这是什么?编译器不会猜测你的意思。如果您的意思是RequestCreator
,那么您必须输入该内容,而不是CreatRequest
。
总而言之,没有真正的问题,只是大量盲目复制/粘贴&#34;类似代码&#34;文本。
以最直接的方式解决以上所有问题:
<强> Live On Coliru 强>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
struct Data {
std::string getIP() const { return "localhost"; }
};
class Connect {
public:
Connect(Data &data) {
//! Get a list of endpoints corresponding to the server name.
boost::asio::ip::tcp::resolver resolver(svc);
boost::asio::ip::tcp::resolver::query query(data.getIP(), "8080");
endpoint_iterator = resolver.resolve(query);
}
int connectServer();
struct RequestCreator {
auto getRequest() const { return boost::asio::buffer("GET / HTTP/1.1\r\n\r\n"); }
};
int write(RequestCreator &requestcreator);
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> &getSocket() { return ssock; }
private:
boost::asio::io_service svc;
boost::asio::ssl::context ctx{svc, boost::asio::ssl::context::method::sslv23_client};
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssock{svc, ctx};
boost::asio::ip::tcp::resolver::iterator endpoint_iterator;
};
int Connect::connectServer() {
boost::system::error_code ec;
boost::asio::connect(ssock.lowest_layer(), endpoint_iterator);
if (ec) {
ssock.lowest_layer().close();
svc.stop();
return 0;
}
return 1;
}
int Connect::write(RequestCreator &requestCreator) {
boost::system::error_code ec;
boost::asio::write(ssock, requestCreator.getRequest(), ec);
if (ec) {
ssock.lowest_layer().close();
svc.stop();
return 0;
}
return 1;
}
main() {
Data data;
Connect c(data);
}