PDA

View Full Version : Login authorization



Higgs
16th April 2014, 10:20
Hi all.
I have writen code which downloads simple file. html file.
code is here:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileInfo>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
url.setUrl("http://google.ge/");
ui->lineEdit->setText(url.toString());

connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(downloadBut ton()));
}

MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::downloadButton()
{
url.setUrl(ui->lineEdit->text());
request.setUrl(url);
QFileInfo fileInfo = url.path();
QString fileName = fileInfo.fileName();
if(fileName.isEmpty())
fileName = "index.html";

file = new QFile(fileName);
file->open(QIODevice::WriteOnly);

startRequest();
}
void MainWindow::startRequest()
{
reply = nmanager.get(request);
connect(reply,SIGNAL(finished()),this,SLOT(httpFin ished()));
connect(reply,SIGNAL(readyRead()),this,SLOT(httpRe ady()));
//QMessageBox::information(this,"ads","asd");
}
void MainWindow::httpFinished()
{
reply->deleteLater();
reply = 0;
file->close();
file = 0;
}
void MainWindow::httpReady()
{
file->write(reply->readAll());
}


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.

ChrisW67
17th April 2014, 00:07
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:

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.
An HTTP GET to fetch the resource you wanted.

Higgs
17th April 2014, 16:30
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:

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.
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 :)