×
Namespaces

Variants
Actions
(Difference between revisions)

Creating an HTTP network request in Qt using QNetworkAccessManager, QNetworkRequest, QNetworkReply

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Text replace - "Category:MeeGo" to "Category:MeeGo Harmattan")
hamishwillee (Talk | contribs)
m (Hamishwillee - Make SIS "bare file")
 
Line 1: Line 1:
[[Category:Qt]][[Category:MeeGo Harmattan]][[Category:Symbian]][[Category:Code Examples]][[Category:HTTP]][[Category:Networking]]
+
[[Category:Qt]][[Category:MeeGo Harmattan]][[Category:Symbian]][[Category:Code Examples]][[Category:HTTP]][[Category:Networking]][[Category:S60 5th Edition]]
 
{{Abstract|This article demonstrates how to prepare a '''HTTP''' request and to make use of the response.}}  
 
{{Abstract|This article demonstrates how to prepare a '''HTTP''' request and to make use of the response.}}  
  
 
{{ArticleMetaData <!-- v1.2 -->
 
{{ArticleMetaData <!-- v1.2 -->
|sourcecode= [[Media:HttpConsole.zip]] [[Media:HttpConsole template.zip]]  
+
|sourcecode= [[Media:HttpConsole.zip]]
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
+
|installfile= [[Media:HttpConsole template.sis]]
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
+
|devices= Nokia 5800, Nokia 5230
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) -->
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) -->
 
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
Line 58: Line 58:
 
[http://gitorious.org/http-console/http-console/trees/master Download from Gitorious.org]
 
[http://gitorious.org/http-console/http-console/trees/master Download from Gitorious.org]
  
SISX File [[File:HttpConsole template.zip]]
+
SISX File [[Media:HttpConsole template.sis]]

Latest revision as of 08:21, 3 December 2012

This article demonstrates how to prepare a HTTP request and to make use of the response.

Article Metadata
Code ExampleTested with
Devices(s): Nokia 5800, Nokia 5230
Compatibility
Dependencies: Qt 4.7
Article
Created: datha_k (24 May 2010)
Last edited: hamishwillee (03 Dec 2012)

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

[edit] 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.

[edit] Screen on Nokia 5800 using a Wifi Network

Scr000005.jpg

[edit] Screen on Nokia 5230 using a 3G Network

Scr000002.jpg

[edit] 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().

[edit] 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().

[edit] 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.

[edit] Miscellaneous

Example code also makes use of QSettings to remember data entered in user input fields.

[edit] Example project

Download from Gitorious.org

SISX File Media:HttpConsole template.sis

This page was last modified on 3 December 2012, at 08:21.
203 page views in the last 30 days.
×