Re: Help needed for Using HTTP for data transfer
I need my program to post some values that exist in key->value pair in my url in the log file thats stored on the server. Following is my code it is executing without any errors but nothing gets posted on the server side.
Can anybody please tell wats the mistake in the code? Or if there is any additional code that needs to be added?
#include "mynetclass.h"
#include "ui_mynetclass.h"
mynetclass::mynetclass(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::mynetclass)
{
ui->setupUi(this);
connect_server();
}
mynetclass::~mynetclass()
{
delete ui;
}
void mynetclass::connect_server()
{
QNetworkAccessManager *manager = new QNetworkAccessManager();
QString strurl= "http://anush/alva/SlCustomObject_AndroidCalls?Param_one=51&Param_two =51";
QUrl url( strurl, QUrl::TolerantMode );
QNetworkRequest request;
request.setUrl(url);
manager->post(request,url.encodedQuery());
}
Re: Help needed for Using HTTP for data transfer
Works fine here except:
- That you are using a GET url in your POST operation, and duplicating the URL parameter list in the POST data.
- Your QNetworkAccessManager is a memory leak.
Here is what I see in my web server log file:
Code:
192.168.1.6 - - [07/Nov/2011:18:48:54 +1000] "POST /alva/SlCustomObject_AndroidCalls?Param_one=51&Param_two%20=51 HTTP/1.1" 404 287 "-" "Mozilla/5.0"
None of this works if you don't have an event loop. The post() call is not synchronous.
Re: Help needed for Using HTTP for data transfer
I've Modified my POST method.. I have another query that can this code communicate with a servlet file "SlCustomObject_AndroidCalls "stored on my server(as mentioned in the url) ? or it can post values only to a CGI script?
Re: Help needed for Using HTTP for data transfer
Just like any other web client Qt knows nothing about how the server processes a request, just that the server exists and offers certain functions and returns certain responses. A POST is a POST regardless of whether the server processing is done in Java, Perl, PHP, or by trained monkeys.
BTW: If you offered more information about exactly what doesn't work and a small, complete program that demonstrates the problem then perhaps we could offer more advice.
Re: Help needed for Using HTTP for data transfer
thank you chris!!
this is for the first time that I am communicating my queries to any expert on a forum,so please forgive me if my way of questioning have caused any inconvinience to you.
Added after 11 minutes:
Following is my servelet program that will accept the parameters sent through Post method & just wite it in a log file that resides on my server.
Code:
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class SlCustomObject_AndroidCalls extends HttpServlet
{
public void init(ServletConfig config) throws ServletException
{
try
{
super.init(config);
}
catch(Exception e){Logger.toLog("Failed ","SlCustomObject_AndroidCalls","init: Exception " + e);}
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
{
try
{
doPost(req,res);
}
catch(Exception e){Logger.toLog("Failed ","SlCustomObject_AndroidCalls","doGet: Exception " + e);}
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
{
try
{
String stParam_one="";
String stParam_two="";
if(req.getParameter("Param_one")!=null)
{
stParam_one = req.getParameter("Param_one").toString().trim();
}
if(req.getParameter("Param_two")!=null)
{
stParam_two = req.getParameter("Param_two").toString().trim();
}
Logger.toLog("Param_1 ","SlCustomObject_AndroidCalls","**** Call In Servlet ******");
Logger.toLog("Param_2 ","SlCustomObject_AndroidCalls","stParam_one: "+stParam_one+" stParam_two: "+stParam_two);
}
catch(Exception e)
{
Logger.toLog("Android ","SlCustomObject_AndroidCalls","doPost: Exception " + e);
}
}//end of doPost() method
}//End of servlet
Re: Help needed for Using HTTP for data transfer
This is wandering outside of Qt.
Do you know the servlet works (is coded correctly and installed in the server correctly) by testing it independently of your Qt program? You can use a simple client like wget or curl, or a hand coded web form to test it.
Code:
wget -S -O - --post-data "Param_one=51&Param_two=51" http://anush/alva/SlCustomObject_AndroidCalls
curl -D - --data "Param_one=51&Param_two=51" http://anush/alva/SlCustomObject_AndroidCalls
Re: Help needed for Using HTTP for data transfer
hello chris ..
I just made few changes in my url as it was the only mistake causing the errors and now the code works fine...thanks :)
Re: Help needed for Using HTTP for data transfer
I am now adding some more functionality to my existing code by trying to receive some reply from the server.
My code is able to post values on server but is not fetching reply from server.
Can you please tell me what exactly is the mistake I am commiting?
Code:
#include "mynetclass.h"
#include "ui_mynetclass.h"
mynetclass
::mynetclass(QWidget *parent
) :ui(new Ui::mynetclass)
{
ui->setupUi(this);
connect_server();
}
mynetclass::~mynetclass()
{
delete ui;
}
void mynetclass::connect_server()
{
QNetworkAccessManager *manager = new QNetworkAccessManager();
QString strurl
= "http://anush/alva/SlCustomObject_AndroidCalls?Param_one=51&Param_two =51";
QNetworkRequest request;
request.setUrl(url);
QNetworkReply* reply=manager->get(request);
ui->resp->setText(str);
}
Re: Help needed for Using HTTP for data transfer
QNetworkAccessManager::get() and post() are not synchronous. The request has not even been sent when you reach line 27, let alone returned with available data. I suggest you look at the examples, particularly HTTP Example, and docs provided with Qt before you waste much more time trying to make this go.
Re: Help needed for Using HTTP for data transfer
thank you chris for your immediate response..
I went through the example you mentioned, so is there any example that would demonstrate how to retrive response sent by server and perform some further operation depending on server response?
Re: Help needed for Using HTTP for data transfer
:confused: That is what the example does if it receives a redirection response.
You can do anything you like in response to reading part or all of the data in the relevant slot. That includes sending another request (perhaps connected to different handler slots).
Re: Help needed for Using HTTP for data transfer
Hiii chris...I am really sorry if i am making it more confusing but I am really clueless what to do next as i want to do is retrieve data from sever in such a way that for e.g If I have passed my user name and password as parameters can i just send the client same parameters back as a concatenated string? And then display it on my GUI? I have written the code that will send the data back to client in my servlet file,so the qt code needs the modifications in order to fetch that string..
As http is asynchronous protocol I’m bit unclear about how to achieve it…can you guide me through this ?
I hope I have provided sufficient information....
Re: Help needed for Using HTTP for data transfer
For known small responses you just connect the QNetworkReply::finished() signal to a slot and in that slot:
- check for error states (connection refused etc.)
- use QNetworkReply::readAll() to get the data the server sent back
- do whatever you want with the data.
I cannot make it much easier than that without doing it for you, and then you wouldn't be learning.
For large responses then you could capture data progressively by using the readyRead() signal as in the HTTP Example
Re: Help needed for Using HTTP for data transfer
hey chris
I connected the my slot with QNetworkReply::finished() slot and now I am able to setch data using readAll() method... thank you very much..you have been a great help!! :D:D
Re: Help needed for Using HTTP for data transfer
hiii there!!
I am trying to write this same application for symbian device so what additional code is required to allow this program to run as a symbian app?