Reply
Thread Tools
Guest | Posts: n/a | Thanked: 0 times | Joined on
#1
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>";
}

?>

Last edited by XenGi; 2011-04-01 at 06:45.
 
Posts: 289 | Thanked: 101 times | Joined on Oct 2009
#2
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();
}
}
 
Guest | Posts: n/a | Thanked: 0 times | Joined on
#3
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());
}

Last edited by XenGi; 2011-04-01 at 09:56. Reason: Got the Answer
 

The Following 2 Users Say Thank You to For This Useful Post:
Reply

Thread Tools

 
Forum Jump


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