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. qnetworkaccessmanager behind proxy
QtWS25 Last Chance

qnetworkaccessmanager behind proxy

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 2.2k Views
  • 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.
  • M Offline
    M Offline
    mgreenish
    wrote on last edited by
    #1

    My application exchanges data with a database via a web server script using the QNetworkAccessManager class. The application works well from our offices and it passed all testing including communication to the remote web server. I deployed it to a customer location and the customer sits behind a proxy; the communication failed with "proxy authentication required".

    In reaching, it seems that coders that user QWebView resolved this issue simply by setting

    QNetworkProxyFactory::setUseSystemConfiguration( true );
    

    in their main.

    I found one reference to someone using QNetworkAccessManager who had more trouble. He had to add a slot to the 'authentication required' signal of the QNetworkAccessManager object, upon entering the slot, get an object to the system proxy, and then pass the username and password to the proxy at that specific time.

    I found one reference to someone usig QNetworkAccessManager who only used the setUseSystemConfiguration( true ) and that was enough.

    We don't have a proxy and our IT seemingly doesn't have enough time to set one up for me to run a test. So I will have to deploy to the customer without testing, not great. I'd like to keep the iterations to a minimum.

    Can someone confirm if setting UseSystemConfiguration to 'true' should be enough or if I need to catch 'authentication required' signals and pass the username and password to the QNetworkAccessManager object?

    1 Reply Last reply
    0
    • Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by Pablo J. Rogina
      #2

      @mgreenish it looks like you need both, setting to use the system configuration and a slot to grab and apply credentials if needed

      ...
              if (useSystemProxy) {
                  QNetworkProxyFactory::setUseSystemConfiguration(true);
                  connect(&manager, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)),
                          this, SLOT(requestProxyCredentials(const QNetworkProxy&, QAuthenticator*)));
              }
      ...
      
      

      so if the proxy requires authentication, you'll need to provide credentials

      requestProxyCredentials(const QNetworkProxy& proxy, QAuthenticator* authenticator)
              // open dialog for user entering proper credentials
              ....
              // apply credentials 
              authenticator->setUser(user);
              authenticator->setPassword(pswd);
      

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      M 1 Reply Last reply
      0
      • Pablo J. RoginaP Pablo J. Rogina

        @mgreenish it looks like you need both, setting to use the system configuration and a slot to grab and apply credentials if needed

        ...
                if (useSystemProxy) {
                    QNetworkProxyFactory::setUseSystemConfiguration(true);
                    connect(&manager, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)),
                            this, SLOT(requestProxyCredentials(const QNetworkProxy&, QAuthenticator*)));
                }
        ...
        
        

        so if the proxy requires authentication, you'll need to provide credentials

        requestProxyCredentials(const QNetworkProxy& proxy, QAuthenticator* authenticator)
                // open dialog for user entering proper credentials
                ....
                // apply credentials 
                authenticator->setUser(user);
                authenticator->setPassword(pswd);
        
        M Offline
        M Offline
        mgreenish
        wrote on last edited by
        #3

        @Pablo-J.-Rogina Thanks for the feedback, so I may have to do both. That's fine, as long as I know :-).
        However, I understood that if the application uses the system configuration , setUseSystemConfiguration(true), that the application grabs the proxy server's credentials from the OS and passes them along to the proxy, no? I didn't think the user would have to re-enter the proxy information (address, port, username & password) again.

        1 Reply Last reply
        0
        • Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #4

          @mgreenish said in qnetworkaccessmanager behind proxy:

          I didn't think the user would have to re-enter the proxy information (address, port, username & password) again.

          You may have the edge case of your Qt application being the first trying to go through the proxy just after credentials expired. So you might need to consider just providing a way in your Qt application to input new credentials or letting the user now that he/she needs to update the system wide proxy credentials.

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • M Offline
            M Offline
            mgreenish
            wrote on last edited by mgreenish
            #5

            So I set the flag to true in the main

            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
                QNetworkProxyFactory::setUseSystemConfiguration( true );
            

            Then in the network request I put the following:

                QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
                connect( networkManager, SIGNAL( finished(QNetworkReply*) ),
                        this, SLOT(finishedRequest(QNetworkReply*)) );
                connect( networkManager, &QNetworkAccessManager::proxyAuthenticationRequired, \
                    [this,networkManager]()
                    {
                        disconnect( networkManager, SIGNAL( finished(QNetworkReply*) ),
                            this, SLOT(finishedRequest(QNetworkReply*)) );
                        networkManager->deleteLater();
                        QMessageBox *msgBox = new QMessageBox();
                        msgBox->setText( "Your computer resides behind a network proxy. Please configure the system proxy and try again." );
                        msgBox->setStandardButtons( QMessageBox::Ok );
                        msgBox->exec();
                    }
                );
            

            and sure enough, my customer informed me that he received the dialog box. So, it seems that the application either did not take the proxy settings from Windows or they aren't configured in Windows, right? That would seem odd to me. Internet access has been verified through IE and Firefox.

            Any ideas on what could be happening or on how to debug this? Is there a way to check that the system proxy settings have been integrated into the network request, to check what they are?

            M 1 Reply Last reply
            0
            • M mgreenish

              So I set the flag to true in the main

              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
                  QNetworkProxyFactory::setUseSystemConfiguration( true );
              

              Then in the network request I put the following:

                  QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
                  connect( networkManager, SIGNAL( finished(QNetworkReply*) ),
                          this, SLOT(finishedRequest(QNetworkReply*)) );
                  connect( networkManager, &QNetworkAccessManager::proxyAuthenticationRequired, \
                      [this,networkManager]()
                      {
                          disconnect( networkManager, SIGNAL( finished(QNetworkReply*) ),
                              this, SLOT(finishedRequest(QNetworkReply*)) );
                          networkManager->deleteLater();
                          QMessageBox *msgBox = new QMessageBox();
                          msgBox->setText( "Your computer resides behind a network proxy. Please configure the system proxy and try again." );
                          msgBox->setStandardButtons( QMessageBox::Ok );
                          msgBox->exec();
                      }
                  );
              

              and sure enough, my customer informed me that he received the dialog box. So, it seems that the application either did not take the proxy settings from Windows or they aren't configured in Windows, right? That would seem odd to me. Internet access has been verified through IE and Firefox.

              Any ideas on what could be happening or on how to debug this? Is there a way to check that the system proxy settings have been integrated into the network request, to check what they are?

              M Offline
              M Offline
              mgreenish
              wrote on last edited by
              #6

              @Pablo: It seems that the customer is using some proxy server that doesn't play nice with others. As such, I have to have bring up a dialog box where the user enters their user name and password for the proxy. So it was a good tip to be prepared for the "corner" case!!

              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