基本上,a具有用于对服务器进行http调用并处理响应的代码,但我想保护传输的数据并进行https调用.
Basically a have the code to make an http call to server and handle the response but I want to secure the data travelling and make the call https.
为此,我有这个executor.hpp
For that I have this 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
and this 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; }及以下是编译器上的完整错误
and below is the full error on compiler
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 ]推荐答案
错误消息引用的类Executor不在您的代码中.您在编译正确的东西吗?
The error message refers to a class Executor which isn't in your code. Are you compiling the right thing?
boost::asio::ssl::context ctx(svc, ssl::context::method::sslv23_client);
这不应该编译,除非svc也是代码中的类型.使用大括号.
this should not compile, unless svc is also a type in your code. Use braces.
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssock(svc, ctx);
这肯定不会在那时编译.同样的问题.
This will most definitely not compile then. Same problem.
Connect(Data &data) : socket(ioService) {
- 未声明套接字.它是什么? (假设您可能是ssock的意思.) 未声明
- ioService.它是什么? (假设您可能是svc的意思.)
- Socket is not declared. What is it? (Assuming you might have meant ssock).
- ioService is not declared. What is it? (Assuming you might have meant svc).
对比度
int Connect::write(CreatRequest &requestCreator) {与
int write(RequestCreator &requestcreator);是哪个?编译器不会猜测您的意思.如果您是说RequestCreator,则必须输入该字符,而不是CreatRequest.
Which is it? Compilers are not going to guess what you mean. If you mean RequestCreator you're gonna have to type that, and not CreatRequest.
总而言之,没有真正的问题,只是盲目地复制/粘贴了类似代码"的文本.
All in all, there's no real question, just a lot of blindly copied/paste "code-like" text.
以最简单的方式修复以上所有问题:
Fixing all the above in the most straightforward way possible:
在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); }更多推荐
使用Boost Asio建立SSL连接
发布评论