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

we use QtWebKit 4.7 and want to know when the load on the frame does the redirection.

Currently, we are counting outgoing requests in a subclass of QNetworkAccessManager, where we are overwriting createRequest.

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

We simply request the URL as follows:

QNetworkRequest request(QUrl("http://www.twitter.com")); // 301 to twitter.com frame->load(request); 
+6
source share
2 answers

Use the void QWebFrame::urlChanged ( const QUrl & url ) signal to detect changes to the URL, 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); 
+3
source

Address QNetworkReply independently, receive a status code from the answer and execute 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; } } } 
+8
source

Source: https://habr.com/ru/post/909725/


All Articles