PDA

View Full Version : QNetworkAccessManager or QHttp help required



prasad.borkar
17th April 2011, 13:08
Hi,

I want to write a code which will connect to particular site and then pass username
and password and then login to that site.
I want to perform user authentication using either QNetworkAccessManager or QHttp or
any other way in Qt.
If it is possible then please help me out...

Thanks in advance...

ChrisW67
17th April 2011, 23:05
Yes it is possible.

QNetworkAccessManager, QNetworkRequest and the Network Examples (http://doc.qt.nokia.com/latest/examples-network.html) should be where you start.

You will also need to understand how web site authentication is done, in general and specifically.

prasad.borkar
19th April 2011, 11:30
Hi,

Thanks for the reply. I am looking to those examples.
I have written below code in which i am able to send request to the given site and
receive response as well. I am receiving response as html source of the given site
but i am not able to login to it.
Please correct me where I am doing mistake.


Code:

#include "mainwindow.h"

#include <QDebug>
#include <QThread>
#include <QtCore/QUrl>
#include <QtGui/QLabel>
#include <QtGui/QProgressBar>
#include <QtGui/QVBoxLayout>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkProxy>
#include <QtXml/QDomDocument>

static const char *REQUEST_URL = "http://192.16.1.10:8080/signIn.do";
static const char *userName = "abc";
static const char *password = "123";
static const char *submit = "submit";

MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout(this);

m_label = new QLabel();
m_progress = new QProgressBar();
m_progress->setRange(0, 100);

layout->addWidget(m_label);
layout->addWidget(m_progress);
setLayout(layout);

m_network = new QNetworkAccessManager(this);
QNetworkRequest request;
request.setRawHeader("Authorization", "Basic " +
QByteArray(QString("%1:%2:%3").arg(userName).arg(password).arg(submit).toAscii( )).toBase64());
request.setUrl(QUrl(REQUEST_URL));
QString payload = "reports.do?repCatg=ETE";
QByteArray arr = payload.toLatin1();

QNetworkReply *reply = m_network->post(request,arr);

QObject::connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
SLOT(slotSetProgress(qint64,qint64)));
QObject::connect(m_network, SIGNAL(finished(QNetworkReply *)),
SLOT(slotRequestFinished(QNetworkReply *)));
}

MainWindow::~MainWindow()
{
}

void MainWindow::slotRequestFinished(QNetworkReply *reply)
{
m_progress->setValue(0);
qDebug() << "Inside slotRequestFinished";

if (reply->error() > 0) {
m_label->setText("Error number = " + reply->errorString());
}
else {

QByteArray data = reply->readAll();
qDebug() << "Data Size:" << data.size();
QString str = data;
qDebug() << str.toAscii();
QDomDocument doc;
doc.setContent(data);
QDomNodeList nodes = doc.elementsByTagName("message");
if (nodes.size() > 0) {
m_label->setText(nodes.at(0).toElement().text());
}
else {
qDebug() << "Inside else nodes.size=0";
}
}
}

void MainWindow::slotSetProgress(qint64 received, qint64 total)
{
if (total > 0 && received > 0) {
m_progress->setValue((int) total / received);
}
}

ChrisW67
20th April 2011, 07:30
Edit your post and put your code into
... tags, you'll make it easier to read and copy.

If your server is using the Basic authentication scheme (and it is entirely possibly it isn't) then you need to send the credentials correctly. There are only two parts to the credentials: user name and password. I have no idea what you are trying to do with "submit".
http://en.wikipedia.org/wiki/Basic_access_authentication

If your web application is managing its own session and credentials then you will most likely have to POST a user name and password to get a session established.

Your payload also looks unusual but I am not familiar with your web application.