How to do HTTP Get and Post using Qt
Article Metadata
Code Example
Source file: Media:HttpGet&Post.zip
Tested with
Devices(s): Nokia 5800 XpressMusic,Nokia N97, Nokia E72, Nokia N8
Compatibility
Platform(s): Qt (tested Qt 4.7 Symbian^3)
Article
Keywords: QNetworkAccessManager,QNetworkReply,QNetworkRequest,QUrl
Created: gaba88
(25 Mar 2010)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
Overview
This article is a basic QWidget application which performs HTTP GET and POST for a user specified URL.
This is a very basic article this can be extended to suite the developers need. Note that it can only be used in portrait mode - it does not scale properly when rotated.
Prerequisite
- Install the Qt SDK
capability required
- NetworkServices
UI design (.ui file)
The below screenshot shows the ui design file.
Header File
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QMessageBox>
#include<QByteArray>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void changeEvent(QEvent *e);
private slots:
void finished(QNetworkReply *reply);
void DoHttpGet();
void activateGetWidgets();
void HideWidgets();
void clearWidgets();
void activatePostWidgets();
private:
Ui::MainWindow *ui;
QNetworkAccessManager *nam;
};
#endif // MAINWINDOW_H
Source File
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
nam = new QNetworkAccessManager(this);
HideWidgets();
connect(ui->getButton,SIGNAL(clicked()),this,SLOT(activateGetWidgets()));
connect(ui->submitButton,SIGNAL(clicked()),this,SLOT(DoHttpGet()));
connect(ui->resetButton,SIGNAL(clicked()),this,SLOT(clearWidgets()));
connect(nam,SIGNAL(finished(QNetworkReply*)),this,SLOT(finished(QNetworkReply*)));
connect(ui->postButton,SIGNAL(clicked()),this,SLOT(activatePostWidgets()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::activateGetWidgets()
{
ui->urlLabel->setHidden(false);
ui->urlLine->setHidden(false);
ui->submitButton->setHidden(false);
ui->textBrowser->setHidden(false);
ui->responseTitleLabel->setHidden(false);
ui->getButton->setHidden(true);
ui->postButton->setHidden(true);
}
void MainWindow::activatePostWidgets()
{
ui->dataLabel->setHidden(false);
ui->dataLine->setHidden(false);
activateGetWidgets();
}
void MainWindow::finished(QNetworkReply *reply)
{
if(reply->error() == QNetworkReply::NoError)
{
ui->textBrowser->setText(reply->readAll());
}
else
{
ui->textBrowser->setText(reply->errorString());
}
}
void MainWindow::DoHttpGet()
{
ui->resetButton->setHidden(false);
QString url = ui->urlLine->text();
QString data = ui->dataLine->text();
QByteArray postData;
postData.append(data.toAscii());
if(postData.isEmpty() == true)
{
nam->get(QNetworkRequest(QUrl(url)));
}
else
{
nam->post(QNetworkRequest(QUrl(url)),postData);
}
}
void MainWindow::HideWidgets()
{
ui->urlLabel->setHidden(true);
ui->urlLine->setHidden(true);
ui->dataLabel->setHidden(true);
ui->dataLine->setHidden(true);
ui->submitButton->setHidden(true);
ui->responseTitleLabel->setHidden(true);
ui->textBrowser->setHidden(true);
ui->resetButton->setHidden(true);
}
void MainWindow::clearWidgets()
{
ui->urlLabel->setHidden(true);
ui->urlLine->setHidden(true);
ui->dataLabel->setHidden(true);
ui->dataLine->setHidden(true);
ui->submitButton->setHidden(true);
ui->responseTitleLabel->setHidden(true);
ui->textBrowser->setHidden(true);
ui->resetButton->setHidden(true);
ui->urlLine->clear();
ui->textBrowser->clear();
ui->dataLine->clear();
ui->getButton->setHidden(false);
ui->postButton->setHidden(false);
}
Output Screenshots
- Initial Screen Asking the user to select and http get or post.
- Screenshot after user has clicked the get button.
- Screenshot after user has clicked the post button.
Sample Application
- A sample Qt Application can be downloaded from this File:HttpGet&Post.zip
Reference Links
- QNetworkAccessManager
- See also HTTP Example





The example code returns always the string "Socket operation timed out" on Nokia C5.00 (both firmware 032.010 and firmware 061.005) if I perform a GET operation to http://www.developer.nokia.com/ , for example. Nevertheless I can browse this page using 3G network and Nokia browser without any problem.
What can be the matter?
I've compiled and deployed the example code both for Qt 4.6.3 and for Qt 4.7.3, the result is the same above in both the scenarios.
Hi Please finish your messages with the following text as this will add a signature and separate posts from responses!
I just tried this on Nokia N8 (Symbian^3) and it works fine. I believe that the code itself is fine. It does appear to use the default network point - do you have a default network defined? Is it using the same network as you use when you browse via internet? Have you got the same version of Qt on your phone as you built this with? Also probably a good question to raise on the forums.
hamishwillee 02:21, 17 May 2011 (EEST)