PDA

View Full Version : QNetworkAccessManager and post method



sabbu
25th May 2011, 12:47
my http connection is Working i send request to server through QNetworkAccessManager
and also find the response i want to make post method ,i want to concatenate (password, username, address) with url please help how to make post method in Qt


Thanks

Santosh Reddy
28th May 2011, 10:05
I never tried this but, just to give to heads up check this out


void QWebView::load ( const QNetworkRequest & request, QNetworkAccessManager::Operation operation = QNetworkAccessManager::GetOperation, const QByteArray & body = QByteArray() )

ChrisW67
30th May 2011, 02:19
You don't usually concatenate the parameters with the request URL in a POST call, that's a GET. If a GET is what you want the read the docs of QUrl as to how you use parameters ("query items").

If you want a POST request call QNetworkAccessManager::post() with the data payload (i.e. the parameters) available via the second argument. The data payload depends on how your "form" is supposed to work. If you are mimicking a simple form then you should use "application/x-www-form-urlencoded" and encode the payload as if it were the query string for a GET request (QUrl can help here, but you only want the query string part). If you are mimicking a file upload then you need the more complicated "multipart/form-data".

sabbu
3rd June 2011, 17:01
Siri have been make connection and find response from the server but i want to bind a mobile number and password to the url and send to the server but i am not sucesses.please help me,pls see my code.
.cpp


#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include<stdlib.h>

Login::Login(QWidget *parent) :
QDialog(parent),
ui(new Ui::Login)
{
ui->setupUi(this);

}

Login::~Login()
{
delete ui;
}

void Login::on_pushButton_clicked()
{ int n;

QString str;
QString str2;

str=ui->lineEdit->text();
str2=ui->lineEdit_2->text();

n=str.count();
if(n<10)
{
QMessageBox msgBox;
msgBox.setText("Enter 10 digit Mobile number");
msgBox.exec();
}
if(n>10)
{
QMessageBox msgBox;
msgBox.setText("Enter 10 digit Mobile number");
msgBox.exec();
}

connection();

}

void Login::on_buttonBox_accepted()
{
MainWindow *mins = new MainWindow();
mins->show();
}

void Login::on_buttonBox_rejected()
{

}
void Login::connection()
{

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

QUrl url("http://qrrency.com/mobile/j2me/cab/Test.php?name=311");
QNetworkReply *reply = nam->get(QNetworkRequest(url));

}
void Login::finishedSlot(QNetworkReply* reply)
{
// Reading attributes of the reply
QVariant statusCodeV =
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute );

//int randomSayi = rand()%3;

QMessageBox msgBox;

QVariant tempQ = QVariant(statusCodeV.toString().length());
msgBox.setText(tempQ.toString());
int ret = msgBox.exec();

// "200 OK" received?
if (statusCodeV.toInt()==200)
{
// TODO: read data from QNetworkReply
msgBox.setText("Successfull!...");
int ret = msgBox.exec();

QByteArray bytes = reply->readAll(); // bytes
const QString string(bytes); // string
//ui->textEdit->setText(string);

QMessageBox msgBox;
msgBox.setText(string );
msgBox.exec();
}
// Some http error or redirect
else
{
// TODO:
msgBox.setText("UnSuccessfull!...");
int ret = msgBox.exec();
}

delete reply;

}


when i take the value in first str and str2,then i call sever connection i want to pass this paramter to the function connection();
-> send the data to the server

schnitzel
3rd June 2011, 19:14
sorry to keep repeating this... CODE TAGS

wysota
3rd June 2011, 20:11
How does the server expect to receive the data?

ChrisW67
4th June 2011, 00:39
Siri have been make connection and find response from the server but i want to bind a mobile number and password to the url and send to the server but i am not sucesses.please help me,pls see my code.


Since your code at lines 64-65 makes absolutely no attempt to associate the content of the user's input with the URL or request I think there is very little we can do to help you until you understand what you are trying to do.

Are you trying to do a GET request (in your code) or a POST request (in your original post)? Do you know which the server is expecting? Do you know the difference?
Do you know the names of the parameters the server is expecting?


when i take the value in first str and str2,then i call sever connection i want to pass this paramter to the function connection();

Then declare connection() (line 57) to accept some parameters and pass them (line 43). This has nothing to do with a network GET/POST request - it is rudimentary programming.



-> send the data to the server
You cannot send the data to the server until you answer (and understand the answer) my earlier questions. We cannot help until you can express to us what you want to do and tried it yourself.


Other observations:

Your "validation" attempts at lines 29-41 are better enforced with a QRegExpValidator attached to the line edit.

