Creating an HTTP network request in Qt using QNetworkAccessManager, QNetworkRequest, QNetworkReply
This article demonstrates how to prepare a HTTP request and to make use of the response.
Article Metadata
Code Example
Tested with
Article
The example code demonstrates usage of QNetworkAccessManager, QNetworkRequest, QNetworkReply, QUrl and QSettings. HttpCommunicator and RawRequestUi are important classes which makes use of several Qt services to implement example application.
Contents |
UI for Entering the URL
RawRequestUi File:HttpConsole.zip class implements simple UI where user can enter URL such as http://beer.com RawRequestUi provides following user input elements
- Select type of request i.e. get, post, head, put or delete.
- Choose request body. Body can be taken from file or can be entered in text input field provided.
- Response area displays headers and response body received from HTTP request.
- Header area where user can insert HTTP header into the request.
Screen on Nokia 5800 using a Wifi Network
Screen on Nokia 5230 using a 3G Network
Creating Request
HttpCommunicator class makes use of QNetworkAccessManager, QNetworkRequest, QNetworkReply, QUrl to get its job done. QNetworkRequest::setUrl() is all you have to do to prepare a HTTP request. User entered headers are inserted using the API QNetworkRequest::setRawHeader().
Communicating with Server
QNetworkAccessManager encapsulates nitty-gritty of actual communication. QNetworkAccessManager has APIs such as get(), post(), deleteResource(), put(), head() to make HTTP request. HttpCommunicator hooks onto QNetworkAccessManager by providing slot for QNetworkAccessManager::finished().
Catching the Response
QNetworkReply makes the job easier by representing the network reply through simple and intuitive APIs. QNetworkAccessManager::get() and other request making functions returns pointer to QNetworkReply. Same pointer is returned in the parameter to QNetworkAccessManager::finished(). It is job of caller to delete QNetworkReply object but beware that you have to use QNetworkReply::deleteLater() if you want to delete the object inside the slot QNetworkAccessManager::finished(). QNetworkReply::rawHeaderList() is useful function which returns header list returned by server. RawRequestUi::processSngResponse() makes use of QNetworkReply::rawHeaderList(), QNetworkReply::rawHeader() to read HTTP reply headers.
Miscellaneous
Example code also makes use of QSettings to remember data entered in user input fields.
Example project
SISX File Media:HttpConsole template.sis




I've tried the example code on Nokia C5.00 (both firmware 032.010 and firmware 061.005) but the reply of a GET request to http://www.google.com.au/ using a 3G Network is always "Socket operation timed out".
What can be the matter?
I can browse Internet pages without problem using 3G Network and Nokia browser. I've compiled and deployed the example code for both Qt 4.6.3 and Qt 4.7.3, I get the same result above for both the cases.
polidorigi 13:34, 17 May 2011 (EEST)
Gurpal2000 - downloads incomplete files
on my nokia n8 this example seems to save an incomplete file. The md5sums of source and downloaded file are different!
-gurpal2000gurpal2000 00:07, 1 December 2012 (EET)
Hamishwillee - Downloads fine for me
I've also uploaded the SIS file separately. Suggest you try again.hamishwillee 07:21, 3 December 2012 (EET)