Results 1 to 4 of 4

Thread: QNetworkAccessManager or QHttp help required

  1. #1
    Join Date
    Mar 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Cool QNetworkAccessManager or QHttp help required

    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...

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QNetworkAccessManager or QHttp help required

    Yes it is possible.

    QNetworkAccessManager, QNetworkRequest and the Network Examples should be where you start.

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

  3. #3
    Join Date
    Mar 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Cool Re: QNetworkAccessManager or QHttp help required

    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(p assword).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);
    }
    }

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QNetworkAccessManager or QHttp help required

    Edit your post and put your code into [code] ... [/code] 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_a...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.

Similar Threads

  1. Using QHttp for authentication required
    By anoop7181 in forum Qt Programming
    Replies: 4
    Last Post: 18th April 2011, 11:25
  2. QNetworkAccessManager and QHttp doesn't sends anything
    By corrado in forum Qt Programming
    Replies: 2
    Last Post: 29th May 2010, 22:20
  3. QNetworkAccessManager or QHttp
    By mind_freak in forum Qt Programming
    Replies: 3
    Last Post: 29th September 2009, 20:24
  4. QNetworkAccessManager vs QHttp
    By jiveaxe in forum Newbie
    Replies: 3
    Last Post: 17th February 2009, 14:07
  5. Replies: 11
    Last Post: 20th January 2009, 14:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.