QNetworkReply and 302 Found code
-
Hi all,
I am developing an application that uses the Qt Webkit. I have this code :@
void SignalHandler::ddlogin()
{
Manager = new QNetworkAccessManager();
QObject::connect(Manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(finished_login(QNetworkReply *)));
QNetworkRequest request;
request.setUrl(QUrl("http://ddunlimited.net/ucp.php?mode=login"));
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/x-www-form-urlencoded"));
request.setRawHeader("User-Agent", "Firefox/3.0.10");
QUrl params;
params.addQueryItem("username", *(Config->get_user_forum()) );
params.addQueryItem("password", *(Config->get_pass_forum()));
params.addQueryItem("redirect","index.php");
params.addQueryItem("login", "Login");
params.encodedQuery();
QNetworkReply *test = Manager->post(request,params.encodedQuery());
}void SignalHandler::finished_login(QNetworkReply *reply)
{
QObject::disconnect(Manager, SIGNAL(finished(QNetworkReply *)),this,SLOT(finished_login(QNetworkReply *)));
QByteArray *temp = new QByteArray(reply->readAll());
QFile file("temp.html");
file.open(QIODevice::WriteOnly);
file.write(*temp);
file.close();
}
@This code should make the login in forums based on phpbb3. It works for several forums, but it doesn't work for a forum. The problem is that the reply->readAll() return an empty QByteArray. The same code works for others forum, and it returns the html page after the login.
I tried to debug the problem with a network analyzer and I saw that the server responds with the http response status code 302. It seems a sort of redirect, but I'm not sure. How can I handle this ? Is there a workaround ? Sorry for my english and sorry for my lack of experience.
Kind regards,
Stefano. -
Follow redirects. From "QNetworkReply doc:":http://qt-project.org/doc/qt-5.0/qtnetwork/qnetworkreply.html Note: When the HTTP protocol returns a redirect no error will be reported. You can check if there is a redirect with the QNetworkRequest::RedirectionTargetAttribute attribute.
-
example can be found "here":http://developer.nokia.com/Community/Wiki/Handling_an_HTTP_redirect_with_QNetworkAccessManager.
-
[quote author="raven-worx" date="1376389368"]example can be found "here":http://developer.nokia.com/Community/Wiki/Handling_an_HTTP_redirect_with_QNetworkAccessManager.[/quote]
I've already see the example but it's a little bit different from my case. To make a successfull login I have to use post request, after that I usually obtain a page and inside the html code I extract a parameter called "ssid". This "ssid" is used in the next request like an authentication token. I'm not sure that the example can help me bacause it uses a get request and without this "ssid" I'll never login.
I don't know if I was clear. Now I'm trying to join and adapt the example code, but I'm not sure that it will work. Thank for the help and thank for any reply.
Kind regards,
Stefano. -
the example just shows how to handle the redirect. It's independent if POST or GET.
If you detect a redirect (example) you do the same request like you did initially but only to the new redirected url. -
@raven-worx the provided link is invalid
-
@ahmed000001 Because it is 6 years old and Qt does not belong to Nokia anymore.
-
@ahmed000001 See https://doc.qt.io/archives/qt-5.10/qnetworkreply.html#redirected
Set QNetworkRequest::FollowRedirectsAttribute in your request, then you will get the above signal with new URL if your request is redirected.