How to download a file using HTTP with URL in QT?
Code:
QString fileName
=fileInfo.
fileName();
if (QFile::exists(fileName
)) { tr("There already exists a file called %1 in "
"the current directory.")
.arg(fileName));
}
file = new QFile(fileName
);
tr("Unable to save the file %1: %2.")
.arg(fileName).arg(file->errorString()));
delete file;
file = 0;
}
file.setFileName(fileName);
setHost
(url.
host(),
QHttp::ConnectionModeHttp);
Am getting while using the HTTP . It says undefined reference to the http object.
I just want to download a file from the host.
Re: How to download a file using HTTP with URL in QT?
What include files have you specified?
Re: How to download a file using HTTP with URL in QT?
I have included <QNetworkAccessManager>, <QHttp>
Re: How to download a file using HTTP with URL in QT?
And you have no other errors during compilation ? Do you have Qt += network in .pro file ?
Re: How to download a file using HTTP with URL in QT?
No. I dont have errors. And also no Qt +=network in my .pro file.
Re: How to download a file using HTTP with URL in QT?
Quote:
And also no Qt +=network in my .pro file
So add it;)
Re: How to download a file using HTTP with URL in QT?
Well if u want to work with network u have to have the Qt += network
Re: How to download a file using HTTP with URL in QT?
Yes, I have added it. Upto this everything is fine. Next is how to download that file.
Added after 1 4 minutes:
Here is the code:: Pls help me..
Code:
QString fileName
=fileInfo.
fileName();
if (QFile::exists(fileName
)) {
tr("There already exists a file called %1 in "
"the current directory.")
.arg(fileName));
}
file = new QFile(fileName
);
tr("Unable to save the file %1: %2.")
.arg(fileName).arg(file->errorString()));
delete file;
file = 0;
}
file.setFileName(fileName);
QObject::connect(&http,
SIGNAL(done
(bool)),
&loop,
SLOT(quit
()));
file.setFileName(filename);
http.setHost(url.host(), url.port(80));
http.
get(url.
toEncoded(QUrl::RemoveScheme |
QUrl::RemoveAuthority),
&file);
loop.exec();
Re: How to download a file using HTTP with URL in QT?
You should consider using QAccessManager instead since QHttp is obsolete and qt strongly recommends not using it in new code
Code:
QNetworkAccessManager* mNetworkManager = new QNetworkAccessManager(this);
QObject::connect(mNetworkManager,
SIGNAL(finished
(QNetworkReply
*)),
this,
SLOT(onNetworkReply
(QNetworkReply
*)));
QUrl url
= "http://someurl.com";
QNetworkReply* reply = mNetworkManager->get(QNetworkRequest(url));
void onNetworkReply(QNetworkReply* reply)
{
if(reply->error() == QNetworkReply::NoError)
{
int httpstatuscode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toUInt();
switch(httpstatuscode)
{
case RESPONSE_OK:
if (reply->isReadable())
{
//Assuming this is a human readable file replyString now contains the file
replyString
= QString::fromUtf8(reply
->readAll
().
data());
}
break;
case RESPONSE_ERROR:
case RESPONSE_BAD_REQUEST:
default:
break;
}
}
reply->deleteLater();
}
Re: How to download a file using HTTP with URL in QT?
It says incomplete type struct QNetworkRequest and lot more errors.. Could you pls help me..
Re: How to download a file using HTTP with URL in QT?
You need include
Code:
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkAccessManager>
in your cpp file
Re: How to download a file using HTTP with URL in QT?
It shows RESPONSE_ERROR was not declared in this scope..
Re: How to download a file using HTTP with URL in QT?
RESPONSE_OK, RESPONSE_ERROR, and RESPONSE_BAD_REQUEST are integers you need to define yourself depending on what status codes your webserver returns,
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
Re: How to download a file using HTTP with URL in QT?
Could you please explain the complete downloading of the file from the Host to the local machine?? please help me:confused:
Re: How to download a file using HTTP with URL in QT?
It's documented and even coded for you above by Berryblue031, which part don't you understand?
Re: How to download a file using HTTP with URL in QT?
No actually. its not getting saved in the QFile. How to save that and also QNetworkreply shows errors like.. invalid struct. declaration error.
Re: How to download a file using HTTP with URL in QT?
You need to be more descriptive.
Re: How to download a file using HTTP with URL in QT?
If you take a look at the documentation qnetworkreply->readAll().data() returns a qbytearray and QFile.write takes a qbytearray, so simply create your file like you did in your orignal code and read in the data from the qnetworkreply.
If you check the documentation this example http://doc.trolltech.com/4.7-snapshot/network-http.html does everything you are trying to do.
Re: How to download a file using HTTP with URL in QT?
File is created but empty one.. How to read it properly from http and write it to a file.
Added after 1 7 minutes:
QHttp http;
QFile file;
file.setFileName("C:\\Qt\\OCZToolbox\\"+fileName+" .vic");
//http.setSocket(&socket);
//header.setValue("HOST",strhost);
//QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
//socket.connectToHost(url.host(),80);
//http.setHost(url.host());
//int httpGetId = http.get(url.path(), &file);
connect (&http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
//QUrl &url;
http.setHost(url.host(),QHttp::ConnectionModeHttp) ;
http.get(url.path(),&file);
//http.request(header,0);
file.open(QIODevice::WriteOnly);
//file.write(http.readAll());
file.close();
http.close();
Using this code Qfile is created but it is 0 kb. nothing is inside.. pls pls pls help me
Re: How to download a file using HTTP with URL in QT?
I don't know why you are persisting with using QHttp when both the posts in this thread and the advice in the Qt documentation tell you to use QNetworkAccessManager. From the QHttp docs:
Quote:
This class is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.
and
Quote:
This class provides a direct interface to HTTP that allows you to download and upload data with the HTTP protocol. However, for new applications, it is recommended to use QNetworkAccessManager and QNetworkReply, as those classes possess a simpler, yet more powerful API and a more modern protocol implementation.
You do have a good reason... don't you? If you were using the recommended network classes then you would find there are examples doing exactly this sort of thing.
Here is your code without your hack and slash commenting,. and with some comments of my own.
Code:
QHttp http;
// Deprecated class, please use QNetworkAccessManager
// Define a file
file.setFileName("C:\\Qt\\OCZToolbox\\"+fileName+" .vic");
// file is not open for read or write
connect (&http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
http.
setHost(url.
host(),
QHttp::ConnectionModeHttp) ;
// Try to get a the URL response into the file that is not open for writing
http.get(url.path(),&file);
// That was an asynchronous operation you just started, so we get here immediately
// Ignore the fact that you have asked QHttp to write the file and try to do it yourself
// Open for writing and truncate any existing file
// write nothing to the file and then close
file.close();
http.close();
I have no idea why you are surprised that the file is empty.