gpt4 book ai didi

c++ - 如何在 C++ 中使用 Poco 连接安全的 websocket

转载 作者:行者123 更新时间:2023-11-30 05:36:31 26 4
gpt4 key购买 nike

我正在尝试通过使用 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 异常。有人可以帮我吗?谢谢

最佳答案

在测试套件中,它是 test driverconfiguration file 为你做的.

至于手工怎么做,大概是这样的:

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);

关于c++ - 如何在 C++ 中使用 Poco 连接安全的 websocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33536725/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com