Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get first redirect (301 or 302) event in QtWebKit

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);
like image 761
Dag Avatar asked Mar 01 '12 10:03

Dag


1 Answers

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;
        }
    }
}
like image 181
Chris Browet Avatar answered Sep 19 '22 13:09

Chris Browet