How to read POST data "sent" from my own QtWebKit application?

793 views Asked by At

How can I read POST data "sent" from my own QtWebKit application? I am developing a small hybrid QWebKit application, which uses HTML forms for user input, than executes local Perl scripts and displays the final result. Nothing is actually sent to or retrieved from servers and no actual network communication is performed; HTML forms are just a local interface for scripts.

I already tried a solution inspired by answers from
Piotr Dobrogost "How to tell QWebPage not to load specific type of resources?" and
Fèlix Galindo Allué "Get raw packet data from Qt application":

# peb.h:
#include <QApplication>
#include <QUrl>
#include <QtWebKit/QWebPage>
#include <QtWebKit/QWebFrame>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QDebug>

class NAM : public QNetworkAccessManager {

    Q_OBJECT

protected:

    virtual QNetworkReply * createRequest ( Operation op,
                                            const QNetworkRequest &req,
                                            QIODevice *outgoingData = 0 ) {
        qDebug() << "Trying to read POST data...";
        QByteArray outgoingByteArray = outgoingData -> readAll();
        QString postData ( outgoingByteArray );
        qDebug() << "POST data" << postData;
        return QNetworkAccessManager::createRequest ( op, req );
    }
};

#peb.cpp
TopLevel::TopLevel()
    : QWebView ( 0 )
{
    main_page = new Page();
    setPage ( main_page );

    NAM *nam = new NAM();
    main_page -> setNetworkAccessManager ( nam );

My program crashes without even displaying GUI after debug message "Trying to read POST data..." Most probably the problem lies within the line:

QByteArray outgoingByteArray = outgoingData -> readAll(); 

I also found and read the following Stack Overflow questions:
1. "QNetworkReply - connection established, first byte written, etc",
2. "QtWebKit QWebPage::acceptNavigationRequest and POST data",
3. "QNetworkAccessManager read outgoingData and keep it in QIODevice",
as well as an interesting post on qt-project.org forums:
4. "[Solved] QNetWorkRequest"
but none of the answers provided me with a working solution.
If no easier solution is found (without external classes), probably will test the Grantlee::Tee solution, proposed by Piotr Dobrogost in question Nr. 3.

I would like to avoid, if possible, Webkit Bridge solutions like Qt Form Extractor Example, which makes my application dependent on inserting specific JavaScript code in each and every HTML form (not to speak that many forms are submitted using jQuery / AJAX and they expect a normal POST capability from the browser).

Every help, advice, information and especially pieces of working code will be highly appreciated!

1

There are 1 answers

0
Ahmad On

I think this code will help to catch posted data and show it in message box -- you will reuse outgoing data too -- as you will see :

class netaccess : public QNetworkAccessManager
{
    Q_OBJECT
public:
netaccess() {}
virtual ~netaccess() {}

QNetworkReply * createRequest(Operation op, const QNetworkRequest &req, QIODevice * outgoingData = 0){

  if(outgoingData){

 QByteArray barr = outgoingData->readAll();

    QBuffer * buff = new QBuffer(&barr);

    if (PostOperation == op){
 QMessageBox::information(NULL,"",QString(barr));

buff->open(QIODevice::ReadOnly);
  QNetworkReply * rep = QNetworkAccessManager::createRequest    (op,req,buff);
 buff->setParent(rep);
return  rep;

    }

}



   QNetworkReply * rep = QNetworkAccessManager::createRequest(op,req,NULL);

    return  rep;
}