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
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
I never tried this but, just to give to heads up check this out
Qt Code:
void QWebView::load ( const QNetworkRequest & request, QNetworkAccessManager::Operation operation = QNetworkAccessManager::GetOperation, const QByteArray & body = QByteArray() )To copy to clipboard, switch view to plain text mode
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".
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
Qt Code:
#include <QNetworkAccessManager> #include <QUrl> #include <QNetworkRequest> #include <QNetworkReply> #include<stdlib.h> 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); this, SLOT(finishedSlot(QNetworkReply*))); 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; 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(); //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; }To copy to clipboard, switch view to plain text mode
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
Last edited by wysota; 3rd June 2011 at 20:03.
sorry to keep repeating this... CODE TAGS
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?
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.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();
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.-> send the data to the server
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 [code][/code] 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.
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
this is get method i want to make post method how to make.i beginner for Qt.Qt Code:
nam = new QNetworkAccessManager(this); this, SLOT(finishedSlot(QNetworkReply*))); To copy to clipboard, switch view to plain text mode
Thanks
Last edited by wysota; 13th June 2011 at 01:34. Reason: missing [code] tags
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.
"We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.
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.
Qt Code:
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 ); } { QByteArray postData; postData.append("mobilenumber="); postData.append(str); postData.append("&password="); postData.append(str2); nam = new QNetworkAccessManager(this); this, SLOT(finishedSlot(QNetworkReply*))); QNetworkReply *reply = nam->post(QNetworkRequest(url),postData); } void Login::finishedSlot(QNetworkReply* reply) { QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); QMessageBox msgBox; msgBox.setText(tempQ.toString()); int ret = msgBox.exec(); // "200 OK" received? if (statusCodeV.toInt()==200) { msgBox.setText("Successfull!..."); int ret = msgBox.exec(); QMessageBox msgBox; msgBox.setText(string ); msgBox.exec(); } else { msgBox.setText("UnSuccessfull!..."); int ret = msgBox.exec(); } delete reply; }To copy to clipboard, switch view to plain text mode
Last edited by wysota; 16th June 2011 at 15:52. Reason: missing [code] tags
removed contents
Last edited by schnitzel; 16th June 2011 at 23:47.
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):
Qt Code:
$ 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 -1To copy to clipboard, switch view to plain text mode
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 '&'?
Bookmarks