PDA

View Full Version : Problem with Qnetwork cookies and getting code after login



DomantasB
30th October 2013, 13:50
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.



QNetworkAccessManager *manager;
manager = new QNetworkAccessManager ();
QNetworkRequest req;

address =("www.example/index.php");

manager->setCookieJar(new QNetworkCookieJar);

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");
req.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
req.setUrl(QUrl("www.example.com/login"));

QByteArray postData;

postData.append("login_user=user&");
postData.append("login_passwd=Pass");

connect (manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(writeToTxt(QNetworkReply *)));

manager->post(req, postData);
manager->get(QNetworkRequest(address));
manager->get(QNetworkRequest(QUrl("www.example.com/example.html")));


void HtmlSource::writeToTxt(QNetworkReply* reply)
{
qDebug() << "atsakymas" << endl;
if(reply->isOpen()){
//qDebug() << reply->readAll();
QFile file( "1.txt" );
if ( file.open(QIODevice::ReadWrite) )
{
file.write(reply->readAll());
file.close();

}}}

DomantasB
2nd November 2013, 17:51
Annyone? I still need help guys. I just don't know where to start searching for problem. What I'am doing wrong

anda_skoa
3rd November 2013, 13:27
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,
_

DomantasB
7th November 2013, 13:57
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?

anda_skoa
7th November 2013, 14:34
Exactly!

Cheers,
_

DomantasB
11th November 2013, 20:30
Well it doesn't work.

I created new function


void HtmlSource::ndlink(){
manager->get(QNetworkRequest(QUrl("https://www.tamo.lt/dienynas1.html?s=namu_darbai")));
}

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


HtmlSource::HtmlSource(QObject *parent) : QObject(parent)
{
qDebug() << "-----------here is constructor";
manager = new QNetworkAccessManager ();
address =("https://www.tamo.lt/dienynas1.html?s=namu_darbai/");
QNetworkRequest req;

manager->setCookieJar(new QNetworkCookieJar);
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");
req.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
req.setRawHeader("Connection", "Keep-Alive");
req.setRawHeader("Accept-Charset", "WINDOWS-1257,utf-8;q=0.7,*;q=0.7");
req.setRawHeader("Cookie", "lang=lt");
req.setUrl(QUrl("https://www.tamo.lt/dienynas1.html?s=namu_darbai/"));
QByteArray postData;
postData.append("login_user=login&");
postData.append("login_passwd=pasew");

connect (manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(write(QNetworkReply*)));
manager->post(req, postData);
}

void HtmlSource::write(QNetworkReply* reply)
{
qDebug() << "atsakymas" << endl;
if(reply->isOpen()){

QFile file( "Ge2as1.txt" );

if ( file.open(QIODevice::ReadWrite) )
{
qDebug() << reply->readAll();
file.write(reply->readAll());
file.close();

}

}
}


void HtmlSource::ndlink(){

qDebug() << "-------------- here starts get function" << endl<< endl<< endl<< endl<< endl<< endl<< endl<< endl;

manager->get(QNetworkRequest(QUrl("https://www.tamo.lt/dienynas1.html?s=namu_darbai")));

}

anda_skoa
12th November 2013, 12:04
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,
_

DomantasB
13th November 2013, 16:00
first of all i would like to thank you for helping me so much.

I call
ndlink(); 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.

anda_skoa
14th November 2013, 11:21
I call
ndlink(); 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

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,
_