The code at lines 60-62 should be in the Login class constructor. As it is now, every time someone pushes the button a new QNetworkAccessManager is allocated on the heap and lost (memory leak). For every new there MUST eventually be a matching delete. If you allocate it on the heap in the constructor then you should put the matching delete in the destructor. BTW: It doesn't really need to be on the heap at all. Line 49 is similar memory leak.


BTW: See how much easier it is for us to reference your code IF YOU BOTHER TO PUT THE CODE IN
TAGS. You've been asked politely many times before, and Wysota has fixed it for you this time, but it is about time you played the game.

sabbu
10th June 2011, 11:56
sir ,

i have been make server connection through the QNetworkAccessManager but i want to make post method and send name, userid, and possword to server please help how to make post method in Qt.see my code

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

QUrl url(QLatin1String("http://taxinomics.com/mobile/Login.php?mobilenumber=number&password=m"));
this is get method i want to make post method how to make.i beginner for Qt.
Thanks

ChrisW67
13th June 2011, 01:08
What was wrong with your earlier thread? You start these threads and as soon as it becomes apparent that no-one is going to post the exact, fully working solution within a day or so you just drop the thread only to come back later with the same question.

To convert a GET to a POST you take the parameters you were encoding into the URL and put them in the POST request content with the relevant headers. The previous thread gives you all the components to achieve this.

wysota
13th June 2011, 01:35
Threads merged. Please don't multipost ever again.

sabbu
16th June 2011, 15:17
my http connction is working but when i send a mobile number and password exact he give response -1 means your mobile number is not match.please any one help me where i am worng my code is below.

Login::Login(QWidget *parent) :
QDialog(parent),
ui(new Ui::Login)
{
ui->setupUi(this);

}

Login::~Login()
{
delete ui;
}

void Login::on_pushButton_clicked()
{

QString str;
QString str2;

str=ui->lineEdit->text();
str2=ui->lineEdit_2->text();

connection(str,str2 );

}



void Login::connection( QString str,QString str2)
{

QByteArray postData;
postData.append("mobilenumber=");
postData.append(str);
postData.append("&password=");
postData.append(str2);
nam = new QNetworkAccessManager(this);
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
this, SLOT(finishedSlot(QNetworkReply*)));

QUrl url(QLatin1String("http://taxinomics.com/mobile/Login.php? "));

QNetworkReply *reply = nam->post(QNetworkRequest(url),postData);



}
void Login::finishedSlot(QNetworkReply* reply)
{

QVariant statusCodeV =
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute );

QMessageBox msgBox;
QVariant tempQ = QVariant(statusCodeV.toString().length());
msgBox.setText(tempQ.toString());
int ret = msgBox.exec();

// "200 OK" received?
if (statusCodeV.toInt()==200)
{

msgBox.setText("Successfull!...");
int ret = msgBox.exec();

QByteArray bytes = reply->readAll(); // bytes
const QString string(bytes); // string

QMessageBox msgBox;
msgBox.setText(string );
msgBox.exec();
}

else
{

msgBox.setText("UnSuccessfull!...");
int ret = msgBox.exec();
}

delete reply;

}

schnitzel
16th June 2011, 22:09
removed contents

ChrisW67
17th June 2011, 00:01
To fix your "-1" response I suggest you send a valid mobilenumber and password or fix the logic of your web application. That is the response I get with a rubbish mobilenumber and password using wget (i.e. nothing to do with Qt):

$ wget -S --post-data='mobilenumber=0123456789&password=rubbish' http://taxinomics.com/mobile/Login.php
--2011-06-17 07:58:25-- http://taxinomics.com/mobile/Login.php
Resolving taxinomics.com (taxinomics.com)... 173.205.127.22
Connecting to taxinomics.com (taxinomics.com)|173.205.127.22|:80... connected.
HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Date: Thu, 16 Jun 2011 21:58:25 GMT
Server: Apache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: PHPSESSID=cc6f1b1c2580586dda06ca681a5988af; path=/
Cache-Control: public
Connection: close
Content-Type: text/html; charset=utf-8
Length: unspecified [text/html]
Saving to: `Login.php'

[ <=> ] 2 --.-K/s in 0s

2011-06-17 07:58:26 (684 KB/s) - `Login.php' saved [2]
$ cat Login.php
-1



You don't need the '?' and trailing space at the end of the URL: your URL is "http://taxinomics.com/mobile/Login.php?%20" (although this does not seem to be breaking it).
You can use QUrl to build the payload with due care for encoding of values. What happens at the moment if the password contains an '&'?