Rok
Rok

Reputation: 2578

How to stop QHttp/QtWebKit from caching pages

Recently I found that I could modify a txt file that's stored online and when I then fetch it using QHttp I'm still getting the previous version. My code also used QtWebKit so maybe this relates to the problem somehow? Or maybe I'm just missing a setting that prevents caching?

(I'm using Qt 4.7.3 under Windows 7 and compiling with the Visual Studio 2010 Add-in)

I'm doing the following to prevent QtWebKit from caching anything -

QWebSettings::globalSettings( )->setAttribute( QWebSettings::PrivateBrowsingEnabled, true );
QWebSettings::globalSettings( )->setAttribute( QWebSettings::LocalContentCanAccessRemoteUrls, true );
QWebSettings::setMaximumPagesInCache( 0 );
QWebSettings::setObjectCacheCapacities( 0, 0, 0 );
QWebSettings::clearMemoryCaches( );

The code I'm using to load the txt file is as follows -

CacheHTML::CacheHTML( QObject* parent, QString urlId, QByteArray* bytes, QString stringUrl ) : QObject( parent ) {
    this->urlId = urlId;
    QUrl url = QUrl( stringUrl );
    http = new QHttp( this );
    connect( http, SIGNAL( requestFinished( int, bool )), this, SLOT( Loaded( int, bool )));
    pBytes = bytes;
    buffer = new QBuffer( pBytes, this );
    buffer->open( QIODevice::WriteOnly );
    http->setHost( url.host( ));
    fetchId = http->get( url.toString( ), buffer );
}

The slot I'm using to correspond with this is -

void CacheHTML::Loaded( int id, bool error ) {
    if( id != fetchId )
        return;
    if( error ) {
        emit CacheComplete( urlId, false );
        return;
    }
    emit CacheComplete( urlId, true );
}

But once it's fetched the txt file from the internet it seems to be caching it somewhere/somehow. No matter what changes I make in the txt file, when I run my code it still buffers the original file.

I've tried OrcunC's suggestion (below) but this results in receiving an empty string as opposed to the contents of the txt file -

CacheHTML::CacheHTML( QObject* parent, QString urlId, QByteArray* bytes, QString stringUrl ) : QObject( parent ) {
    this->urlId = urlId;
    QUrl url = QUrl( stringUrl );
    http = new QHttp( this );
    connect( http, SIGNAL( requestFinished( int, bool )), this, SLOT( Loaded( int, bool )));
    pBytes = bytes;
    buffer = new QBuffer( pBytes, this );
    buffer->open( QIODevice::WriteOnly );

    QHttpRequestHeader header( "GET", url.path( ));
    header.setValue( "Cache-Control", "no-cache" );
    header.setValue( "Host", url.host( ));

    http->setHost( url.host( ));
    fetchId = http->request( header );
}

I've also tried implementing it in a different way as show below, but got the same results as if the txt file is being cached. Even though checking the QNetworkReply in the finished slot with bool fromCache = reply->attribute( QNetworkRequest::SourceIsFromCacheAttribute ).toBool( ) returned false.

    QNetworkAccessManager* manager = new QNetworkAccessManager( this );
    connect( manager, SIGNAL( finished( QNetworkReply* )), this, SLOT( Loaded( QNetworkReply* )));
    QNetworkRequest req = QNetworkRequest( QUrl( stringUrl ));
    req.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork );
    req.setAttribute( QNetworkRequest::CacheSaveControlAttribute, false );
    manager->get( req );

I'm at my wit's end now so would be very grateful for anyone pointing me in the right direction.

Upvotes: 3

Views: 4781

Answers (2)

kralyk
kralyk

Reputation: 4387

This might seem like a stupid suggestion, but... have you tried loading up the txt in a web browser? And checking if it updates changes? The problem might be somewhere on the way, not in your app at all...

Upvotes: 0

O.C.
O.C.

Reputation: 6819

Probably a proxy is caching the file content. Try something as below to clear the cache:

QHttpRequestHeader header("GET", "/mytext.txt");
header.setValue("Cache-Control", "no-cache");
header.setValue("Host", "myhost.com");

http->setHost("myhost.com");
fetchId = http->request(header);

Upvotes: 1

Related Questions