Results 1 to 4 of 4

Thread: How to use QNetworkAccesManager ??

  1. #1
    Join Date
    Feb 2013
    Location
    Banzart
    Posts
    54
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default How to use QNetworkAccesManager ??

    Is there any tutorials or example for how to use QNetworkAccessManager with QAuthenticator and QNetworkRequest??
    My application contains a step where the user fill in his login and password and connect to a server .
    I didn't find too much on this subject on the web .Any help please ??
    Last edited by sliverTwist; 28th March 2013 at 11:30.
    Sliver_Twist

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to use QNetworkAccesManager ??

    It's about 5 lines of code.

    Connect QNetworkAccessManager::authenticationRequired() to a slot. When the slot is called insert the user name and password matching the requesting site into the supplied QAuthenticator using setUser() and setPassword(). Job done.

  3. #3
    Join Date
    Feb 2013
    Location
    Banzart
    Posts
    54
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to use QNetworkAccesManager ??

    Quote Originally Posted by ChrisW67 View Post
    It's about 5 lines of code.

    Connect QNetworkAccessManager::authenticationRequired() to a slot. When the slot is called insert the user name and password matching the requesting site into the supplied QAuthenticator using setUser() and setPassword(). Job done.
    Please could you give me a code example which is verified because i tried that and didn't work for me.
    Sliver_Twist

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to use QNetworkAccesManager ??

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QNetworkAccessManager>
    3. #include <QNetworkRequest>
    4. #include <QNetworkReply>
    5. #include <QAuthenticator>
    6. #include <QDebug>
    7.  
    8. static const QUrl url("http://test-host/");
    9. static const char *user = "user";
    10. static const char *password = "password";
    11.  
    12. class Downloader: public QObject
    13. {
    14. Q_OBJECT
    15.  
    16. QNetworkAccessManager nam;
    17. QNetworkReply *reply;
    18.  
    19. public:
    20. Downloader(QObject * p=0): reply(0) {
    21. connect(&nam,
    22. SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
    23. SLOT(authRequired(QNetworkReply*,QAuthenticator*)));
    24. }
    25.  
    26. void run() {
    27. QNetworkRequest req(url);
    28. reply = nam.get(req);
    29. connect(reply, SIGNAL(finished()), SLOT(done()));
    30. }
    31.  
    32. public slots:
    33. void authRequired(QNetworkReply *reply, QAuthenticator *authenticator) {
    34. qDebug() << Q_FUNC_INFO << authenticator->realm();
    35. // get user name and password from user for authenticator->realm()
    36. // if (not cancelled) set the details otherwise don't
    37. authenticator->setUser(user);
    38. authenticator->setPassword(password);
    39. }
    40.  
    41. void done() {
    42. qDebug() << reply->readAll();
    43. qApp->quit();
    44. }
    45. };
    46.  
    47. int main(int argc, char **argv) {
    48. QCoreApplication app(argc, argv);
    49. Downloader d;
    50. d.run();
    51. return app.exec();
    52. }
    53.  
    54. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    Please be aware that this is how you handle the web server requesting authentication. This has nothing to do with handling web-applications requesting credentials: each of those is different but generally some form of HTML form.

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.