Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't use QNetworkAccessManager to login to a website

Can't use QNetworkAccessManager to login to a website

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • cocoC Offline
    cocoC Offline
    coco
    wrote on last edited by coco
    #1

    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);
            manager->get(newReq);
    
        }
        else if((statusCode == 403) || (statusCode == 404))
        {
            qDebug() << reply->url().toString();
            qDebug() << reply->error();
            qDebug() << reply->errorString();
    
        }
        else
        {
            qDebug() << "Done";
        }
    
        reply->deleteLater();
    }
    
    

    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.

    1 Reply Last reply
    1
    • cocoC Offline
      cocoC Offline
      coco
      wrote on last edited by
      #2

      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!

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved