Results 1 to 11 of 11

Thread: QNetworkRequest https problem connect missing reply

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 

    }

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.


  3. #3
    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) ?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.


  5. #5
    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, 10: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, 15:33
  3. Issue using connect, what am I missing?
    By technoViking in forum Qt Programming
    Replies: 1
    Last Post: 11th January 2010, 06:11
  4. QReply HTTPS problem
    By Tanuki-no Torigava in forum Qt Programming
    Replies: 0
    Last Post: 12th September 2009, 09:26
  5. QNetworkRequest file upload -- please help
    By Runtime Technologies in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2009, 16: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
  •  
Qt is a trademark of The Qt Company.