PDA

View Full Version : QNetworkAccessManager authenticationRequired slot doesn't get called



prasad.borkar
27th April 2011, 09:28
Hi,

I want to access site using QNetworkAccessManager which requires authentication.
I have written sample code to login to particular site by using Post method but its not working.
Refer below code:


#include "tikonalogin.h"
#include "ui_tikonalogin.h"

tikonalogin::tikonalogin(QWidget *parent) :
QDialog(parent),
ui(new Ui::tikonalogin)
{
ui->setupUi(this);
acc = new QNetworkAccessManager(this);

connect(acc,SIGNAL(finished(QNetworkReply*)),this, SLOT(accFinish(QNetworkReply*)));

connect(acc,SIGNAL(authenticationRequired(QNetwork Reply*,QAuthenticator*)),this,SLOT(authReq(QNetwor kReply*,QAuthenticator*)));
}

tikonalogin::~tikonalogin()
{
delete ui;
}

void tikonalogin::on_goButton_clicked()
{

QNetworkRequest request;
QUrl url ("https://www.whatever.in/login.do");

QByteArray data;
QUrl param;
param.addQueryItem("type","2");
param.addQueryItem("username","abc");
param.addQueryItem("password","123");
data = param.encodedQuery();
// QString typ,username,password;
// typ="2";
// username="abc";
// password="123";

//request.setRawHeader("Authorization","Basic "+QByteArray(QString("%1:%2:%3").arg(typ).arg(username).arg(password).toAscii().t oBase64()));
request.setUrl(url);
reply = acc->post(request,data);
//reply = acc->get(request);

connect(reply,SIGNAL(readyRead()),this,SLOT(readda ta()));
}

void tikonalogin::accFinish(QNetworkReply *rep)
{
qDebug() << "Finished";
}

void tikonalogin::readdata(){
qDebug() << "Inside readdata()";
QByteArray arr = reply->readAll();
QString s = arr.data();
// qDebug() << s;
// ui->txDetails->setText(s);
}

void tikonalogin::authReq(QNetworkReply *, QAuthenticator *a){
qDebug() << "Authenticating";
a->setOption("type",2);
a->setUser("1103544735");
a->setPassword("Tech1234");
}



Please help me on this.
Thanks in advance.

high_flyer
28th April 2011, 08:50
"Not working" is very general.
Many things can be "not working".
Debug your code, and find the bit of code which is "not working".
Then we can look in to why it is "not working".

ChrisW67
28th April 2011, 09:00
It won't get called unless the web server returns an HTTP status 401. This in unlikely if you are passing valid credentials appropriate to the web site/page in the headers/payload of the request. You seem to be doing that above.

prasad.borkar
29th April 2011, 11:27
Hi High_flyer,

What are you replying man. I dont know thats why i am posting a question.
If you know then reply with some valid answer or else don't...
Obviously i am debugging it, but i am not getting the solution.

high_flyer
29th April 2011, 13:44
Don't you get it that I am not your foe but your friend?
When you post a question, and you want help, you should help others help you.
Show and mention what you have done to pint point the problem.

Obviously i am debugging it
Not obvious from what you have posted.
It would have been obvious if you had said which part of the code does work as you expect, which doesn't, and what have you done so far for debugging.
This way potential helpers can know in advance what you have tried, and not suggest it again, and think in lines that are better suited to solving your problem.