Important: Please read the Qt Code of Conduct -

Can't use QNetworkAccessManager to login to a website

  • Hello,

    I am trying to login to a website using QNetworkAccessManager, QNetworkReply and QNetworkRequest. I got the status code 200 and nothing else. Did I miss something here?

    void LogFunc::doLogin()
        QUrl url("https://url");
        QNetworkRequest request(url);
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
        QString username = "user";
        QString password = "pass";
        QUrlQuery urlQuery;
        urlQuery.addQueryItem("username", username);
        urlQuery.addQueryItem("password", password);
        QString params = urlQuery.query();
        manager = new QNetworkAccessManager(this);
        connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
        manager->post(request, params.toUtf8());
    void LogFunc::replyFinished(QNetworkReply *reply)
        qDebug() << reply->readAll();
        int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
        QString statusName = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
        qDebug() << statusCode << statusName;
        if(statusCode == 302)
            QUrl newUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
            QNetworkRequest newReq(newUrl);
        else if((statusCode == 403) || (statusCode == 404))
            qDebug() << reply->url().toString();
            qDebug() << reply->error();
            qDebug() << reply->errorString();
            qDebug() << "Done";

    Thank you in advance.

    I got page content if I tried to add fault key for username or password. The page will say they need username or password to login (obviously). I also got something if I use GET but nothing with POST.

  • So I did miss something and not Qt related...

    Turns out the website also provides API for ease of use. I use the same code and API info provided by the particular website. To login, I need another login url and to get the data I want, I need to add another query then voila!

Log in to reply