PDA

View Full Version : problems with Qhttp.



Con Nít
25th February 2011, 10:08
I have 2 file php below:
login.php

<?
session_start();
?>
<FORM METHOD=POST>
Password : <Input type = 'TEXT' Name ='password'> <INPUT TYPE=SUBMIT NAME="submit" VALUE="submit">
</FORM>
<?
if(isset($_POST['submit'])){
if(($_POST['password']) == "123456"){
$_SESSION['login'] = "ADMIN";
}else{
$_SESSION['login'] = NULL;
}
?>
<SCRIPT languages="javascript">
window.location = 'index.php';
</SCRIPT>
<?
}
?>
index.php

<?php
session_start();
if(isset($_SESSION['login'])){
echo "ok";
}else{
echo "n-ok";
}
?>
this is the class to send Post to login.php

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QHttp>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
QHttp *http;
QHttp *secondHttp;
QString cookie_value;
private slots:
void readResponseHeader(const QHttpResponseHeader &);
void readSecondResponseHeader(const QHttpResponseHeader &);
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
http = new QHttp("localhost", 80);
secondHttp = new QHttp("localhost", 80);
connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
connect(secondHttp, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),this, SLOT(readSecondResponseHeader(const QHttpResponseHeader &)));
QByteArray content = "";
content.append("password");
content.append("=");
content.append("123456");
content.append("&");
content.append("submit");
content.append("=");
content.append("submit");
QHttpRequestHeader header("POST", "/test/login.php");
header.setValue("Host", "localhost");
header.setContentType("application/x-www-form-urlencoded");
header.setContentLength(content.length());
http->request(header,content);
}

void MainWindow::readResponseHeader(const QHttpResponseHeader &responseHeader){
cookie_value = responseHeader.value("Set-Cookie");
cookie_value = cookie_value.section(";", 0, 0);
ui->textEdit->append(responseHeader.toString());

QHttpRequestHeader header("GET", "/test/index.php");
header.setValue("Host", "localhost");
header.setValue("Cookie", cookie_value);
header.setContentType("application/x-www-form-urlencoded");
secondHttp->request(header);
}

void MainWindow::readSecondResponseHeader(const QHttpResponseHeader &responseHeader){
ui->textEdit->append(responseHeader.toString());
}

MainWindow::~MainWindow()
{
delete ui;
delete http;
delete secondHttp;
}


The second time when send "GET" to index.php attach session code, run good
The first time send request sever response :

HTTP/1.1 200 OK
Date: Fri, 25 Feb 2011 09:05:42 GMT
Server: Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i PHP/5.2.9
X-Powered-By: PHP/5.2.9
Set-Cookie: PHPSESSID=2595a2b9578d54555be352faaa673feb; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 214
Content-Type: text/html
The second time send request sever response :

HTTP/1.1 200 OK
Date: Fri, 25 Feb 2011 09:05:43 GMT
Server: Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i PHP/5.2.9
X-Powered-By: PHP/5.2.9
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 2
Content-Type: text/html
But my isuisse is: how to get the content of index.php after send request
(the content is ok or n-ok)

Con Nít
26th February 2011, 12:15
somebody help me.
thanks.

Added after 26 minutes:

I have solved my problem.

squidge
26th February 2011, 15:03
Another thing that comes to mind is why you are not using the cookie jar.

Con Nít
6th May 2011, 09:47
Another thing that comes to mind is why you are not using the cookie jar.

In PHP, I can do that with CURLOPT_COOKIEJAR option, But I don't know how to do that with HTTP protocol, Could U edit directly in demo for me?

squidge
6th May 2011, 13:16
In Qt, you would use the QNetworkCookieJar class: http://doc.trolltech.com/latest/qnetworkcookiejar.html

Con Nít
9th May 2011, 11:52
I use responseHeader.allValues("Set-Cookie"); to get list cookie string type. How can I foist a list of cookies for the requests after that but no need use setValue instead of use QNetworkCookieJar.
I don't understand QNetworkCookieJar's activity.
Thanks for your help.

ChrisW67
9th May 2011, 12:01
You set the cookie jar on your QNetworkAccessManager and forget about it... or at least you would if you were not using the obsolete QHttp class.