Results 1 to 9 of 9

Thread: Problem with Qnetwork cookies and getting code after login

  1. #1
    Join Date
    Oct 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    Windows

    Exclamation Problem with Qnetwork cookies and getting code after login

    I want with POST method connect to a webpage from "www.example.com/login" and after i succsesfully login I want to go for e.g. here "www.example.com/index.php" but i cant get this to work becouse it still returns me "www.example.com/login" no matter what i try.


    Well, I connect successfully and get source code of login page without any errors. I believe session should be still alive after I do post, but I don't know how to correctly go to another page. Maybe after login I should ask for cookies and use them when I ask for another page.


    Qt Code:
    1. QNetworkAccessManager *manager;
    2. manager = new QNetworkAccessManager ();
    3. QNetworkRequest req;
    4.  
    5. address =("www.example/index.php");
    6.  
    7. manager->setCookieJar(new QNetworkCookieJar);
    8.  
    9. req.setHeader(req.UserAgentHeader,"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2");
    10. req.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
    11. req.setUrl(QUrl("www.example.com/login"));
    12.  
    13. QByteArray postData;
    14.  
    15. postData.append("login_user=user&");
    16. postData.append("login_passwd=Pass");
    17.  
    18. connect (manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(writeToTxt(QNetworkReply *)));
    19.  
    20. manager->post(req, postData);
    21. manager->get(QNetworkRequest(address));
    22. manager->get(QNetworkRequest(QUrl("www.example.com/example.html")));
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void HtmlSource::writeToTxt(QNetworkReply* reply)
    2. {
    3. qDebug() << "atsakymas" << endl;
    4. if(reply->isOpen()){
    5. //qDebug() << reply->readAll();
    6. QFile file( "1.txt" );
    7. if ( file.open(QIODevice::ReadWrite) )
    8. {
    9. file.write(reply->readAll());
    10. file.close();
    11.  
    12. }}}
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    Windows

    Angry Re: Problem with Qnetwork cookies and getting code after login

    Annyone? I still need help guys. I just don't know where to start searching for problem. What I'am doing wrong
    Last edited by DomantasB; 2nd November 2013 at 18:09.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with Qnetwork cookies and getting code after login

    Well, you currently do three HTTP requests in parallel, a post and two gets.

    If the post is a form of login, you might want to wait for its result before you do the gets.

    Cheers,
    _

  4. #4
    Join Date
    Oct 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with Qnetwork cookies and getting code after login

    So if i understand you correctly i should remove those to gets and put them in different function. After i run function with POST and it finishes I should run function with gets?

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with Qnetwork cookies and getting code after login

    Exactly!

    Cheers,
    _

  6. #6
    Join Date
    Oct 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with Qnetwork cookies and getting code after login

    Well it doesn't work.

    I created new function

    Qt Code:
    1. void HtmlSource::ndlink(){
    2. manager->get(QNetworkRequest(QUrl("https://www.tamo.lt/dienynas1.html?s=namu_darbai")));
    3. }
    To copy to clipboard, switch view to plain text mode 

    I moved all post function to constructor.

    aand it doesn't work. I tried connecting ndlink function with another connect and to another slot but then it doesn't return anythink.

    Constructor part works perfectly. It logins to website but then I can't go to another that site page.

    here is all code

    Qt Code:
    1. HtmlSource::HtmlSource(QObject *parent) : QObject(parent)
    2. {
    3. qDebug() << "-----------here is constructor";
    4. manager = new QNetworkAccessManager ();
    5. address =("https://www.tamo.lt/dienynas1.html?s=namu_darbai/");
    6. QNetworkRequest req;
    7.  
    8. manager->setCookieJar(new QNetworkCookieJar);
    9. req.setHeader(req.UserAgentHeader,"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2");
    10. req.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
    11. req.setRawHeader("Connection", "Keep-Alive");
    12. req.setRawHeader("Accept-Charset", "WINDOWS-1257,utf-8;q=0.7,*;q=0.7");
    13. req.setRawHeader("Cookie", "lang=lt");
    14. req.setUrl(QUrl("https://www.tamo.lt/dienynas1.html?s=namu_darbai/"));
    15. QByteArray postData;
    16. postData.append("login_user=login&");
    17. postData.append("login_passwd=pasew");
    18.  
    19. connect (manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(write(QNetworkReply*)));
    20. manager->post(req, postData);
    21. }
    22.  
    23. void HtmlSource::write(QNetworkReply* reply)
    24. {
    25. qDebug() << "atsakymas" << endl;
    26. if(reply->isOpen()){
    27.  
    28. QFile file( "Ge2as1.txt" );
    29.  
    30. if ( file.open(QIODevice::ReadWrite) )
    31. {
    32. qDebug() << reply->readAll();
    33. file.write(reply->readAll());
    34. file.close();
    35.  
    36. }
    37.  
    38. }
    39. }
    40.  
    41.  
    42. void HtmlSource::ndlink(){
    43.  
    44. qDebug() << "-------------- here starts get function" << endl<< endl<< endl<< endl<< endl<< endl<< endl<< endl;
    45.  
    46. manager->get(QNetworkRequest(QUrl("https://www.tamo.lt/dienynas1.html?s=namu_darbai")));
    47.  
    48. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by DomantasB; 11th November 2013 at 19:35.

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with Qnetwork cookies and getting code after login

    So what you have is a post request and when that is finished you read the reply and write it to a file. You also have a method called ndlink() that is never called.
    I would have expected that you want to call ndlink after the post succeeded.

    What exactly does not work? You say that the post works and the code doen't try to do anything else.

    Cheers,
    _

  8. #8
    Join Date
    Oct 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with Qnetwork cookies and getting code after login

    first of all i would like to thank you for helping me so much.

    I call
    Qt Code:
    1. ndlink();
    To copy to clipboard, switch view to plain text mode 
    from main.cpp file. After your post I also tried to call it in a consturctor but it gives same resullts.

    My problem is that I post with url "https://www.tamo.lt/dienynas1.html?s=namu_darbai/" and after that, page redirects me to somethink like /index.php

    and after that redirection, I want to come back again to "https://www.tamo.lt/dienynas1.html?s=namu_darbai/". But what ever I try it still gives me that index.php

    P.S.

    If you're not busy and would like to help me, maybe you can help me through Pm or maybe another platform like skype. I believe it would be easier and faster.
    Last edited by DomantasB; 13th November 2013 at 15:07.

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with Qnetwork cookies and getting code after login

    Quote Originally Posted by DomantasB View Post
    I call
    Qt Code:
    1. ndlink();
    To copy to clipboard, switch view to plain text mode 
    from main.cpp file.
    How did you make sure to do that after the post has succeeded?

    You don't seem to emit any signal in the slot connected to finished().

    After your post I also tried to call it in a consturctor but it gives same resullts.
    In the constructor of what?

    My recommendation is to follow your own idea, i.e. you posted in NOv 7tth
    Quote Originally Posted by DomantasB View Post
    So if i understand you correctly i should remove those to gets and put them in different function. After i run function with POST and it finishes I should run
    function with gets?
    Not sure why you are trying to do something different now, that sounded right.

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 30th October 2013, 13:45
  2. Replies: 1
    Last Post: 9th January 2012, 07:26
  3. Login Problem
    By srichand in forum Qt Programming
    Replies: 1
    Last Post: 9th May 2008, 12:37

Tags for this Thread

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.