Home How to add HTTP authentication support in Qt applications – Nokia Developer Wiki

How to add HTTP authentication support in Qt applications – Nokia Developer Wiki

Adding HTTP authentication support to your Qt application

This article shows how to add HTTP authentication support in Qt application. In today’s mobile world, many of the mobile apps are heavy users of web services. Often you will create your web backed using a web framework, for example Pyramid or Django which will enable you to support HTTP basic authentication over SSL (useful if you want to enable your users to log in securely to their cloud account from your Qt app).
Getting authenticated against the web back end could not be easier using Qt’s QNetworkAccessManager.
This class is a higher level abstraction on top of network resources, allowing you to quite easily make your app network aware.
You get SSL support for free by just using an “https://” prefixed URL, given that the remote certificate is signed by a known CA.
If it is self signed you have to ask to ignore the error that is accompanied, to learn how to do so, this was found quite useful and a nice intro to Qt networking stack that now days mostly encourages using higher level abstractions than dealing with sockets and bytes.
When an authentication is required, a signal is emitted for you to connect to and fill the authentication credentials in your slot. This will in quick look even more simple once a glance over the example code is given. To make this “interactive” you just need to take your creds from some input fields.
And, here is the code. First we need to create a QNetworkAccessManager instance and connect to its signals, the important one here for authentication is the “authenticationRequired” signal, which is dispatched as soon as the servers asks for authentication to complete our request.

QNetworkAccessManager *manager = new QNetworkAccessManager();
connect(manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
            SLOT(provideAuthenication(QNetworkReply*,QAuthenticator*)));
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(httpReply(QNetworkReply*)));
QNetworkRequest req(QUrl("https://api.mydomain.tld/about"));
manager->get(req);

The slot code for filling the credentials for the authentication will look something like:

void MainWindow::provideAuthenication(QNetworkReply *reply, QAuthenticator *ator)
{
    qDebug() << reply->readAll(); // this is just to see what we received
    ator->setUser(QString("USERNAME"));
    ator->setPassword(QString("MYPASSWORD"));
}

 
Source Developer Nokia

About ReadWrite’s Editorial Process

The ReadWrite Editorial policy involves closely monitoring the gambling and blockchain industries for major developments, new product and brand launches, game releases and other newsworthy events. Editors assign relevant stories to in-house staff writers with expertise in each particular topic area. Before publication, articles go through a rigorous round of editing for accuracy, clarity, and to ensure adherence to ReadWrite's style guidelines.

Get the biggest iGaming headlines of the day delivered to your inbox

    By signing up, you agree to our Terms and Privacy Policy. Unsubscribe anytime.

    Gambling News

    Explore the latest in online gambling with our curated updates. We cut through the noise to deliver concise, relevant insights, keeping you informed about the ever-changing world of iGaming and its most important trends.

    In-Depth Strategy Guides

    Elevate your game with tailored strategies for sports betting, table games, slots, and poker. Learn how to maximize bonuses, refine your tactics, and boost your chances to beat the house.

    Unbiased Expert Reviews

    Honest and transparent reviews of sportsbooks, casinos and poker rooms crafted through industry expertise and in-depth analysis. Delve into intricacies, get the best bonus deals, and stay ahead with our trustworthy guides.