Networking

In Cascades, networking APIs form part of the QtNetwork module. This module exposes a mix of low-level and high-level APIs used for sending and receiving data over a network. To use the networking APIs, include the following library reference in your app .pro file:

QT += network

The QNetworkAccessManager class provides the functionality to send and receive data over a network. The image shown below demonstrates the relationship between the QNetworkAccessManager, and the QNetworkRequest and QNetworkReply classes.

A flow diagram showing that data flows from QNetworkRequest to QNetworkAccessManager and then to QNetworkReply.

QNetworkRequest represents an HTTP request. At a minimum, it must contain a URL for the destination. The request can also contain HTTP headers and SSL options.

Required and option attributes of a QNetworkRequest.

QNetworkAccessManager receives network requests, and sends them over the network using one of its HTTP request functions (get(), post(), put(), and so forth) which returns a pointer to its QNetworkReply. These functions are all asynchronous in nature, so they do not block your main application thread.

QNetworkAccessManager also contains functionality for cookie handling, caching, and proxy handling.

The methods and attributes of a QNetworkAccessManager.

QNetworkReply represents an HTTP reply. Among other information, it contains reply data from the destination URL, HTTP headers consisting of the status code, and any errors. Data in a QNetworkReply is reported asynchronously, so you can connect to its signals and manage information as it arrives.

When new data arrives over the network, the readyRead() event is triggered to indicate that the data is ready for reading. If you want to parse data as it arrives, you should subscribe to this signal. The readyRead() signal can be used even if you're not finished with the reply. When an HTTP request is complete, the finished() signal is triggered to indicate this event to your app.

The QNetworkReply API.

Here's an example that shows you how to send an HTTP GET request and retrieve the network reply. The first step is to create the network request and set the destination URL as a QUrl object.

// Creates the network request and sets the destination URL.
QNetworkRequest request = QNetworkRequest();
request.setUrl(QUrl("http://www.blackberry.com"));

Next, you create the network access manager, connect a custom slot to handle its finished() signal, and send the HTTP GET request.

// Create the network access manager 
QNetworkAccessManager* m_pNetAccessMngr = 
                                     new QNetworkAccessManager(this);

// Connect your custom slot to its finished signal
// Check the return value for connection errors
bool res;
Q_UNUSED(res);

res = connect(m_pNetAccessMngr, 
              SIGNAL(finished(QNetworkReply*)),
              this,
              SLOT(onRequestFinished()));
Q_ASSERT(res);

// Use the QNetworkAccessManager object to
// send the HTTP request and capture the 
// reply
m_pNetReply = m_pNetAccessMngr->get(request);

As you can see in the code sample shown above, we use a function called onRequestFinished() to handle the finished signal. The onRequestFinished() slot, shown below, is used to process the network reply data when the request is finished. In onRequestFinished(), when we've finished processing the network reply, we call deleteLater() to delete the reply object and free up system resources.

Within our app's header file, we create a member variable called m_pNetReply to hold the network reply. Then we use m_pNetReply for various purposes, such as updating our UI, and retrieving the raw headers. Here's sample code that shows you how we handle the network reply by using our custom onRequestFinished() slot.

void ApplicationUI::onRequestFinished ()
{
    // Check for errors and verify that data is available
    if (m_pNetReply != NULL &&
        m_pNetReply->bytesAvailable() > 0 &&
        m_pNetReply->error() == QNetworkReply::NoError)
    {
        // ...
        
        // Use the data in the m_pNetReply member variable
        // to update other components in your UI
        openFile();

        // Use the reply to set a raw headers dialog box
        QString rawHdrInfo = getRawHeaderInfo(m_pNetReply);
        m_pRawHeaderInfoTxa->setText(rawHdrInfo);

        // ...
          
        m_pNetReply->deleteLater();
    }
    else
    {
        // Handle errors here
    }
}

In the above code sample, we use a getRawHeaderInfo() function to retrieve the raw headers from the m_pNetReply member variable that we set when we made our request to the server. Here's the implementation code for the getRawHeaderInfo() function.

// This function collects the raw headers and returns them as a
// QString object
QString ApplicationUI::getRawHeaderInfo ( QNetworkReply* reply )
{
    QList <QByteArray> rawHeaderList = reply->rawHeaderList();
    QString rawHdrs;

    foreach(QByteArray hdrName, rawHeaderList)
    {
        QByteArray hdrValue = reply->rawHeader(hdrName);
        rawHdrs += hdrName;
        rawHdrs += " : ";
        rawHdrs += hdrValue;
        rawHdrs += "\n";
    }

    return rawHdrs;
}

Related resources

Last modified: 2014-04-03



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus