允许导航到需要证书QML的站点

时间:2012-08-21 18:43:57

标签: c++ qt qml

我在我的应用程序qml中添加了一个Web浏览器,但我无法连接到需要证书的网站。 因为当我添加网站时,我获得了一个白页。 请帮帮我。

谢谢。

1 个答案:

答案 0 :(得分:2)

要与使用不受信任证书的网站进行通信,您应该将自己的NetworkManagerFactory提供给QDeclarativeEngine,如下所示。此外,这项技术将帮助您监控在任何其他情况下网络请求出现的问题。

class WebViewNetworkManagerFactory
  : public QObject
  , public QDeclarativeNetworkAccessManagerFactory
{
  Q_OBJECT

public:
  explicit WebViewNetworkManagerFactory(QObject *parent = 0)
    : QObject(parent)
  {}

public: // QDeclarativeNetworkAccessManagerFactory
  QNetworkAccessManager *create(QObject *parent) {
    QNetworkAccessManager *networkAccessManager = new QNetworkAccessManager(parent);
    connect(networkAccessManager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
            this, SLOT(onSslErrors(QNetworkReply*,QList<QSslError>)));
    connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(onReplyFinished(QNetworkReply*)));
    return networkAccessManager;
  }

private slots:
  void onSslErrors(QNetworkReply *reply, const QList<QSslError> &errors) {
    qDebug("onSslErrors");
    reply->ignoreSslErrors(errors);
  }

  void onReplyFinished(QNetworkReply *reply) {
    Q_ASSERT(reply); if (!reply) return;
    if (reply->error() == QNetworkReply::NoError) {
      return;
    }
    const int httpStatusCode = reply->attribute(
      QNetworkRequest::HttpStatusCodeAttribute).toInt();
      qDebug(QString("Network error = %1, HTTP code = %2, error description = '%3'")
               .arg(reply->error())
               .arg(httpStatusCode)
               .arg(reply->errorString())
               .toAscii());
  }
};

在代码中的某处使用,您可以在其中创建根DeclarativeView:

QDeclarativeView *buildRootView() {
  QScopedPointer<QDeclarativeView> view(new QDeclarativeView());
  QObject::connect(view->engine(), SIGNAL(quit()),
                   view.data(), SLOT(close()));
  view->engine()->setNetworkAccessManagerFactory(
    new WebViewNetworkManagerFactory(view.data()));
  // other initialization stuff...
  return view.take();
}