QWebView / Qt WebKit won't open some SSL pages; redirects not allowed?

后端 未结 2 628
Happy的楠姐
Happy的楠姐 2021-02-19 17:59

Clean install of Qt SDK 1.1.4 on Windows 7 with Visual C++ 2008 SP1; I\'m using Qt Creator. Why does this code not load some web pages?

#include 

        
2条回答
  •  渐次进展
    2021-02-19 18:42

    You are probably getting SSL errors which you can handle in a slot. Although not the best final solution, you can use the slot to ignore all SSL errors. I did this by subclassing QWebView:

    qwebview.h:

    #ifndef WEBVIEW_H
    #define WEBVIEW_H
    
    #include 
    
    class WebView : public QWebView
    {
        Q_OBJECT
    
        public:
            WebView(QWidget *parent = 0);
        private slots:
            void handleSslErrors(QNetworkReply* reply, const QList &errors);
    };
    
    #endif // WEBVIEW_H
    

    qwebview.cpp:

    #include "webview.h"
    #include 
    #include 
    #include 
    
    WebView::WebView(QWidget *parent) :
        QWebView(parent)
    {
        load(QUrl("https://gmail.com"));
    
        connect(page()->networkAccessManager(),
                SIGNAL(sslErrors(QNetworkReply*, const QList & )),
                this,
                SLOT(handleSslErrors(QNetworkReply*, const QList & )));  
    }
    
    void WebView::handleSslErrors(QNetworkReply* reply, const QList &errors)
    {
        qDebug() << "handleSslErrors: ";
        foreach (QSslError e, errors)
        {
            qDebug() << "ssl error: " << e;
        }
    
        reply->ignoreSslErrors();
    }
    

    main.cpp"

    #include 
    #include "WebView.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        WebView w;
        w.show();
        return a.exec();
    }
    

    Running this should produce debug output like this:

    handleSslErrors:  
    ssl error:  "The host name did not match any of the valid hosts for this certificate" 
    ssl error:  "No error" 
    ssl error:  "No error" 
    ...
    

    In your final program, you will of course want to handle SSL errors properly :)

提交回复
热议问题