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. Unable to login using qtnetworkaccessmanager , but able to login using postman tool
Forum Updated to NodeBB v4.3 + New Features

Unable to login using qtnetworkaccessmanager , but able to login using postman tool

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 299 Views 2 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.
  • Q Offline
    Q Offline
    QTDevPal
    wrote on last edited by VRonin
    #1

    HI,
    I am using qnetworkaccessmanager to login to a website, i am setting user and password in rowHeader but getting empty response in code, but the same request with user and password set in header works properly using Postman tool, can anyone suggest what could be the issue ?

    QString user = "user";
    	QString pass = "testPassword";
    	QByteArray data1 = user.toLocal8Bit().toBase64();
    	QByteArray data2 = pass.toLocal8Bit().toBase64();
    	QNetworkRequest request = QNetworkRequest(QUrl("http://server_address/b1/login"));
    
    	request.setRawHeader("user", data1);
    	request.setRawHeader("password", data2);
    
    	QNetworkReply *reply = manager->get(request);
    

    and i am fetching reply using below code -

    manager = new QNetworkAccessManager();
    	QObject::connect(manager, &QNetworkAccessManager::finished,
    		this, [=](QNetworkReply *reply) {
    		if (reply->error()) {
    			qDebug() << reply->errorString();
    			return;
    		}
    
    		QString answer = QString::fromUtf8(reply->readAll());
    		qDebug() << answer;
    
    		bool isFinished = reply->isFinished();
    		bool isRunning = reply->isRunning();
    		QNetworkReply::NetworkError err = reply->error();
    
    
    		QByteArray bts = reply->readAll();
    		QString str(bts);
    		qDebug() << str;
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      You can try to NOT set
      request.setRawHeader("user", data1);
      request.setRawHeader("password", data2);

      and then use the signal +QAuthenticator

       QObject::connect(&netMan,&QNetworkAccessManager::authenticationRequired,repl,[repl](QNetworkReply *reply, QAuthenticator *authenticator)->void{
              if(reply!=repl)
                  return;
              authenticator->setUser("MyUserName");
              authenticator->setPassword("MyPassword");
          });
      

      and see if that makes a difference.

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        QTDevPal
        wrote on last edited by
        #3

        QNetworkReply *reply = manager->get(QNetworkRequest(tempURL));
        QObject::connect(manager, &QNetworkAccessManager::authenticationRequired, reply, [reply](QNetworkReply *repl, QAuthenticator *authenticator)->void {
        // if (repl != reply)
        // return;
        authenticator->setUser("USER");
        authenticator->setPassword("Password");
        });

        Tried this, but it is not going inside slot for setting user and password.
        Am I doing something wrong?

        mrjjM 1 Reply Last reply
        0
        • Q QTDevPal

          QNetworkReply *reply = manager->get(QNetworkRequest(tempURL));
          QObject::connect(manager, &QNetworkAccessManager::authenticationRequired, reply, [reply](QNetworkReply *repl, QAuthenticator *authenticator)->void {
          // if (repl != reply)
          // return;
          authenticator->setUser("USER");
          authenticator->setPassword("Password");
          });

          Tried this, but it is not going inside slot for setting user and password.
          Am I doing something wrong?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @QTDevPal
          It looks correct.
          Maybe it only triggers with HTTPS links.

          Since you use QNetworkAccessManager::finished signal, we can rule out the normal suspects that event loop is not running etc.

          It was worth a shot :) Give it some more hours and maybe someone has an idea of why setRawHeader seems not to work.
          (it should as far as i know)

          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