[英]How to connect secure websocket by using Poco in C++
我試圖通過使用C ++連接echo.websocket.org來使用安全的websocket。 它可以在https://www.websocket.org/echo.html的 javascript中工作。
我的解決方案是使用Poco庫,我的代碼如下所示。
Poco::Net::initializeSSL();
Poco::Net::Context context(Poco::Net::Context::CLIENT_USE, "", "", "", Poco::Net::Context::VERIFY_RELAXED, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
Poco::Net::HTTPSClientSession Client("echo.websocket.org", 443, &context);
//SSL Exception on above line
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/?encoding=text", Poco::Net::HTTPMessage::HTTP_1_1);
request.set("origin", "https://www.websocket.org");
Poco::Net::HTTPResponse response;
try
{
Poco::Net::WebSocket webSocket(Client, request, response);
std::string str = "Hello!";
char receiveBuff[256];
int len = webSocket.sendFrame(str.data(), str.size(), Poco::Net::WebSocket::FRAME_TEXT);
std::cout << "Sent bytes " << len << std::endl;
int flags = 0;
int rlen = webSocket.receiveFrame(receiveBuff, 256, flags);
std::cout << "Received bytes " << rlen << std::endl;
std::cout << receiveBuff << std::endl;
webSocket.close();
}
catch (std::exception &e)
{
std::cout << "Exception " << e.what();
}
但是我收到此代碼的SSL異常。 有人可以幫我嗎? 謝謝
至於如何手動操作,將是這樣的:
class MyInvalidCertificateHandler: public InvalidCertificateHandler
{
public:
MyInvalidCertificateHandler(bool handleOnServerSide):
InvalidCertificateHandler(handleOnServerSide) { }
virtual ~MyInvalidCertificateHandler() { }
void onInvalidCertificate(const void*, VerificationErrorArgs& errorCert)
{
//log or something
errorCert.setIgnoreError(false);
}
};
std::string certFilename = "my/cert/filename"; // path to file
SharedPtr<InvalidCertificateHandler> ptrCert = new MyInvalidCertificateHandler(false);
Context::Ptr ptrContext = new Context(Context::CLIENT_USE, "", "", certFilename, VERIFY_STRICT, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
SSLManager::instance().initializeClient(0, ptrCert, ptrContext);
感謝您的澄清。 無論如何,我找到了解決方案。 這是一個測試者。 而且您可以在這里很好-https: //github.com/pocoproject/poco/tree/develop/NetSSL_OpenSSL/testsuite但是另一個問題是我不知道它是如何工作的。 例如,我認為pem文件用於認證,但我不知道該怎么做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.