maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   Qt http post request (https://talk.maemo.org/showthread.php?t=71694)

XenGi 2011-04-01 06:43

Qt http post request
 
Hi,

I'm developing a Qt program for maemo that should do something similar to filling a html formular, send it via post and download the resulting page (in my case an xml file).

I found some code and stuff using google but I'm still a little bit confused about this.

Maybe anyone can give me a minimal example?

Lets say that is the page ("http://myserver.com/xml.php") that gives me the xml:

PHP Code:

<?php

if(checkuser($_POST['user'], $_POST['pass']))
{
    echo 
"<?xml ...";
}
else
{
    echo 
"<p>sorry, wrong auth.</p>";
}

?>


Lullen 2011-04-01 07:04

Re: Qt http post request
 
I have these two functions for HTTP-get and HTTP-post
void doHttpGet(QString url)
{
nam->get(QNetworkRequest(QUrl(url)));
}

void doHttpPost(QString url, QString data)
{
QByteArray postData;
postData.append(data.toAscii());
nam->post(QNetworkRequest(QUrl(url)),postData);
}

in the constructor I have

nam = new QNetworkAccessManager(this);
connect(nam,SIGNAL(finished(QNetworkReply*)),this, SLOT(finished(QNetworkReply*)));

void Network::finished(QNetworkReply *reply)
{
if(reply->error() == QNetworkReply::NoError)
{
//then you use this function wich will make you get a bytearray with the xml file
byteArray = reply->readAll();
}
}

XenGi 2011-04-01 07:08

Re: Qt http post request
 
That sounds quite simple. Thanks for your advice, I will test it later and post the results here. :)

Only one question: What should "QString data" be? Something like "user=root;pass=toor"?

EDIT:
I found it myself. Now everything works fine. Thanks for the help.
If somebody has the same Issue here is a small example showing the "right" way:

Code:

// this file: mainwindow.cpp

#include "mainwindow.h"
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <QUrl>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    leditURL->setText("http://192.168.2.14/index.php");
    leditUser->setText("root");
    leditPassword->setText("toor");

    nam = new QNetworkAccessManager(this);

    connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedDownload(QNetworkReply*)));
    connect(ui->pbDownload, SIGNAL(clicked()), this, SLOT(downloadXML()));
}

void MainWindow::downloadXML()
{
    QByteArray data;
    QUrl params;

    params.addQueryItem("user", ui->leditUser->text());
    params.addQueryItem("pass", ui->leditPassword->text());
    data.append(params.toString());
    data.remove(0,1);

    nam->post(QNetworkRequest(QUrl(leditURL->text())), data);
}

void MainWindow::finishedDownload(QNetworkReply *reply)
{
    txtXML->setText(reply->readAll());
}



All times are GMT. The time now is 09:24.

vBulletin® Version 3.8.8