Click here to Skip to main content
12,065,756 members (32,796 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VC8.0 C++
While using setHost statement, It retuns error "undefined reference".
How to download a file using HTTP with URL?


QUrl url = QUrl::fromUserInput(strUrl);
QFileInfo fileInfo(url.path());
QString fileName=fileInfo.fileName();
if (QFile::exists(fileName)) {
   QMessageBox::information(this, tr("HTTP"),
   tr("There already exists a file called %1 in "
   "the current directory.")
   .arg(fileName));
}
 
file = new QFile(fileName);
if (!file->open(QIODevice::WriteOnly)) {
QMessageBox::information(this, tr("HTTP"),
tr("Unable to save the file %1: %2.")
.arg(fileName).arg(file->errorString()));
delete file;
file = 0;
}
 
file.setFileName(fileName);
file.open(QIODevice::WriteOnly);
QHttp *http;
http=new QHttp(this);
setHost(url.host(),QHttp::ConnectionModeHttp);
Posted 7-Mar-11 2:30am
Edited 7-Mar-11 5:14am
v2
Comments
HimanshuJoshi 7-Mar-11 10:15am
   
Edited to add <pre> tags around your code
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

The QHttp class used in your code is obsolete[^]. New code should use the QNetworkAccessManager[^] class instead. See the example code[^] in the Qt documentation.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Simplest way to download from the HTTP is below::
connect(&http,SIGNAL(done(bool)),this,SLOT(httpdon e())); //added in the constructor..

In the header file......Make the following changes...

QHttp http;
QFile file;

private slots:
bool on_pushButton_clicked();
void httpdone();

signals:
void done();

in the Qthttp.cpp file...

bool QtHttp:n_pushButton_clicked()
{
 
QString strUrl="http://www.blabla.com//file";
QUrl url = QUrl::fromUserInput(strUrl);
 
QFileInfo fileInfo(url.path());
QString strhost=url.encodedHost();
QString filename=fileInfo.fileName();
file.setFileName("C:\\Qt\\QHttp\\"+filename);
http.setHost(url.host(),url.port(80));
http.get(url.path(),&file);
 
if(!file.open(QIODevice::WriteOnly))
{
QMessageBox::warning(NULL,"warning","file is not opened",QMessageBox::Ok);
}
file.write(http.readAll());
http.close();
return true;
 
}
 
void QtHttp::httpdone()
{
 
file.close();
Q_EMIT done();
}
  Permalink  
v2
Comments
Kozlov_Sergey 22-Mar-11 4:46am
   
Edited for code tags.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160207.1 | Last Updated 22 Mar 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100