qnetworkaccessmanager behind proxy



  • 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?



  • @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);
    


  • @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.



  • @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.



  • 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?



  • @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!!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.