Results 1 to 3 of 3

Thread: Login authorization

  1. #1
    Join Date
    May 2013
    Location
    Georgia,Tbilisi
    Posts
    32
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Login authorization

    Hi all.
    I have writen code which downloads simple file. html file.
    code is here:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QFileInfo>
    4. #include <QMessageBox>
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. url.setUrl("http://google.ge/");
    11. ui->lineEdit->setText(url.toString());
    12.  
    13. connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(downloadButton()));
    14. }
    15.  
    16. MainWindow::~MainWindow()
    17. {
    18. delete ui;
    19. }
    20. void MainWindow::downloadButton()
    21. {
    22. url.setUrl(ui->lineEdit->text());
    23. request.setUrl(url);
    24. QFileInfo fileInfo = url.path();
    25. QString fileName = fileInfo.fileName();
    26. if(fileName.isEmpty())
    27. fileName = "index.html";
    28.  
    29. file = new QFile(fileName);
    30. file->open(QIODevice::WriteOnly);
    31.  
    32. startRequest();
    33. }
    34. void MainWindow::startRequest()
    35. {
    36. reply = nmanager.get(request);
    37. connect(reply,SIGNAL(finished()),this,SLOT(httpFinished()));
    38. connect(reply,SIGNAL(readyRead()),this,SLOT(httpReady()));
    39. //QMessageBox::information(this,"ads","asd");
    40. }
    41. void MainWindow::httpFinished()
    42. {
    43. reply->deleteLater();
    44. reply = 0;
    45. file->close();
    46. file = 0;
    47. }
    48. void MainWindow::httpReady()
    49. {
    50. file->write(reply->readAll());
    51. }
    To copy to clipboard, switch view to plain text mode 

    I request page,that requires login autorization, username: and password:
    I want my code to type automatically this fields(username and password) and then download page,which is after login autorization.

    How can I do it?

    p.s sorry fo my bad English.

  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: Login authorization

    It depends on how the server is demanding a username and password.

    If you try to fetch a page and get an HTTP response code 401 Unauthorized (possibly shows as QNetworkReply::ContentAccessDenied in your error handling slot) then you can probably just use QUrl::setUserName() and QUrl::setPassword() before you create and send your QNetworkRequest.

    If you mean the server sends you to a separate HTML page that has a form requesting user name and password then you need to make two separate requests in sequence:
    1. An HTTP POST (or, less likely, GET) to the form's submit URL with encoded data to mimic submitting the authentication form. You will need to arrange keeping any cookies the server sends in the response.
    2. An HTTP GET to fetch the resource you wanted.

  3. #3
    Join Date
    May 2013
    Location
    Georgia,Tbilisi
    Posts
    32
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Login authorization

    Quote Originally Posted by ChrisW67 View Post
    It depends on how the server is demanding a username and password.

    If you try to fetch a page and get an HTTP response code 401 Unauthorized (possibly shows as QNetworkReply::ContentAccessDenied in your error handling slot) then you can probably just use QUrl::setUserName() and QUrl::setPassword() before you create and send your QNetworkRequest.

    If you mean the server sends you to a separate HTML page that has a form requesting user name and password then you need to make two separate requests in sequence:
    1. An HTTP POST (or, less likely, GET) to the form's submit URL with encoded data to mimic submitting the authentication form. You will need to arrange keeping any cookies the server sends in the response.
    2. An HTTP GET to fetch the resource you wanted.
    Thanks for your reply.
    I for example I have account on the qtcentre.org,and I want my program to enter qtcentre.org,type username and password,and for example,get number of posts I have made so far.
    How can I do this?

    I'm a bit confused

Similar Threads

  1. Network Login
    By Vivek1982 in forum Newbie
    Replies: 3
    Last Post: 11th October 2013, 18:49
  2. Authorization on Mac
    By anjul.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 4th March 2013, 21:12
  3. Login Screen GUI
    By augusbas in forum Qt Programming
    Replies: 17
    Last Post: 18th December 2009, 07:20
  4. QTcpServer + Authorization
    By NoRulez in forum Qt Programming
    Replies: 2
    Last Post: 27th May 2008, 15:04

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.