Results 1 to 15 of 15

Thread: Help needed for Using HTTP for data transfer

  1. #1
    Join Date
    Oct 2011
    Location
    India
    Posts
    13
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default 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());
    }

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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:
    Qt Code:
    1. 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"
    To copy to clipboard, switch view to plain text mode 

    None of this works if you don't have an event loop. The post() call is not synchronous.
    "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.

  3. The following user says thank you to ChrisW67 for this useful post:

    pranj1488 (8th November 2011)

  4. #3
    Join Date
    Oct 2011
    Location
    India
    Posts
    13
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default 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?

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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.
    Last edited by ChrisW67; 8th November 2011 at 05:53.

  6. #5
    Join Date
    Oct 2011
    Location
    India
    Posts
    13
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default 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.

    Qt Code:
    1. import javax.servlet.ServletConfig;
    2. import javax.servlet.ServletException;
    3. import javax.servlet.http.HttpServlet;
    4. import javax.servlet.http.HttpServletRequest;
    5. import javax.servlet.http.HttpServletResponse;
    6. import java.io.IOException;
    7.  
    8. public class SlCustomObject_AndroidCalls extends HttpServlet
    9. {
    10. public void init(ServletConfig config) throws ServletException
    11. {
    12. try
    13. {
    14. super.init(config);
    15. }
    16. catch(Exception e){Logger.toLog("Failed ","SlCustomObject_AndroidCalls","init: Exception " + e);}
    17. }
    18.  
    19. public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
    20. {
    21. try
    22. {
    23. doPost(req,res);
    24. }
    25. catch(Exception e){Logger.toLog("Failed ","SlCustomObject_AndroidCalls","doGet: Exception " + e);}
    26. }
    27.  
    28. public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
    29. {
    30. try
    31. {
    32. String stParam_one="";
    33. String stParam_two="";
    34.  
    35. if(req.getParameter("Param_one")!=null)
    36. {
    37. stParam_one = req.getParameter("Param_one").toString().trim();
    38. }
    39. if(req.getParameter("Param_two")!=null)
    40. {
    41. stParam_two = req.getParameter("Param_two").toString().trim();
    42. }
    43. Logger.toLog("Param_1 ","SlCustomObject_AndroidCalls","**** Call In Servlet ******");
    44. Logger.toLog("Param_2 ","SlCustomObject_AndroidCalls","stParam_one: "+stParam_one+" stParam_two: "+stParam_two);
    45.  
    46. }
    47. catch(Exception e)
    48. {
    49. Logger.toLog("Android ","SlCustomObject_AndroidCalls","doPost: Exception " + e);
    50. }
    51. }//end of doPost() method
    52.  
    53. }//End of servlet
    To copy to clipboard, switch view to plain text mode 
    Last edited by pranj1488; 8th November 2011 at 06:39.

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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.
    Qt Code:
    1. wget -S -O - --post-data "Param_one=51&Param_two=51" http://anush/alva/SlCustomObject_AndroidCalls
    2. curl -D - --data "Param_one=51&Param_two=51" http://anush/alva/SlCustomObject_AndroidCalls
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to ChrisW67 for this useful post:

    pranj1488 (8th November 2011)

  9. #7
    Join Date
    Oct 2011
    Location
    India
    Posts
    13
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default 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

  10. #8
    Join Date
    Oct 2011
    Location
    India
    Posts
    13
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default 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?

    Qt Code:
    1. #include "mynetclass.h"
    2. #include "ui_mynetclass.h"
    3.  
    4. mynetclass::mynetclass(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::mynetclass)
    7. {
    8. ui->setupUi(this);
    9. connect_server();
    10. }
    11.  
    12. mynetclass::~mynetclass()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void mynetclass::connect_server()
    18. {
    19. QNetworkAccessManager *manager = new QNetworkAccessManager();
    20.  
    21. QString strurl= "http://anush/alva/SlCustomObject_AndroidCalls?Param_one=51&Param_two =51";
    22. QUrl url( strurl, QUrl::TolerantMode );
    23.  
    24. QNetworkRequest request;
    25. request.setUrl(url);
    26. QNetworkReply* reply=manager->get(request);
    27. QByteArray bytes = reply->readAll();
    28. QString str(bytes); // string
    29. ui->resp->setText(str);
    30. }
    To copy to clipboard, switch view to plain text mode 

  11. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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.

  12. The following user says thank you to ChrisW67 for this useful post:

    pranj1488 (9th November 2011)

  13. #10
    Join Date
    Oct 2011
    Location
    India
    Posts
    13
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default 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?

  14. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Help needed for Using HTTP for data transfer

    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).

  15. #12
    Join Date
    Oct 2011
    Location
    India
    Posts
    13
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default 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....

  16. #13
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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

  17. The following user says thank you to ChrisW67 for this useful post:

    pranj1488 (10th November 2011)

  18. #14
    Join Date
    Oct 2011
    Location
    India
    Posts
    13
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default 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!!

  19. #15
    Join Date
    Oct 2011
    Location
    India
    Posts
    13
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default 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?

Similar Threads

  1. Data transfer using TCP Socket
    By rex in forum Qt Programming
    Replies: 0
    Last Post: 24th March 2011, 17:06
  2. USB/Bluetooth Data Transfer
    By awaisj2003 in forum Newbie
    Replies: 1
    Last Post: 31st August 2010, 12:44
  3. Data transfer between QIODevice
    By benlau in forum Qt Programming
    Replies: 0
    Last Post: 24th February 2010, 06:59
  4. Transfer Data Between Dialogs
    By umulingu in forum Qt Programming
    Replies: 4
    Last Post: 19th February 2010, 09:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.