How get all HTTP request send by Qt WebKit



  • I want get all HTTP request which is send by my application.
    I try to inherit the QNetworkAccessManager .Then I use QWebPage->setNetworkAccessManager().
    But it does not work...............How could I do?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You'll have to be a bit more specific, what doesn't work ?



  • [quote author="SGaist" date="1375788633"]Hi and welcome to devnet,

    You'll have to be a bit more specific, what doesn't work ?[/quote]

    Thanks.I will paste my code.
    It is my Widget
    @#include <QNetworkRequest>
    #include <QWebFrame>

    Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
    {
    ui->setupUi(this);
    netWorkAccessManager = new MyNetWorkAccessManager();
    QNetworkRequest *request = new QNetworkRequest(QUrl("http://www.baidu.com"));
    // netWorkAccessManager->get(*request);
    QWebPage *page = new QWebPage();
    page->setNetworkAccessManager(netWorkAccessManager);
    ui->webView->setPage(page);
    ui->webView->load(*request);
    }

    Widget::~Widget()
    {
    delete ui;
    }@

    It is my NetWorkAccessManager
    @#include "mynetworkaccessmanager.h"
    #include <QDebug>
    #include <QNetworkAccessManager>

    MyNetWorkAccessManager::MyNetWorkAccessManager()
    {
    }

    QNetworkReply* MyNetWorkAccessManager::deleteResource(const QNetworkRequest &request)
    {
    qDebug("1");
    return this->QNetworkAccessManager::deleteResource(request);
    }

    QNetworkReply* MyNetWorkAccessManager::get(const QNetworkRequest &request)
    {
    qDebug("2");
    return this->QNetworkAccessManager::get(request);
    }

    QNetworkReply* MyNetWorkAccessManager::head(const QNetworkRequest &request)
    {
    qDebug("3");
    return this->QNetworkAccessManager::head(request);
    }

    QNetworkReply* MyNetWorkAccessManager::post(const QNetworkRequest &request, QIODevice *data)
    {
    qDebug("4");
    return this->QNetworkAccessManager::post(request,data);
    }

    QNetworkReply* MyNetWorkAccessManager::post(const QNetworkRequest &request, const QByteArray &data)
    {
    qDebug("5");
    return this->QNetworkAccessManager::post(request,data);
    }

    QNetworkReply* MyNetWorkAccessManager::post(const QNetworkRequest &request, QHttpMultiPart *multiPart)
    {
    qDebug("6");
    return this->QNetworkAccessManager::post(request,multiPart);
    }

    QNetworkReply* MyNetWorkAccessManager::put(const QNetworkRequest &request, const QByteArray &data)
    {
    qDebug("7");
    return this->QNetworkAccessManager::put(request,data);
    }

    QNetworkReply* MyNetWorkAccessManager::put(const QNetworkRequest &request, QIODevice *data)
    {
    qDebug("8");
    return this->QNetworkAccessManager::put(request,data);
    }

    QNetworkReply* MyNetWorkAccessManager::put(const QNetworkRequest &request, QHttpMultiPart *multiPart)
    {
    qDebug("9");
    return this->QNetworkAccessManager::put(request,multiPart);
    }

    QNetworkReply* MyNetWorkAccessManager::sendCustomRequest(const QNetworkRequest &request, const QByteArray &verb, QIODevice *data)
    {
    qDebug("10");
    return this->QNetworkAccessManager::sendCustomRequest(request,verb,data);
    }
    @

    When I use @// netWorkAccessManager->get(*request);@
    The console print "2".
    But When I use @ui->webView->load(*request);@
    The console did not print anything.Why?
    And how could I get all Http request which send by my application?
    Thank you again.


  • Lifetime Qt Champion

    Don't do

    @load(*request);@

    It's a memory leak.

    You are setting your netWorkAccessManager on a QWebPage that has nothing to do with your QWebView, so your QWebView uses it's QNetworkAccessManager.

    Also remove all these this-> statement, you are calling the base class implementation not an element of your class.

    Hope it will get you further



  • [quote author="SGaist" date="1375870771"]Don't do

    @load(*request);@

    It's a memory leak.

    You are setting your netWorkAccessManager on a QWebPage that has nothing to do with your QWebView, so your QWebView uses it's QNetworkAccessManager.

    Also remove all these this-> statement, you are calling the base class implementation not an element of your class.

    Hope it will get you further[/quote]
    Thanks for your answer.It really get me further:)
    This is my test code.I just want to test whether the QWebView call MyNetWorkAccessManager`s method.
    OK.Let me describe what I want to do.
    At frist I wrote a very simple browser by using QWebView.
    Then I use the browser to surf the Internet.And I will get and store all the HTTP request which send by my browser.
    At last I can resend one of these HTTP request and make sure which DOM element will change after the HTTP response return.

    So,How could I get all the HTTP request which send by my browser?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.