Question

we are using QtWebKit 4.7 and want to know when a frame load does a redirect.

At the moment we are counting the outgoing requests within a subclass of the QNetworkAccessManager, where we do overwrite createRequest.

This works in most cases fine, but when the first response is 301 or 302 (redirect), it is swallowed somewhere.

We simply request a url the following way:

QNetworkRequest request(QUrl("http://www.twitter.com")); // 301 to twitter.com
frame->load(request);
Was it helpful?

Solution

Use the void QWebFrame::urlChanged ( const QUrl & url ) signal to detect url changes, i.e. redirects, i.e.

QNetworkRequest request(QUrl("http://www.twitter.com")); // 301 to twitter.com
connect(frame, SIGNAL(urlChanged (const QUrl&)), this, SLOT(onRedirected(const QUrl&));
frame->load(request);

OTHER TIPS

Handle the QNetworkReply yourself, get the status code from the reply and do a QWebFrame::setcontent.

QNetworkRequest request(QUrl("http://www.twitter.com")); // 301 to twitter.com
connect (frame->page()->networkAccessManager(), SIGNAL(finished(QNetworkReply*), this, SLOT(onFinished(QNetworkReply*));
frame->page()->networkAccessManager()->get(request);

[...]

void onFinished(QNetworkReply* reply)
{
    if (reply->error() == QNetworkReply::NoError) {
        int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
        switch (statusCode) {
            case 301:
            case 302:
            case 307:
                qDebug() << "redirected: " << reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
                break;

            case 200:
                frame->setContent(reply->readAll());
                break;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top