我把书上的例子简化写了个程序,下载HTTP图片,经测试结果正常。
主要代码如下:
.h头文件:
class HttpGet : public QObject
{
Q_OBJECT
public:
HttpGet(QObject *parent = 0);
bool getFile(const QUrl &url);
signals:
void done();
private slots:
void httpDone(bool error);
private:
QHttp http;
QFile file;
};
.cpp源文件:
HttpGet::HttpGet(QObject *parent)
: QObject(parent)
{
connect(&http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
}
bool HttpGet::getFile(const QUrl &url)
{
QString localFileName = "./Images/"+QFileInfo(url.path()).fileName();
file.setFileName(localFileName);
http.setHost(url.host(), url.port(80));
http.get(url.path(), &file);
http.close();
return true;
}
void HttpGet::httpDone(bool error)
{
if (error) {
std::cerr << "Error: " << qPrintable(http.errorString())
<< std::endl;
} else {
std::cerr << "File downloaded as "
<< qPrintable(file.fileName()) << std::endl;
}
file.close();
emit done();
}
main.cpp中
调用getFile("http://........")图片成功下载。
——————————————————————————————————————————————
我把这个同样的类移入我的项目中(代码只改动filename部分),编译没有报错,但是当调用哪个到:
http.setHost(url.host(), url.port(80));
int g = http.get(url.path(), &file);
就出问题了,HTTP给我返回了一个ErrorString:Request Aborted
查看路径下的文件夹,该图片文件已经建立,但是没有任何内容。
查看int g的值,返回值为6……
希望高手能帮帮忙,告诉我大概怎么回事,或者给我个提示看看该怎么找这个问题……我从昨晚一直调试到今天,没找到原因。
在url部分,我也直接放入相同的URL地址,独立于项目外的类可以下载图片,而移植到项目内的类就出这个错……
难道是我项目中哪些冲突了?根据HTTP返回的error string 这个错误大概是什么原因产生的??