QT intercepts HTML requests (inherit Qnetworkaccessmanager and Qnetworkreply)

Source: Internet
Author: User

Qtwebkit loading HTML pages, there will be a lot of HTML requests, such as and so on, if you want to intercept these requests, then customize the answer ...

1. Define a class that inherits Qnetworkaccessmanager (request) #ifndef Networkaccessmanager_h#define networkaccessmanager_h  #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkProxy> #include <qtnetwork/ qnetworkreply> #include <qtnetwork/qnetworkrequest> class networkaccessmanager:public Qnetworkaccessmanager{     q_object public:    explicit Networkaccessmanager (Qnetworkaccessmanager *manager, Qobject *parent); protected:     Qnetworkreply * Createrequest (qnetworkaccessmanager::operation operation,                        const Qnetworkrequest &request, Qiodevice *device);};   #endif//networkaccessmanager_h  #include "networkaccessmanager.h"   #include <QtCore/QDebug>  networkaccessmanager::networkaccessmanager (Qnetworkaccessmanager *manager, QObject *Parent)     : Qnetworkaccessmanager (parent) {    setcache (manager-> Cache ());     setcookiejar (Manager->cookiejar ());     setproxy ( Manager->proxy ());     setproxyfactory (Manager->proxyfactory ());}  qnetworkreply * Networkaccessmanager::createrequest (qnetworkaccessmanager::operation operation,                    const Qnetworkrequest &request, Qiodevice *device) {    if (Request.url (). Scheme ()! = "http")              return Qnetworkaccessmanager::createrequest ( operation, request, device);     else     return new blobreply ( Request.url);} 2. Define a class, Inherit qnetworkreply (answer) #ifndef blobreply_h#define blobreply_h  #include <QtNetwork/QNetworkReply>  class BlObreply:public qnetworkreply{    q_object public:    blobreply ( Const Qurl &url);     void abort ();  protected:    qint64 ReadData ( Char *data, Qint64 maxSize); };  #endif//blobreply_h  #include "blobreply.h"   #include <qtcore/ Qdebug> blobreply::blobreply (const qurl &url)     : qnetworkreply () {     qdebug () << url.tostring ();}  void Blobreply::abort () { } qint64 blobreply::readdata (char *data, Qint64 maxSize) {     return-1;} 3. When initializing the WebView, instantiate the above Qnetworkaccessmanager *oldmanager = Ui->webview->page ()->networkaccessmanager () ; Networkaccessmanager *newmanager = new Networkaccessmanager (Oldmanager, this); Ui->webview->page () Setnetworkaccessmanager (Newmanager); Ui->webview->page ()->setforwardunsupportedcontent (true);  If you have technical questions, please consult blogger Qq... http://blog.sina.Com.cn/s/blog_671732440100y3xx.html

Qt intercepts HTML requests (inherit Qnetworkaccessmanager and qnetworkreply)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.