PDA

View Full Version : QHttp : Strange behavior with different host!



Ferdous
9th September 2008, 12:57
I've written a class that downloads a single file. Surprisingly, I've noticed that the program successfully downloads one file but fails for another file that has a different host.

I'm curious to know the reason. Please tell me if you can figure out.

Here are my classes:

myhttp.h file:


#ifndef MYHTTP_H
#define MYHTTP_H

#include <QObject>
#include <QHttp>
#include <QFile>

class MyHttp : public QObject
{
Q_OBJECT

private:
QHttp http;
QFile file;

public:
MyHttp(const QString& strFilePath, QObject* parent = 0);

signals:
void finished();

private slots:
void myHttpDone(bool error);
};

#endif


myhttp.cpp file:


#include <QtNetwork>

#include <QFileInfo>
#include <QUrl>

#include "MyHttp.h"

MyHttp::MyHttp(const QString& strFilePath, QObject* parent /* = 0 */) : QObject(parent)
{
connect(&http, SIGNAL(done(bool)), this, SLOT(myHttpDone(bool)));

QUrl url(strFilePath);

QFileInfo fileInfo(url.path());
file.setFileName(fileInfo.fileName());
file.open(QIODevice::WriteOnly);

http.setHost(url.host(), url.port(80));
http.get(url.path(), &file);
http.close();
}

void MyHttp::myHttpDone(bool error)
{
file.close();

if (error == true)
{
file.remove();
}

emit finished();
}

main.cpp file:


#include <QCoreApplication>

#include "MyHttp.h"

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

// program doesn't work for this link
// MyHttp file("http://thedailystar.net/images/tdsmainlogo.jpg");

// program works for this link
MyHttp file("http://ahmadferdous.googlepages.com/Ferdous.JPG");

QObject::connect(&file, SIGNAL(finished()), &app, SLOT(quit()));

return app.exec();
}