这是使用 QWebPage 设置 SSL 协议的正确方法吗? [英] Is this the right way to set the SSL protocol with QWebPage?

查看:49
本文介绍了这是使用 QWebPage 设置 SSL 协议的正确方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在 Qt 中使用 SSL,我需要在其中设置特定的协议(而不是默认的安全协议").看起来像这样:

I've been working with SSL in Qt, where I need to set a specific protocol (instead of the default "secure protocols"). It looks like this works:

QSslConfiguration config = QSslConfiguration::defaultConfiguration();
config.setProtocol(QSsl::TlsV1_0);
QSslConfiguration::setDefaultConfiguration(config);

但是像这样以全局方式设置协议让我感到不舒服,而不是在 QWebPageQWebView 或其他东西上设置它.我是否遗漏了一些明显的东西,或者这真的是最好的方法吗?我知道我可以在 SSL 套接字上设置它,但我使用的是 QtWebKit,并且无法访问各个套接字.

But it makes me uncomfortable to set the protocol in a global way like this, instead of setting it on the QWebPage or QWebView or something. Am I missing something obvious or is this really the best way to do this? I know I can set it on an SSL socket, but I'm using QtWebKit, and don't have access to the individual sockets.

推荐答案

我发现这样做的方法是扩展 QNetworkAccessManager 并在 createRequest:

The way I've found to do this is to extend QNetworkAccessManager and set the protocol in createRequest:

class NetworkAccessManager : public QNetworkAccessManager
{
    Q_OBJECT
public:
    explicit NetworkAccessManager(QObject *parent = 0);

protected:
    virtual QNetworkReply * createRequest(Operation operation, const QNetworkRequest & request, QIODevice * outgoingData = 0) {
        // I have no idea why request is const, but I need to change it
        QNetworkRequest notConstRequest = request;
        QSslConfiguration conf = notConstRequest.sslConfiguration();
        conf.setProtocol(QSsl::TlsV1_0);
        notConstRequest.setSslConfiguration(conf);
        return QNetworkAccessManager::createRequest(operation, notConstRequest, outgoingData);
    }
};

然后我可以使用 setNetworkAccessManager.

Then I can set it in my QWebpage using setNetworkAccessManager.

这篇关于这是使用 QWebPage 设置 SSL 协议的正确方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