Results 1 to 12 of 12

Thread: A Qhttp::host() && QHttp::request() fully functionnal example ?

  1. #1
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default A Qhttp::host() && QHttp::request() fully functionnal example ?

    Hi,

    I'm having troubles with the QHttp class.

    I need to use the QHttp class in order to fill a HTTPs form on our Intranet network.
    So, since this morning, I'm traning the QHttp class... But the fact we use HTTPs seems not to help me.
    I would like to test it with an external web form, like www.google.com.

    I'm testing all the source codes I have found, but I can't get the answer

    As somebody a fully functionnal QHttp:: post() && QHttp:: request() example, that allow to fill the www.google.com form and see the result, please ?

    Thanks for your help

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: A Qhttp::host() && QHttp::request() fully functionnal example ?

    Google's search form seems to use GET method so it should be something as simple as:
    Qt Code:
    1. QHttp http("www.google.com");
    2. http.get("/search?q=Qt");
    To copy to clipboard, switch view to plain text mode 
    You can use tools like LiveHTTPHeaders to sniff HTTP requests.
    J-P Nurmi

  3. #3
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A Qhttp::host() && QHttp::request() fully functionnal example ?

    Oh yes, thanks...
    So, I need a Post() example

    I set a simple PHP script here.
    I takes a login and a password.
    The sumbit button calls the same page with the post() method, and display the results.
    If the two fields are filed with "root", the answers says "OK".

    With this simple script I can train my code, and if you want, you can help me .

    Here is the essai.php source code :
    Qt Code:
    1. <html>
    2. <head></head>
    3. <body>
    4.  
    5. <?php
    6. $log_id=$_POST['a'];
    7. $log_mdp=$_POST['b'];
    8.  
    9. echo("Login : $log_id <br>");
    10. echo("Password : $log_mdp");
    11.  
    12. if($log_id=='root' && $log_mdp=='root'){echo("<br><br><B>Good identifiers !</B>\n");}
    13. else{echo("<br><br><B>Bad indentifiers !</B><br>(Only Login=\"root\" and Password=\"root\" will be accepted)");}
    14. ?>
    15.  
    16. <br><br><br><hr><br>
    17.  
    18. <form action='http://www.membres.lycos.fr/nyphel/Essais_stage/essai.php' method='post'>
    19. <br>Login : <input type='text' name='a' value=''><br>
    20. <br>Password : <input type='text' name='b' value=''><br>
    21. <br><input type='submit' name='c' value='submit'>
    22. </form>
    23.  
    24. </body>
    25. </html>
    To copy to clipboard, switch view to plain text mode 

    And now the headers informations given by LiveHTTPHeaders :

    REQUEST :
    REQUEST : GET /nyphel/Essais_stage/essai.php HTTP/1.1
    HOST : www.membres.lycos.fr
    USER AGENT : Mozilla/5.0 (Windows; U; Windows NT 5.0; fr; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3
    ACCEPT : text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
    ACCEPT-LANGUAGE : fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
    ACCEPT-ENCODING : gzip,deflate
    ACCEPT-CHARSET : ISO-8859-1,utf-8;q=0.7,*;q=0.7
    KEPP ALIVE : 300
    PROXY-CONNECTION : keep-alive
    COOKIE : adFrameForcePHP=1; LBC=1f58389ba9f43fbd0c089c99bd202084; utgc=Z2Q9TTpwY29kZT03ODQ1MTpkb2I9MTk4NDA1MDg%3D; lsua=bnlwaGVsbDpOeXBoZWw6TGUgQmVsOmZy; adsprefs=31%3A313938342d30352d3038%3A3738343531%3A 460a193e; lsud=c34a2f6e668bf6648c112d85c818794b%3A1175066941
    ANSWER :
    RESPONSE : HTTP/1.0 200 OK
    DATE : Wed, 28 Mar 2007 07:35:43 GMT
    SERVER : Apache
    X-POWERED-BY: PHP/4.3.2
    CONTENT-TYPE : text/html
    X-CACHE : MISS from www.membres.lycos.fr, MISS from melita.bayonne.fr
    CONTENT-ENCODING : gzip
    CONTENT-LENGHT : 1446
    X-CACHE-LOOKUP : MISS from melita.bayonne.fr:3128
    Last edited by Nyphel; 28th March 2007 at 08:48.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: A Qhttp::host() && QHttp::request() fully functionnal example ?

    I expected it to be this simple:
    Qt Code:
    1. QHttp http("www.membres.lycos.fr");
    2. QByteArray content("a=root&b=root&c=submit");
    3. http.post("/nyphel/Essais_stage/essai.php", content);
    To copy to clipboard, switch view to plain text mode 

    But it turns out that the content type must be set appropriately so it ended up like this:
    Qt Code:
    1. // content
    2. QByteArray content("a=root&b=root&c=submit");
    3.  
    4. // header
    5. QHttpRequestHeader header("POST", "/nyphel/Essais_stage/essai.php");
    6. header.setValue("Host", "www.membres.lycos.fr");
    7. header.setContentType("application/x-www-form-urlencoded"); // important
    8. header.setContentLength(content.length());
    9.  
    10. // request
    11. QHttp http("www.membres.lycos.fr");
    12. http.request(header, content);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A Qhttp::host() && QHttp::request() fully functionnal example ?

    Thanks jpn, but how do you see the response (answer) ?

    I have tried to redirect the response to a file, but nothing has been writen into.
    Qt Code:
    1. http_bis.request(header, content, file);
    To copy to clipboard, switch view to plain text mode 

    And why the content-type isn't "text/html" ?
    The LiveHTTPHeaders plug-in saif that the Content-type of the response was "text/html".

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: A Qhttp::host() && QHttp::request() fully functionnal example ?

    Quote Originally Posted by Nyphel View Post
    Thanks jpn, but how do you see the response (answer) ?

    I have tried to redirect the response to a file, but nothing has been writen into.
    Qt Code:
    1. http_bis.request(header, content, file);
    To copy to clipboard, switch view to plain text mode 
    I used QHttp::readAll() and debug output, but I have now attached a compilable example which writes the result into a file called result.html.

    And why the content-type isn't "text/html" ?
    The LiveHTTPHeaders plug-in saif that the Content-type of the response was "text/html".
    It's the content type of the answer sent by the server. The content type of the request is what matters, that's what we build by hand.
    Attached Files Attached Files
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    Nyphel (28th March 2007)

  8. #7
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A Qhttp::post() && QHttp::request() fully functionnal example ?

    Thanks for your help jpn, but the file isn't created on my computer.
    Perhaps there is connection error, due to the proxy or the port.
    I'll try to check it out .

    In my previous post I said the file was created, but it was created by a simple fileOpen() .
    I used Ethereal to analyze my network, and I noted that no HTTP packet were emitted.

    Now I understand better what happens, and why everybody says me : "Don't use post(), use request() !". The post() don't allow me to set my own headers like it is necessary

    Thanks for all, I'll try to do it

  9. #8
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A Qhttp::host() && QHttp::request() fully functionnal example ?

    That's done !

    Thanks a lot JPN, you helped me so much !
    Now I understand well the QHttp class .

    My network (Intranet, proxy settings, etc.) didn't help me, and drove me in error.





  10. #9
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A Qhttp::host() && QHttp::request() fully functionnal example ?

    Hi

    Here is your main.cpp :
    Qt Code:
    1. #include <QtCore>
    2. #include <QtDebug>
    3. #include <QtNetwork>
    4. #include <iostream>
    5.  
    6. int main(int argc, char* argv[])
    7. {
    8. QCoreApplication a(argc, argv);
    9.  
    10. // content
    11. QByteArray content("a=root&b=root&c=submit");
    12.  
    13. // header
    14. QHttpRequestHeader header("POST", "/nyphel/Essais_stage/essai.php");
    15. header.setValue("Host", "www.membres.lycos.fr");
    16. header.setContentType("application/x-www-form-urlencoded"); // important
    17. header.setContentLength(content.length());
    18.  
    19. // request
    20. QFile file("result.html");
    21. QHttp http("www.membres.lycos.fr", 80);
    22. http.setProxy("172.16.1.1", 8080);
    23. http.request(header, content, &file);
    24.  
    25. a.connect(&http, SIGNAL(done(bool)), &a, SLOT(quit()));
    26. return a.exec();
    27.  
    28. }
    To copy to clipboard, switch view to plain text mode 



    Here is my HTTPChecker class :

    • main.cpp
      Qt Code:
      1. #include <QApplication>
      2. #include <QtGui>
      3.  
      4. #include "HTTPChecker.h"
      5.  
      6. int main(int argc, char *argv[])
      7. {
      8. QApplication app(argc, argv);
      9.  
      10. HTTPChecker *httpChecker = new HTTPChecker();
      11. httpChecker->Check();
      12.  
      13. return app.exec();
      14. }
      To copy to clipboard, switch view to plain text mode 
    • HTTPChecker.h
      Qt Code:
      1. #ifndef HTTPChecker_h
      2. #define HTTPChecker_h
      3.  
      4. #include <QObject>
      5. #include <QHttp>
      6.  
      7. #include <QtCore>
      8. #include <QtDebug>
      9. #include <QtNetwork>
      10. #include <iostream>
      11.  
      12. class HTTPChecker : public QObject
      13. {
      14. Q_OBJECT
      15.  
      16. public:
      17. HTTPChecker();
      18. void Check();
      19.  
      20. private slots:
      21. void SLOT_showPage();
      22.  
      23. private:
      24.  
      25. };
      26.  
      27. #endif // HTTPChecker_h
      To copy to clipboard, switch view to plain text mode 
    • HTTPChecker.cpp
      Qt Code:
      1. #include "HTTPChecker.h"
      2.  
      3. HTTPChecker::HTTPChecker() : QObject()
      4. {
      5. }
      6.  
      7. void HTTPChecker::Check()
      8. {
      9. // content
      10. QByteArray content("a=root&b=root&c=submit");
      11.  
      12. // header
      13. QHttpRequestHeader header("POST", "/nyphel/Essais_stage/essai.php");
      14. header.setValue("Host", "www.membres.lycos.fr");
      15. header.setContentType("application/x-www-form-urlencoded");
      16. header.setContentLength(content.length());
      17.  
      18. // request
      19. QFile file("result.html");
      20. QHttp http("www.membres.lycos.fr", 80);
      21. http.setProxy("172.16.1.1", 8080);
      22. http.request(header, content, &file);
      23.  
      24. connect(&http, SIGNAL(done(bool)), this, SLOT(SLOT_showPage()));
      25. }
      26.  
      27. void HTTPChecker::SLOT_showPage()
      28. {
      29. std::cout << "\n\tOK !" << std::endl;
      30. exit(0);
      31. }
      To copy to clipboard, switch view to plain text mode 



    I don't understand why, but :
    - your main.cpp works well and creates the result.html file
    - my class doesn't create any file ("OK !" is displayed in the console... Immediatly )

  11. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: A Qhttp::host() && QHttp::request() fully functionnal example ?

    You have to allocate the QHttp and QFile objects on the heap (with keyword "new"). Otherwise they get automatically destroyed while going out of scope at the end of HTTPChecker::Check(). In my example, "a.exec()" blocks as long as the application is running so they won't get destroyed before the application quits and so it works.
    J-P Nurmi

  12. #11
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A Qhttp::host() && QHttp::request() fully functionnal example ?

    Oh yes... That's true !
    I forbid that the request was scheduled and performed asynchronously...

    If the IO device to is not 0, the content data of the response is written directly to the device. Make sure that the to pointer is valid for the duration of the operation (it is safe to delete it when the requestFinished() signal is emitted).

    The function does not block and returns immediately. The request is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by requestStarted() and requestFinished().
    Thank you, onemore time...

  13. #12

    Default Re: A Qhttp::host() && QHttp::request() fully functionnal example ?

    There is also one nice solution for posting to web forms: http://www.tuckdesign.com/sources/Qt

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.