Results 1 to 17 of 17

Thread: Setting a cookie for a Qhttp connection

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

    Default Setting a cookie for a Qhttp connection

    Hello,

    I've heard that it was possible to set a cookie for a QHttp connection, while I was searching the solution . The search function of the forum has shown me various posts, but none explain how to use the cookies.

    Could you give me a tip, please ?

    Thanks



    My source :
    Qt Code:
    1. QHttp *RC_http;
    2. RC_http = new QHttp("courriel", 80);
    3. RC_httpId = RC_http->get("/index.php?tg=addon/46/main&idx=aac");
    To copy to clipboard, switch view to plain text mode 
    courriel stands for a local host and let me see the "?tg=addon/46/main&idx=aac" page only if I'm authentificated. So I do a post() request on index.php in order to be authenticated, but I need to get the cookie in order to send it with the get() call.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Setting a cookie for a Qhttp connection

    You'll have to use the request() method instead of get() and set the cookie header (Cookie: name=value) in the QHttpRequestHeader object.

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

    Nyphel (5th April 2007)

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

    Default Re: Setting a cookie for a Qhttp connection

    Thanks Wysota, I made it

    Qt Code:
    1. // header
    2. QHttpRequestHeader header("GET", "/index.php?tg=addon/46/main&idx=aac");
    3. header.setValue("Host", "courriel");
    4. header.setValue("Cookie", "OV1274289294=8bffb00c3b0917a3c446531973dcd8c7");
    5. header.setContentType("application/x-www-form-urlencoded");
    6.  
    7. // request
    8. RC_httpId = RC_http->request(header);
    To copy to clipboard, switch view to plain text mode 

    That's nice, but the cookie value change for each new connection.
    So, when my application do a post() request in order to log in, there is a new cookie value... And when my application wants to do the get() request, sha must know the new cookie value.

    So, is it possible to get the cookie value when I do a post() request ?
    Here is my post() request :
    Qt Code:
    1. // content
    2. //QByteArray content("nickname=Stagiaire&password=QDMSy9P&submit=Connexion");
    3. QByteArray content = "";
    4. content.append("nickname");
    5. content.append("=");
    6. content.append(identifiant);
    7. content.append("&");
    8. content.append("password");
    9. content.append("=");
    10. content.append(password);
    11. content.append("&");
    12. content.append("submit");
    13. content.append("=");
    14. content.append("Connexion");
    15.  
    16. // header
    17. QHttpRequestHeader header("POST", "/index.php");
    18. header.setValue("Host", "courriel");
    19. header.setContentType("application/x-www-form-urlencoded");
    20. header.setContentLength(content.length());
    21.  
    22. // request
    23. IL_httpId = IL_http->request(header, content);
    To copy to clipboard, switch view to plain text mode 

    IL_http is connected to the done(bool) signal, wich calls a slot in order to read th response (performing a simple "QByteArray reponse = IL_http->readAll();").
    The reponse QBteArray holds the content of the page that is displayed when the user tries to longgin on the website, but I can't see the cookie value...
    IL_http is also connected to the signals requestFinished(int, bool) and responseHeaderReceived(const QHttpResponseHeader &), in order to detect if an errors has occured... I've heard that this last signal should help me, but I don't understand how.
    Last edited by Nyphel; 4th April 2007 at 14:07.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Setting a cookie for a Qhttp connection

    Do the same the other way round - look for "Set-cookie:" header in http response header.

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

    Default Re: Setting a cookie for a Qhttp connection

    Oki !

    Qt Code:
    1. connect(IL_http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
    2. this, SLOT(SLOT_IL_readResponseHeader(const QHttpResponseHeader &)));
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void HTTPChecker::SLOT_IL_readResponseHeader(const QHttpResponseHeader &responseHeader)
    2. {
    3. std::cout << "\n\n" << (responseHeader.toString()).toStdString() << std::endl;
    4. if (responseHeader.statusCode() != 200)
    5. {
    6. IL_NB_erreurs = IL_NB_erreurs + 1;
    7. std::cout << "\n\nERREUR (IL_readResponseHeader) : " << (responseHeader.reasonPhrase()).toStdString() << std::endl;
    8. return;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    And this returned me the responseHeader content :
    Qt Code:
    1. HTTP/1.1 200 OK
    2. date: Wed, 04 Apr 2007 13:15:44 GMT
    3. server: Apache/2.0.54 (Debian GNU/Linux) PHP/5.1.6-1~bpo.1 mod_ssl/2.0.54 OpenSSL/0.9.7e
    4. x-powered-by: PHP/5.1.6-1~bpo.1
    5. set-cookie: OV1274289294=a13f0dc68488034b668c01f61f03de50; path=/
    6. expires: Thu, 19 Nov 1981 08:52:00 GMT
    7. cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    8. pragma: no-cache
    9. content-length: 4053
    10. content-type: text/html
    To copy to clipboard, switch view to plain text mode 

    Here is the cookie value
    I think that I can get only the cookie value be calling QHttpHeader::value() or QHttpHeader::values().

    Thanks a lot for your help Wysota
    Last edited by jacek; 4th April 2007 at 22:23. Reason: wrapped too long line

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

    Default Re: Setting a cookie for a Qhttp connection

    I don't understand well...

    1) I authenticate manually, using my navigator.
    A plug-in give me the cookie value.
    I set this value to my header (for my QHttp objet), in my application.
    While I'm still connected with the navigator, I launch my application in order to get a page that is behind the authtentification one.
    OK : I can acces and download this document.

    2) If I use only my application and get the cookie value while I'm authenticating me (A Qhttp object that perform a post()),
    and if I set this cookie value to my header of the second Qhttp objet, the one that will do the post() request,
    I can't acces to the document .

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Setting a cookie for a Qhttp connection

    Does the "authentication" involve only the cookie? Could you please check what exact headers are sent to and from the browser?

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

    Default Re: Setting a cookie for a Qhttp connection

    So, I need to log in the "courriel" page with a POST() request.

    Here is the HTML form :
    Qt Code:
    1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    2. <html>
    3.  
    4. [...]
    5.  
    6. <form method="post" action="index.php">
    7.  
    8. <input type="hidden" name="tg" value="login">
    9. <input type="hidden" name="referer" value="http%3A%2F%2Fcourriel%2Findex.php">
    10. <input type="hidden" name="login" value="login">
    11.  
    12. <br><br>
    13. <table width="50%" border="0" cellspacing="0" cellpadding="2" align="center">
    14. <tr>
    15. <td class="BabLoginCadreBackground" align="center" valign="middle">
    16. <table class="BabLoginMenuBackground" width="100%" border="0" cellspacing="0" cellpadding="5" align="center">
    17. <tr>
    18. <td width="30%" align="right">Identifiant : </td>
    19. <td>
    20. <input type="text" name="nickname" value="" size="20" maxlength="255">
    21. </td>
    22.  
    23. </tr>
    24. <tr>
    25. <td align="right">Mot de passe : </td>
    26. <td>
    27. <input type="password" name="password" size="20" maxlength="30">
    28. </td>
    29. </tr>
    30. <tr>
    31. <td align="right">Mémoriser mon identité sur cet ordinateur : </td>
    32.  
    33. <td>
    34. <select name="lifetime">
    35. <option value="0">Non</option>
    36. <option value="18000" selected>5 heures</option>
    37. <option value="36000">10 heures</option>
    38. </select>
    39. </td>
    40. </tr>
    41.  
    42.  
    43. <tr valign="middle">
    44. <td colspan="2" align="center">
    45. <input type="submit" name="submit" value="Connexion">
    46. </td>
    47. </tr>
    48. </table>
    49. </td>
    50. </tr>
    51. </table>
    52. </form>
    53.  
    54. </div>
    55. </div>
    56. <script type="text/javascript">
    57. faux_onload();
    58. </script>
    59. </div>
    60.  
    61.  
    62.  
    63. </body>
    64. </html>
    To copy to clipboard, switch view to plain text mode 

    Here are the headers detected when I enter the "courriel" adress in my navigator (Fiefox) :
    http://courriel/

    REQUEST :
    GET / HTTP/1.1
    Host: courriel
    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
    Keep-Alive: 300
    Connection: keep-alive
    Cookie: OV1274289294=b976dcbd8c1f5190d9b45bcd93bdb178

    RESPONSE :
    HTTP/1.x 200 OK
    Date: Thu, 05 Apr 2007 07:19:23 GMT
    Server: Apache/2.0.54 (Debian GNU/Linux) PHP/5.1.6-1~bpo.1 mod_ssl/2.0.54 OpenSSL/0.9.7e
    X-Powered-By: PHP/5.1.6-1~bpo.1
    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
    Content-Length: 4053
    Keep-Alive: timeout=15, max=100
    Connection: Keep-Alive
    Content-Type: text/html
    And here is what is detected when I log-in manually, with good identifiers :
    http://courriel/index.php

    REQUEST :
    POST /index.php HTTP/1.1
    Host: courriel
    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
    Keep-Alive: 300
    Connection: keep-alive
    Referer: http://courriel/
    Cookie: OV1274289294=b976dcbd8c1f5190d9b45bcd93bdb178
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 85
    tg=login&login=login&referer=&nickname=stagiaire&p assword=%2FQDMSy9P&submit=Connexion

    RESPONSE :
    HTTP/1.x 302 Found
    Date: Thu, 05 Apr 2007 07:23:50 GMT
    Server: Apache/2.0.54 (Debian GNU/Linux) PHP/5.1.6-1~bpo.1 mod_ssl/2.0.54 OpenSSL/0.9.7e
    X-Powered-By: PHP/5.1.6-1~bpo.1
    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
    Content-Length: 4064
    Keep-Alive: timeout=15, max=100
    Connection: Keep-Alive
    Content-Type: text/html
    And now with bad identifiers :
    http://courriel/index.php

    REQUEST :
    POST /index.php HTTP/1.1
    Host: courriel
    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
    Keep-Alive: 300
    Connection: keep-alive
    Referer: http://courriel/
    Cookie: OV1274289294=b976dcbd8c1f5190d9b45bcd93bdb178
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 85
    tg=login&login=login&referer=&nickname=stagiaire&p assword=%2FQDMSy9P&submit=Connexion

    RESPONSE :
    HTTP/1.x 302 Found
    Date: Thu, 05 Apr 2007 07:23:50 GMT
    Server: Apache/2.0.54 (Debian GNU/Linux) PHP/5.1.6-1~bpo.1 mod_ssl/2.0.54 OpenSSL/0.9.7e
    X-Powered-By: PHP/5.1.6-1~bpo.1
    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
    Content-Length: 4064
    Keep-Alive: timeout=15, max=100
    Connection: Keep-Alive
    Content-Type: text/html
    Last edited by Nyphel; 5th April 2007 at 11:16.

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

    Default Re: Setting a cookie for a Qhttp connection

    Perhaps would you want to get a look to my code :
    "IL" (Intranet_Login) concerns the initial POST() request, that connect the client to the "courriel" page (authentification).
    "RC" (Recuperer_Comptes) concerns the GET() request, that download a page once we are connected.
    The initial request (post) get the cookie value and store it in "cookie".
    The second request (get) set this cookie value in his header.
    Qt Code:
    1. #include <QApplication>
    2. #include <QByteArray>
    3. #include <iostream>
    4.  
    5. #include "HTTPChecker.h"
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication app(argc, argv);
    10.  
    11. identifiant = "Stagiaire";
    12. password = "/QDMSy9P";
    13.  
    14. HTTPChecker *httpChecker = new HTTPChecker();
    15. httpChecker->Intranet_Login(identifiant, password);
    16.  
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. HTTPChecker::HTTPChecker() : QObject()
    2. {
    3. cookie = ""; // Holds the cookie value, from the POST() request
    4.  
    5. IL_NB_erreurs = 0;
    6. IL_http = new QHttp("courriel", 80);
    7.  
    8. connect(IL_http, SIGNAL(done(bool)), this, SLOT(SLOT_IL_showPage()));
    9. connect(IL_http, SIGNAL(requestFinished(int, bool)), this, SLOT(SLOT_IL_httpRequestFinished(int, bool)));
    10. connect(IL_http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),this, SLOT(SLOT_IL_readResponseHeader(const QHttpResponseHeader &)));
    11.  
    12. RC_NB_erreurs = 0;
    13. RC_http = new QHttp("courriel", 80);
    14.  
    15. connect(RC_http, SIGNAL(done(bool)), this, SLOT(SLOT_RC_showPage()));
    16. connect(RC_http, SIGNAL(requestFinished(int, bool)), this, SLOT(SLOT_RC_httpRequestFinished(int, bool)));
    17. connect(RC_http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),this, SLOT(SLOT_RC_readResponseHeader(const QHttpResponseHeader &)));
    18. }
    To copy to clipboard, switch view to plain text mode 

    First request : authentification :
    Qt Code:
    1. void HTTPChecker::Intranet_Login(QByteArray identifiant, QByteArray password)
    2. {
    3. // content //tg=login&login=login&referer=&nickname=stagiaire&password=%2FQDMSy9P&submit=Connexion
    4.  
    5. QByteArray content = "";
    6. content.append("tr");
    7. content.append("=");
    8. content.append("login");
    9. content.append("login");
    10. content.append("=");
    11. content.append("login");
    12. content.append("referer");
    13. content.append("=");
    14. content.append("");
    15. content.append("nickname");
    16. content.append("=");
    17. content.append(identifiant);
    18. content.append("&");
    19. content.append("password");
    20. content.append("=");
    21. content.append(password);
    22. content.append("&");
    23. content.append("submit");
    24. content.append("=");
    25. content.append("Connexion");
    26.  
    27. // header
    28. QHttpRequestHeader header("POST", "/index.php");
    29. header.setValue("Host", "courriel");
    30. header.setContentType("application/x-www-form-urlencoded");
    31. header.setContentLength(content.length());
    32.  
    33. // request
    34. IL_httpId = IL_http->request(header, content);
    35. }
    36.  
    37.  
    38. void HTTPChecker::SLOT_IL_showPage()
    39. {
    40. if (IL_NB_erreurs == 0)
    41. {
    42. QByteArray reponse = IL_http->readAll();
    43.  
    44. QString reponse_lisible = "";
    45. reponse_lisible.append(reponse);
    46. reponse_lisible = reponse_lisible.section("** ",1,1);
    47. reponse_lisible = reponse_lisible.section(" **",0,0);
    48.  
    49. std::cout << "-=" << reponse_lisible.toStdString() << "=-" << std::endl;
    50. }
    51. else
    52. {
    53. std::cout << "\n\nREPONSE : =-ERREURS-=\n---------" << std::endl;
    54. }
    55. Recuperer_Comptes(); // Call the second request
    56. }
    57.  
    58.  
    59. void HTTPChecker::SLOT_IL_httpRequestFinished(int requestId, bool error)
    60. {
    61. if (requestId != IL_httpId)
    62. return;
    63.  
    64. if (error)
    65. {
    66. IL_NB_erreurs = IL_NB_erreurs + 1;
    67. std::cout << "\n\nERREUR (IL_httpRequestFinished) : " << (IL_http->errorString()).toStdString() << std::endl;
    68. }
    69. }
    70.  
    71. void HTTPChecker::SLOT_IL_readResponseHeader(const QHttpResponseHeader &responseHeader)
    72. {
    73. if (responseHeader.statusCode() != 200)
    74. {
    75. IL_NB_erreurs = IL_NB_erreurs + 1;
    76. std::cout << "\n\nERREUR (IL_readResponseHeader) : " << (responseHeader.reasonPhrase()).toStdString() << std::endl;
    77. return;
    78. }
    79.  
    80. QString cookie_value = responseHeader.value("set-cookie");
    81. cookie_value = cookie_value.section(";", 0, 0);
    82. cookie = cookie_value; // Store teh cokie value, for the second request
    83. }
    To copy to clipboard, switch view to plain text mode 

    Second request : the download of a page
    Qt Code:
    1. void HTTPChecker::Recuperer_Comptes()
    2. {
    3. // header
    4. QHttpRequestHeader header("GET", "/index.php?tg=addon/46/main&idx=aac");
    5. header.setValue("Host", "courriel");
    6. header.setValue("Cookie", cookie);
    7. header.setContentType("application/x-www-form-urlencoded");
    8.  
    9. std::cout << "\n\n" << (header.toString()).toStdString() << std::endl;
    10.  
    11. // request
    12. RC_httpId = RC_http->request(header);
    13. }
    14.  
    15.  
    16. void HTTPChecker::SLOT_RC_showPage()
    17. {
    18. if (RC_NB_erreurs == 0)
    19. {
    20. QByteArray reponse = RC_http->readAll();
    21.  
    22. QString reponse_lisible = "";
    23. reponse_lisible.append(reponse);
    24. std::cout << reponse_lisible.toStdString() << std::endl;
    25. }
    26. else
    27. {
    28. std::cout << "\n\nREPONSE : =-ERREURS-=\n---------" << std::endl;
    29. }
    30. Quitter();
    31. }
    32.  
    33.  
    34. void HTTPChecker::SLOT_RC_httpRequestFinished(int requestId, bool error)
    35. {
    36. if (requestId != RC_httpId)
    37. return;
    38.  
    39. if (error)
    40. {
    41. RC_NB_erreurs = RC_NB_erreurs + 1;
    42. std::cout << "\n\nERREUR (RC_httpRequestFinished) : " << (RC_http->errorString()).toStdString() << std::endl;
    43. }
    44. }
    45.  
    46. void HTTPChecker::SLOT_RC_readResponseHeader(const QHttpResponseHeader &responseHeader)
    47. {
    48. if (responseHeader.statusCode() != 200)
    49. {
    50. RC_NB_erreurs = RC_NB_erreurs + 1;
    51. std::cout << "\n\nERREUR (RC_readResponseHeader) : " << (responseHeader.reasonPhrase()).toStdString() << std::endl;
    52. return;
    53. }
    54. }
    To copy to clipboard, switch view to plain text mode 

    Isn't it good ?

    Another idea :
    My account for testing is : "Stagiaire" - "/QDMSy9P".
    If we have a look to the headers shown by the navigator, we can see that "/QDMSy9P" becomes "%2FQDMSy9P".
    And my request, in the application, send "/QDMSy9P".
    I dont think this is a problem, cause when I test with "Stagiaire" - "%2FQDMSy9P", nothing changes.

  11. #10
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Setting a cookie for a Qhttp connection

    To grab cookie i make it so..... code down ....
    grab check and resend ..... next session....

    otherwise if i upload big file i use method PUT && PHP
    is faster as post!

    http://www.qtforum.de/forum/viewtopic.php?t=3085

    and i send one virtual cookie nr. "2352Rt35j235-252Zu532kh5-3252" to autenficate on php script
    and one cookie wo place to save file .... simply 10 line code...

    Qt Code:
    1. typedef QMap<int, QStringList> cookiepam;
    2.  
    3. cookiepam ActualCook;
    4.  
    5. /* each request i resend the cookie from last session phpsession id... or other */
    6. QStringList resendcoo;
    7. resendcoo.clear();
    8. cookiepam ::Iterator it;
    9. for ( it = ActualCook.begin(); it != ActualCook.end(); ++it ) {
    10. QStringList resi = it.value();
    11. QString name = resi.at(0);
    12. QString oneci = QString("%1=%2").arg(name).arg(resi.at(1));
    13. resendcoo.append(oneci);
    14. }
    15. header.setValue("Cookie",resendcoo.join(";"));
    16.  
    17.  
    18.  
    19.  
    20. /* take cookie from header */
    21. void RegisterCookie(const QHttpResponseHeader &responseHeader )
    22. {
    23. ActualCook.clear();
    24. QStringList cookielist = responseHeader.allValues("set-cookie");
    25. for (int i = 0; i < cookielist.size(); ++i) {
    26. QString cokeline = cookielist.at(i);
    27. QStringList onlines = cokeline.split("=");
    28. QString cookiename = onlines.at(0);
    29. QString cookievalue = onlines.at(1);
    30. ActualCook.insert(i,QStringList() << cookiename << Url_Decode(cookievalue) << actualurl);
    31. /////////////////qDebug() << "### entry set-cookie pos=" << i << " name= " << cookiename << " value=" << Url_Decode(cookievalue);
    32. }
    33. //////////emit WatReturn(QString("One code=%1 - Other say = %2 \n%3").arg( responseHeader.statusCode() ).arg( responseHeader.reasonPhrase() ).arg(CookieVars()) );
    34. }
    35.  
    36. /* decode cookie value */
    37. QString Base_Function::Url_Decode( QString indata )
    38. {
    39. /*
    40. http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
    41. Dollar ("$") 24
    42. Ampersand ("&") 26
    43. Plus ("+") 2B
    44. Comma (",") 2C
    45. Forward slash/Virgule ("/") 2F
    46. Colon (":") 3A
    47. Semi-colon (";") 3B
    48. Equals ("=") 3D
    49. Question mark ("?") 3F
    50. 'At' symbol ("@") 40
    51. Left Curly Brace ("{") 7B
    52. Right Curly Brace ("}") 7D
    53. Vertical Bar/Pipe ("|") 7C
    54. Backslash ("\") 5C
    55. Caret ("^") 5E
    56. Tilde ("~") 7E
    57. Left Square Bracket ("[") 5B
    58. Right Square Bracket ("]") 5D
    59. Grave Accent ("`") 60
    60. */
    61. QString blnull = "";
    62. QString notaccept = "%60|%5D|%5B|%7E|%5E|%5C|%7C|%7D|%7B";
    63. QStringList notallow;
    64. notallow = notaccept.split("|");
    65.  
    66. for (int i = 0; i < notallow.size(); ++i) {
    67. if ( indata.contains(notallow.at(i)) ) {
    68. return blnull;
    69. }
    70. }
    71.  
    72. QString spaceout = indata.replace("%20"," ");
    73. spaceout = spaceout.replace("%3A",":");
    74. spaceout = spaceout.replace("%3B",";");
    75. spaceout = spaceout.replace("%3D","=");
    76. spaceout = spaceout.replace("%2F","/");
    77. spaceout = spaceout.replace("%3F","?");
    78. spaceout = spaceout.replace("%40","@");
    79. spaceout = spaceout.replace("%24","$");
    80. spaceout = spaceout.replace("%2B","+");
    81. spaceout = spaceout.replace("+"," ");
    82. int zool = spaceout.indexOf(";",0);
    83. return spaceout.left(zool);;
    84. }
    To copy to clipboard, switch view to plain text mode 

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Setting a cookie for a Qhttp connection

    Are you sure your POST request is correct? Try setting up a fake server and check if the content the server receives is exactly the same for a regular browser and your manual auth. Under Unix you could use netcat for that, but I don't know if it's available for Windows.

  13. The following user says thank you to wysota for this useful post:

    Nyphel (5th April 2007)

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

    Default Re: Setting a cookie for a Qhttp connection

    That's what I made last week .
    I think my post request is correct, but perhaps is there an authentification problem.
    The webmaster said me I was authenticated as an external user, and in this case the server unauthentificate me each time the connection is close (closing Firefox for example). So, perhaps, I'm unauthentificated cause I use 2 QHttp objetcs...

    I'll try to see that, and confirm that I'm correctly logged-in with the first request

  15. #13
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Setting a cookie for a Qhttp connection

    Quote Originally Posted by Nyphel View Post
    That's what I made last week .
    I think my post request is correct, but perhaps is there an authentification problem.
    The webmaster said me I was authenticated as an external user, and in this case the server unauthentificate me each time the connection is close (closing Firefox for example). So, perhaps, I'm unauthentificated cause I use 2 QHttp objetcs...

    I'll try to see that, and confirm that I'm correctly logged-in with the first request

    if you have quote..
    If we have a look to the headers shown by the navigator, we can see that "/QDMSy9P" becomes "%2FQDMSy9P".
    http://www.blooberry.com/indexdot/ht...rlencoding.htm urldecode problem....
    if you grab cookie must decode and resend qstring can not decode url string!!

    Grab before post ... the form page .... on get method the form page .... to take phpsession code cookie ..
    i suppose if php can not init session your post is invalid! why session is not init!

    first ....
    1- QHttp get ( formpage_client ) take session PHPSESSIONID= xxxxxxxx
    and
    2- QHttp post ( form data ) +session + referrer ( PHPSESSIONID= xxxxxxxx ) cookie and param ...
    like firefox method....

    otherwise session on server not valid ASP have it a same mechanismous ... if the session ist not incomming server suppose a hack...

  16. The following user says thank you to patrik08 for this useful post:

    Nyphel (5th April 2007)

  17. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Setting a cookie for a Qhttp connection

    Quote Originally Posted by Nyphel View Post
    The webmaster said me I was authenticated as an external user, and in this case the server unauthentificate me each time the connection is close (closing Firefox for example).
    I don't think this is the case. Closing a connection can happen without closing the browser - the server will close the connection about a few seconds without seeing another request coming, so you'd end up with the same effect in Firefox which I guess is not the case.
    When the browser closes, it may discard the cookie, but the server won't be notified about it, so this shouldn't influence your requests either.

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

    Default Re: Setting a cookie for a Qhttp connection

    You are true : my first request (the POST one, in order to be authentificated) is wrong.
    I got a cookie, but i was not logged-in.

    Moreover, my header content wasn't well written :
    Qt Code:
    1. QByteArray content = "";
    2. content.append("tr");
    3. content.append("=");
    4. content.append("login");
    5. content.append("login");
    6. content.append("=");
    7. content.append("login");
    8. content.append("referer");
    9. content.append("=");
    10. content.append("");
    11. content.append("nickname");
    12. content.append("=");
    13. content.append(identifiant);
    14. content.append("&");
    15. content.append("password");
    16. content.append("=");
    17. content.append(password);
    18. content.append("&");
    19. content.append("submit");
    20. content.append("=");
    21. content.append("Connexion");
    To copy to clipboard, switch view to plain text mode 
    Some "&" were missing !

    So here is my new content :
    Qt Code:
    1. QByteArray content = "";
    2.  
    3. content.append("tr");
    4. content.append("=");
    5. content.append("login");
    6. content.append("&");
    7. content.append("login");
    8. content.append("=");
    9. content.append("login");
    10. content.append("&");
    11. content.append("referer");
    12. content.append("=");
    13. content.append("http%3A%2F%2Fcourriel%2Findex.php");
    14. content.append("&");
    15.  
    16. content.append("nickname");
    17. content.append("=");
    18. content.append("Stagiaire");
    19. content.append("&");
    20. content.append("password");
    21. content.append("=");
    22. content.append("%2FQDMSy9P");
    23. content.append("&");
    24. content.append("lifetime");
    25. content.append("=");
    26. content.append("10000");
    27. content.append("&");
    28. content.append("submit");
    29. content.append("=");
    30. content.append("Connexion");
    To copy to clipboard, switch view to plain text mode 

    In order to prevent url encoding errors, wrote the "/" directly with "%2F".
    Now, my responseHeaderReceived() signal catch an error :
    statusCode = 302
    reasonPhrase = Found

    That's strange, cause the 302 error indicates that the Web server thinks that my URL has been temporarily redirected to another URL.

    Here is the full responseHeader :
    Qt Code:
    1. HTTP/1.1 302 Found
    2. date: Thu, 05 Apr 2007 12:55:23 GMT
    3. server: Apache/2.0.54 (Debian GNU/Linux) PHP/5.1.6-1~bpo.1 mod_ssl/2.0.54 OpenSSL/0.9.7e
    4. x-powered-by: PHP/5.1.6-1~bpo.1
    5. set-cookie: OV1274289294=b5ca1f5abaeea5cb6944080a88adc958; path=/
    6. expires: Thu, 19 Nov 1981 08:52:00 GMT
    7. cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    8. pragma: no-cache
    9. set-cookie: c_nickname=Stagiaire; expires=Thu, 05-Apr-2007 15:42:03 GMT
    10. set-cookie: c_password=a07390205157ca30543f2128de117385; expires=Thu, 05-Apr-2007 15:42:03 GMT
    11. location: http://courriel/index.php
    12. transfer-encoding: chunked
    13. content-type: text/html
    To copy to clipboard, switch view to plain text mode 

    Now, I'm logged in and everything is OK... If I don't care about the 302 error.
    So, I've you got an idea to explain why I get such error, please ?
    Last edited by Nyphel; 5th April 2007 at 14:11.

  19. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Setting a cookie for a Qhttp connection

    Should this script set those cookies (lines 9 and 10 of the above output) in that situation? You'd have to ask the script author what 302 means in this case It contains a redirection to index.php so looks like everything is fine, you should follow the location. It probably just redirects you to the proper page after login.

  20. The following user says thank you to wysota for this useful post:

    Nyphel (5th April 2007)

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

    Default Re: Setting a cookie for a Qhttp connection

    The cookies of lines 9 and 10 seems to be corresponding with the authentification... But I don't think they are usefull. I think that they are identifying the longgin session period.

    About the redirection :
    Qt Code:
    1. <form method="post" action="index.php">
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. <input type="hidden" name="referer" value="http%3A%2F%2Fcourriel%2Findex.php">
    To copy to clipboard, switch view to plain text mode 
    The redirection targets the same page as the form action .
    Ok, I will discuss about that with the webmaster.

    Thanks a lot Wysota and Patrick, now I understand better the cookies and how managing them with Qt4/ QHttp

Similar Threads

  1. QHttp internal error
    By LubosD in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2006, 09:57
  2. how to use QHttp inside QThread in Qt3
    By alusuel in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2006, 11:19

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
  •  
Qt is a trademark of The Qt Company.