QWebView / Qt WebKit不会打开一些SSL页面;重定向不允许? [英] QWebView / Qt WebKit won't open some SSL pages; redirects not allowed?

查看:1369
本文介绍了QWebView / Qt WebKit不会打开一些SSL页面;重定向不允许?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Visual C ++ 2008 SP1在Windows 7上清洁安装Qt SDK 1.1.4;我使用Qt Creator。为什么此代码不加载某些网页?

  #include< QtGui / QApplication> 
#include< QtWebKit / QWebView>

int main(int argc,char * argv [])
{
QApplication a(argc,argv);
QWebView b;
b.load(QUrl(https://gmail.com)); // does not work
//b.load(QUrl(\"https://accounts.google.com)); // works
//b.load(QUrl( \"https://google.com)); // does not work
//b.load(QUrl(\"https://www.google.com)); // works
b.show();

return a.exec();
}

为什么某些网址无效, >

我认为google.com / www.google.com是特别有说服力的; google.com通常会重定向到www.google.com。 gmail.com正在重定向到accounts.google.com。 WebKit是否不允许安全页面重定向?如果是这样,如何解决这个问题?



顺便说一下,Qt SDK 1.1.4似乎包括OpenSSL;我注意到它在C:\ QtSDK\Desktop\Qt\4.7.4\ msvc2008\bin\ssleay32.dll的存在。



修改:还有两个网址:

  b.load(QUrl(https://support.motionview3d.com/help/_media/images/directory.png) ); // does not work 
b.load(QUrl(https://mail.google.com)); // works

同样,这两种方式在其他网络浏览器中都可以正常工作。

解决方案

您可能会收到SSL错误,您可以在插槽中处理。虽然不是最好的最终解决方案,您可以使用插槽来忽略所有SSL错误。我通过子类化 QWebView



qwebview.h:

  #ifndef WEBVIEW_H 
#define WEBVIEW_H

#include< QWebView>

class WebView:public QWebView
{
Q_OBJECT

public:
WebView(QWidget * parent = 0);
private slots:
void handleSslErrors(QNetworkReply * reply,const QList< QSslError>& errors);
};

#endif // WEBVIEW_H

qwebview.cpp: / strong>

  #includewebview.h
#include< QNetworkReply>
#include< QtDebug>
#include< QSslError>

WebView :: WebView(QWidget * parent):
QWebView(parent)
{
load(QUrl(https://gmail.com)) ;

connect(page() - > networkAccessManager(),
SIGNAL(sslErrors(QNetworkReply *,const QList< QSslError>&)),
this,
SLOT(handleSslErrors(QNetworkReply *,const QList< QSslError>&))));
}

void WebView :: handleSslErrors(QNetworkReply * reply,const QList< QSslError>& errors)
{
qDebug()< handleSslErrors:;
foreach(QSslError e,errors)
{
qDebug()< ssl error:< e;
}

reply-> ignoreSslErrors();
}

main.cpp b
$ b

  #include< QApplication> 
#includeWebView.h

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

运行这应该会产生如下的调试输出:

  handleSslErrors:
ssl错误:主机名与此证书的任何有效主机不匹配
ssl错误: 无错误
ssl错误:无错误
...

在您的最终程序中,您当然会正确处理SSL错误:)


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 <QtGui/QApplication>
#include <QtWebKit/QWebView>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWebView b;
    b.load(QUrl("https://gmail.com")); // doesn't work
    //b.load(QUrl("https://accounts.google.com")); // works
    //b.load(QUrl("https://google.com")); // doesn't work
    //b.load(QUrl("https://www.google.com")); // works
    b.show();

    return a.exec();
}

Why do some of the URLs not work, and others do?

I think the google.com / www.google.com is especially telling; google.com normally redirects to www.google.com. And gmail.com is redirecting to accounts.google.com. Is WebKit not allowing secure pages to redirect? If so, how to fix that?

By the way, Qt SDK 1.1.4 seems to include OpenSSL; I noticed its presence at C:\QtSDK\Desktop\Qt\4.7.4\msvc2008\bin\ssleay32.dll. Also notice that some pages seem to work, just not others.

EDIT: Two more URLs:

b.load(QUrl("https://support.motionview3d.com/help/_media/images/directory.png")); // doesn't work
b.load(QUrl("https://mail.google.com")); // works

Again, both of these work fine in other web browsers.

解决方案

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 <QWebView>

class WebView : public QWebView
{
    Q_OBJECT

    public:
        WebView(QWidget *parent = 0);
    private slots:
        void handleSslErrors(QNetworkReply* reply, const QList<QSslError> &errors);
};

#endif // WEBVIEW_H

qwebview.cpp:

#include "webview.h"
#include <QNetworkReply>
#include <QtDebug>
#include <QSslError>

WebView::WebView(QWidget *parent) :
    QWebView(parent)
{
    load(QUrl("https://gmail.com"));

    connect(page()->networkAccessManager(),
            SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError> & )),
            this,
            SLOT(handleSslErrors(QNetworkReply*, const QList<QSslError> & )));  
}

void WebView::handleSslErrors(QNetworkReply* reply, const QList<QSslError> &errors)
{
    qDebug() << "handleSslErrors: ";
    foreach (QSslError e, errors)
    {
        qDebug() << "ssl error: " << e;
    }

    reply->ignoreSslErrors();
}

main.cpp"

#include <QApplication>
#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 :)

这篇关于QWebView / Qt WebKit不会打开一些SSL页面;重定向不允许?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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