Results 1 to 11 of 11

Thread: QNetworkRequest https problem connect missing reply

  1. #1
    Join Date
    Sep 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QNetworkRequest https problem connect missing reply

    Hello I create this thread because i dont find solution for my problem.

    I want to connect in this web site and take cookies: https://cas.univ-ubs.fr/login

    my CookieJar class write in a file all cookies of the reply but they have nothing with this website.

    Qt Code:
    1. void FenPrincipale::takeCookies()
    2. {
    3. QUrl postData;
    4. postData.addQueryItem("username","user101");
    5. postData.addQueryItem("password",var2_Edit->text());
    6. postData.addQueryItem("warn", "true");
    7. postData.addQueryItem("lt", lt);
    8. postData.addQueryItem("_eventId", "submit");
    9. postData.addQueryItem("submit", "SE CONNECTER");
    10.  
    11. // On crée une requête
    12. QNetworkRequest requete(QUrl(urlEdit1->text()));
    13.  
    14. requete.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    15. r = m->post(requete, postData.encodedQuery());
    16.  
    17.  
    18. // Gestion des erreurs
    19. connect(r, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(messageErreur(QNetworkReply::NetworkError)));
    20. // Gestion de fin de téléchargement
    21. connect(r, SIGNAL(finished()), this, SLOT(takeLink()));
    22. }
    23.  
    24. // The second slot for the reply
    25. void FenPrincipale::takeLink()
    26. {
    27. QNetworkReply *reply = static_cast<QNetworkReply*>(sender());
    28. QNetworkAccessManager *m = reply->manager();
    29. m->setCookieJar(new CookieJar(this));
    30.  
    31. qDebug() << reply->readAll();
    32. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance.
    Last edited by wysota; 10th September 2010 at 19:28. Reason: missing [code] tags

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

    Default Re: QNetworkRequest https problem connect missing reply

    So what exactly is the problem? Why are you assigning a cookie jar to the reply? It won't have any effect, it's too late for that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QNetworkRequest https problem connect missing reply

    Hello wysota
    In the first method takeId i make a GET request to take an id like :c9A188DB9-048B-8F48-0B65-D696212D9AF2_k7D5BBE5A-A72E-33F9-38AD-7C37C4146126

    In the takeCookie method i made a cookie in write in my cookie file :
    Sun, 12-Aug-2012 17:59:28 GMT; domain=cas.univ-ubs.fr; path=/c9A188DB9-048B-8F48-0B65-D696212D9AF2_k7D5BBE5A-A72E-33F9-38AD-7C37C4146126

    next i make a POST request for login with my first cookie, my username, the id, and my password but nothing append.

    in firefox when i just go in the web i receveid a cookie but in qt i can't so i simulated the first cookie.

    In python a similar code work perfectly then i think i just a little error in my code.
    Qt Code:
    1. void FenPrincipale::takeId()
    2. {
    3. QNetworkAccessManager *m = new QNetworkAccessManager();
    4. QNetworkRequest requete(QUrl("https://cas.univ-ubs.fr/login"));
    5.  
    6. QNetworkReply *r = m->get(requete);
    7. qDebug() << "takeId";
    8.  
    9. connect(r, SIGNAL(finished()), this, SLOT(takeCookies()));
    10. connect(r, SIGNAL(error(QNetworkReply::NetworkError)), this,
    11. SLOT(messageErreur(QNetworkReply::NetworkError)));
    12.  
    13. }
    14.  
    15. void FenPrincipale::takeCookies()
    16. {
    17. boutonEnvoyer->setEnabled(false);
    18.  
    19.  
    20. QNetworkReply *reply = static_cast<QNetworkReply*>(sender());
    21. QNetworkAccessManager *m = reply->manager();
    22.  
    23. // Récupère L'id
    24. QString source = reply->readAll();
    25. QString lt = source.section("\"",119,119);
    26. // Récupère le Cookie JSession
    27. source = source.section("\"",41,41);
    28. // Contruit le cookie
    29. QString data;
    30. data = "JSESSIONID";
    31. data += source.remove(QRegExp("[a-z/;]"));
    32. data += "; expires=Sun, 12-Aug-2012 17:59:28 GMT; domain=cas.univ-ubs.fr; path=/";
    33.  
    34. qDebug() << "takeCookies =" << data;
    35.  
    36. QFile f("Cookies.txt");
    37. QTextStream out(&f);
    38. if ( f.open(QIODevice::ReadWrite) )
    39. {
    40. QString exist = out.readAll();
    41. if (exist.indexOf("cas") != -1 )
    42. {
    43. out.seek(0);
    44. qDebug() << "seek";
    45. }
    46.  
    47. out << data;
    48. f.close();
    49. }
    50. m->setCookieJar(new CookieJar(this));
    51.  
    52. QUrl postData;
    53. postData.addQueryItem("username","");
    54. postData.addQueryItem("password",var2_Edit->text());
    55. postData.addQueryItem("warn", "true");
    56. postData.addQueryItem("lt", lt);
    57. postData.addQueryItem("_eventId", "submit");
    58. postData.addQueryItem("submit", "SE CONNECTER");
    59.  
    60. // On crée une requête
    61. QNetworkRequest requete(QUrl(urlEdit1->text()));
    62.  
    63. QNetworkReply *r;
    64. if (box2->isChecked())
    65. {
    66. requete.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    67. r = m->post(requete, postData.encodedQuery());
    68. }
    69.  
    70. // Gestion des erreurs
    71. connect(r, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(messageErreur(QNetworkReply::NetworkError)));
    72. // Gestion de fin de téléchargement
    73. connect(r, SIGNAL(finished()), this, SLOT(takeLink()));
    74.  
    75. // Gestion de la barre de progression
    76. connect(r, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(progressionTelechargement(qint64, qint64)));
    77.  
    78. connect(r, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(ErreurSsl(const QList<QSslError>&)));
    79.  
    80. connect(m, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)),
    81. this,SLOT(authentification(QNetworkReply*, QAuthenticator*)));
    82.  
    83. }
    84.  
    85. void FenPrincipale::takeLink()
    86. {
    87. QNetworkReply *reply = static_cast<QNetworkReply*>(sender());
    88. QNetworkAccessManager *m = reply->manager();
    89. m->setCookieJar(new CookieJar(this));
    90.  
    91. qDebug() << reply->readAll();
    92. boutonEnvoyer->setEnabled(true);
    93.  
    94. QNetworkRequest requete(QUrl("http://web.univ-ubs.fr/edt/ade/custom/myplanning/myPlanning.jsp?top=top.moniframeesup"));
    95. QNetworkReply *r = m->get(requete);
    96. qDebug() << "takeLink";
    97. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QNetworkRequest https problem connect missing reply

    I already told you, you are using the cookie jar incorrectly. Your code is so vague I can't make anything of it. If you tried expressing yourself in a more proper English, maybe I could help you but right now I have no idea what your code is supposed to do. Surely setting a new cookie jar with each request is not the right thing to do. Setting it when you receive a reply is another wrong thing to do. You should set the cookie jar once right after creating the network access manager object. Then you can add cookies which you want the manager to return with requests it makes and you can read cookies from the cookie jar that are returned with replies from the server.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QNetworkRequest https problem connect missing reply

    I would simply connect to this web site. for her I must make two requestes. A GET request for an id and a POST request with the id, username and password.

    I just create the first cookie because i can't received the cookie with the GET request.

    I have move the 'setCookiejar' at first.

    Qt Code:
    1. void FenPrincipale::takeId()
    2. {
    3. QNetworkAccessManager *m = new QNetworkAccessManager();
    4. // Set the cookieJar
    5. m->setCookieJar(new CookieJar(this));
    6. QNetworkRequest requete(QUrl("https://cas.univ-ubs.fr/login"));
    7.  
    8. // I make a first GET request.
    9. QNetworkReply *r = m->get(requete);
    10. qDebug() << "takeId";
    11.  
    12. connect(r, SIGNAL(finished()), this, SLOT(takeCookies()));
    13. connect(r, SIGNAL(error(QNetworkReply::NetworkError)), this,
    14. SLOT(messageErreur(QNetworkReply::NetworkError)));
    15.  
    16. }
    17.  
    18. void FenPrincipale::takeCookies()
    19. {
    20. QNetworkReply *reply = static_cast<QNetworkReply*>(sender());
    21. QNetworkAccessManager *m = reply->manager();
    22.  
    23. // I recover the id
    24. QString source = reply->readAll();
    25. QString lt = source.section("\"",119,119);
    26. source = source.section("\"",41,41);
    27.  
    28. // Build the cookie
    29. QString data;
    30. data = "JSESSIONID";
    31. data += source.remove(QRegExp("[a-z/;]"));
    32. data += "; expires=Sun, 12-Aug-2012 17:59:28 GMT; domain=cas.univ-ubs.fr; path=/";
    33.  
    34. // Write a cookie in the cookie file
    35. QFile f("Cookies.txt");
    36. QTextStream out(&f);
    37. if ( f.open(QIODevice::ReadWrite) )
    38. {
    39. QString exist = out.readAll();
    40. if (exist.indexOf("cas") != -1 )
    41. {
    42. out.seek(0);
    43. qDebug() << "seek";
    44. }
    45.  
    46. out << data;
    47. f.close();
    48. }
    49.  
    50. // Build url contents
    51. QUrl postData;
    52. postData.addQueryItem("username","");
    53. postData.addQueryItem("password",var2_Edit->text());
    54. postData.addQueryItem("warn", "true");
    55. postData.addQueryItem("lt", lt);
    56. postData.addQueryItem("_eventId", "submit");
    57. postData.addQueryItem("submit", "SE CONNECTER");
    58.  
    59. QNetworkRequest requete(QUrl("https://cas.univ-ubs.fr/login"));
    60. requete.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    61.  
    62. // I make the second request
    63. QNetworkReply *r = m->post(requete, postData.encodedQuery());
    64.  
    65.  
    66. connect(r, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(messageErreur(QNetworkReply::NetworkError)));
    67. connect(r, SIGNAL(finished()), this, SLOT(takeLink()));
    68.  
    69. connect(r, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(ErreurSsl(const QList<QSslError>&)));
    70.  
    71. connect(m, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)),
    72. this,SLOT(authentification(QNetworkReply*, QAuthenticator*)));
    73.  
    74. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QNetworkRequest https problem connect missing reply

    readAll() from the network reply will not return you the cookies. By the time you receive the reply, the cookie should already be in the cookie jar, you don't have to create it yourself. Use QNetworkCookieJar::cookiesForUrl() to retrieve the cookie or dump it somewhere from your implementation of the cookie jar's (as I see you have one) QNetworkCookieJar::setCookiesFromUrl() method.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Sep 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QNetworkRequest https problem connect missing reply

    My cookieJar class implementation work good, with any web site in http (google etc...) but with an https request in don't received any cookie. Normally i have to received cookie with https://cas.univ-ubs.fr/login in GET and POST request. I was using readAll for the id, just for that.

    Qt Code:
    1. // CookieJar class
    2. #include "Cookies.h"
    3.  
    4. QList<QNetworkCookie> CookieJar::cookiesForUrl ( const QUrl & url ) const
    5. {
    6. return QNetworkCookieJar::cookiesForUrl(url);
    7. }
    8.  
    9. bool CookieJar::setCookiesFromUrl ( const QList<QNetworkCookie> & cookieList, const QUrl & url )
    10. {
    11. return QNetworkCookieJar::setCookiesFromUrl(cookieList, url);
    12. }
    13.  
    14. CookieJar::CookieJar(QObject *parent)
    15. :QNetworkCookieJar(parent)
    16. {
    17. QFile cookieFile("Cookies.txt");
    18. if (cookieFile.exists() && cookieFile.open(QIODevice::ReadOnly) )
    19. {
    20. QList<QNetworkCookie> list;
    21. QByteArray line;
    22. while(!(line = cookieFile.readLine()).isNull())
    23. {
    24. list.append(QNetworkCookie::parseCookies(line));
    25. }
    26. setAllCookies(list);
    27. }
    28. }
    29.  
    30. CookieJar::~CookieJar()
    31. {
    32. QFile cookieFile("Cookies.txt");
    33. if (cookieFile.open(QIODevice::WriteOnly))
    34. {
    35. QTextStream out(&cookieFile);
    36. foreach(const QNetworkCookie & cookie, allCookies())
    37. {
    38. if (!cookie.isSessionCookie())
    39. {
    40. out << cookie.toRawForm() << endl;
    41. qDebug() << "write **********COOKIE******************* ";
    42. }
    43. }
    44. }
    45. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // My request code
    2. void FenPrincipale::takeId()
    3. {
    4. QNetworkAccessManager *m = new QNetworkAccessManager();
    5. // Set the cookieJar
    6. m->setCookieJar(new CookieJar(this));
    7. QNetworkRequest requete(QUrl("https://cas.univ-ubs.fr/login"));
    8.  
    9. // I make a first GET request.
    10. QNetworkReply *r = m->get(requete);
    11. qDebug() << "takeId";
    12.  
    13. connect(r, SIGNAL(finished()), this, SLOT(takeCookies()));
    14. connect(r, SIGNAL(error(QNetworkReply::NetworkError)), this,
    15. SLOT(messageErreur(QNetworkReply::NetworkError)));
    16.  
    17. }
    18.  
    19. void FenPrincipale::takeCookies()
    20. {
    21. QNetworkReply *reply = static_cast<QNetworkReply*>(sender());
    22. QNetworkAccessManager *m = reply->manager();
    23.  
    24. // I recover the id
    25. QString source = reply->readAll();
    26. QString lt = source.section("\"",119,119);
    27. source = source.section("\"",41,41);
    28.  
    29. // Build url contents
    30. QUrl postData;
    31. postData.addQueryItem("username","");
    32. postData.addQueryItem("password",var2_Edit->text());
    33. postData.addQueryItem("warn", "true");
    34. // I make the id in the URL
    35. postData.addQueryItem("lt", lt);
    36. postData.addQueryItem("_eventId", "submit");
    37. postData.addQueryItem("submit", "SE CONNECTER");
    38.  
    39. QNetworkRequest requete(QUrl("https://cas.univ-ubs.fr/login"));
    40. requete.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    41.  
    42. // I make the second request
    43. QNetworkReply *r = m->post(requete, postData.encodedQuery());
    44.  
    45.  
    46. connect(r, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(messageErreur(QNetworkReply::NetworkError)));
    47. connect(r, SIGNAL(finished()), this, SLOT(takeLink()));
    48.  
    49. connect(r, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(ErreurSsl(const QList<QSslError>&)));
    50. }
    51.  
    52. void FenPrincipale::takeLink()
    53. {
    54. QNetworkReply *reply = static_cast<QNetworkReply*>(sender());
    55. QNetworkAccessManager *m = reply->manager();
    56.  
    57. // ----------> I want to have access to protect file in the web site with my cookie
    58. }
    To copy to clipboard, switch view to plain text mode 

    }

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

    Default Re: QNetworkRequest https problem connect missing reply

    Please, understand it - the result returned by readAll() will not contain the part of the conversation with the cookie. Your "lt" and "source" variables will not contain parts of the cookie.

    With an ssl connection you don't get anything probably because the site returns an SSL error (handshake failed due to self signed certificate) which you do not handle correctly (see docs for QNetworkReply::sslErrors() signal).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Sep 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QNetworkRequest https problem connect missing reply

    I know, I just want to have the id like this in the web site:
    <input type="hidden" name="lt" value="_cC02BDB12-D1D9-7155-261D-9D1174D68BD3_k4E4C0CC3-872E-EA91-456C-A29E1E0B4CF5" />

    I was using this method for the sslError but i don't have any error:

    Qt Code:
    1. void FenPrincipale::ErreurSsl(const QList<QSslError> & errors)
    2. {
    3. qDebug () << "errorSSl";
    4. for (int i=0; i < errors.length(); i++)
    5. {
    6. qDebug() << "errorSSl = " << errors[i].errorString();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Why i don't have any SSL error ? why the web site return nothing (no redirection) ?

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

    Default Re: QNetworkRequest https problem connect missing reply

    Quote Originally Posted by Linux5445 View Post
    I know, I just want to have the id like this in the web site:
    <input type="hidden" name="lt" value="_cC02BDB12-D1D9-7155-261D-9D1174D68BD3_k4E4C0CC3-872E-EA91-456C-A29E1E0B4CF5" />
    Doing it with QString::section() is a bad idea - if the website contents change it is likely you method will stop working. Better use regular expressions (QRegExp) for getting the id.

    I was using this method for the sslError but i don't have any error:
    Bearing the fact the certificate of the website is self-signed, you should be getting one error. Does the finished() signal for the network reply even get emitted?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Sep 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QNetworkRequest https problem connect missing reply

    Yes is true for the QString section.

    When i'm try connected until build a cookie i have a redirection with the cookie in the url.
    https://cas.univ-ubs.fr/login;jsessionid=EC0A6DAAB7A739B99B00B17200346C84" .
    When i don't used the cookie i just have a redirection.

    In python i just used (HTTPCookieProcessor)
    Qt Code:
    1. def __init__(self):
    2. self.CJ = CookieJar()
    3. self.connection = build_opener(HTTPCookieProcessor(self.CJ))
    4.  
    5. def getConnection(self):
    6. try:
    7. u = self.connection.open("https://cas.univ-ubs.fr/login").read()
    8. f = '<input type="hidden" name="lt" value="'
    9. l = u.rfind(f)
    10. r = u.rfind('" />\n\t\t\t\t\t\t<input type="hidden" name="_eventId"')
    11. data = u[l+len(f):r]
    12. params = urlencode({
    13. "username":self.username,
    14. "password":self.password,
    15. "lt":data,
    16. "_eventId":"submit",
    17. "submit":"SE CONNECTER"
    18. })
    19.  
    20. self.connection.open("https://cas.univ-ubs.fr/login", params)
    To copy to clipboard, switch view to plain text mode 

    I can't translate perfectly in C++ with Qt but i think is possible to work.

Similar Threads

  1. Replies: 0
    Last Post: 3rd August 2010, 09:07
  2. Compiler error when calling QObject::connect. What am I missing?
    By themanwiththequestion in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2010, 14:33
  3. Issue using connect, what am I missing?
    By technoViking in forum Qt Programming
    Replies: 1
    Last Post: 11th January 2010, 05:11
  4. QReply HTTPS problem
    By Tanuki-no Torigava in forum Qt Programming
    Replies: 0
    Last Post: 12th September 2009, 08:26
  5. QNetworkRequest file upload -- please help
    By Runtime Technologies in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2009, 15:55

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.