hi i'm new here. i'm trying to use qhttp for a update app. but there is a problem for me wich i can not solve. here is the problem: i try to download a file(works perfectly) but if there is no connection to the www the file is created but has 0byte, so that my old file is overritten, which is not so good for the application trying to use the file. what i need is to check if the computer is connected to the www. note: proxy may set. i used this example from qt's homepage http://doc.trolltech.com/4.4/network-http.html how can i do that? would be very nice if somebody would have a sollution for this. thanks in advance for help.
Firstly, you should probably now be using QNetworkAccessManager rather than QHttp.
Using either of them, you should do a dummy query to a site you pretty much always know will be up (e.g. http://www.google.com/) and use that as a test to see if you have an internet connection.
A better way of doing this would be instead to use QNetworkAccessManager to read into a QByteArray and then check it isn't empty before writing to your file.
You should switch to the QNetworkAccessManager as Mike Suggested, here is an example of a slot on the finished() signal:
void ApplicationUpdate::replyFinishedhttpGetChangeLog(QNetworkReply* myReply) {
if (myReply->error() != QNetworkReply::NoError)
{
QByteArray returnedData = myReply->readAll();
if (returnedData.size() > 0) {
if( m_fileChangeLog->exists() )
{
m_fileChangeLog->close();
m_fileChangeLog->remove();
}
m_fileChangeLog->open(QIODevice::ReadWrite);
QDataStream out( m_fileChangeLog );
out.writeRawData(returnedData.data(), returnedData.size());
m_fileChangeLog->flush();
m_fileChangeLog->close();
}
}
}
thanks for help it realy fixed my main problem. but now there is another prboblem to fix how can i show an error message if there is no internet conntion. problem is without (w)lan connection there is no error shown. when trying with (w)lan connection and without needed proxy it works well. here is what i got for now:
void updater4::download(QUrl urls, QString filename)
{ files = filename; QNetworkReply* reply = nam->get(QNetworkRequest(urls));
}
void updater4::finished(QNetworkReply *reply)
{
QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
int a = statusCodeV.toInt();
QByteArray test;
QString done= "Downloading file ";
QString fail = "Error when downloading <b>";
fail.append(files);
fail.append("</b><br>Please check internet connection or Proxy settings.<br>or download foo.bar");
z=z+50;
// "200 OK" received?
if (a==200)
{
test = reply->readAll();
if (test.size()>0)
{
file = new QFile(files);//neue Datei enlegen
file->open(QIODevice::WriteOnly) ;
file->write(test);
file->close();
done.append(files).append("\t\t complete\n");
ui.textEdit->insertPlainText(done);
ui.progressBar->setValue(z);
}
else
QMessageBox::warning(this,"Error","Could not open File",QMessageBox::Ok);
}
else
ui.textEdit->insertPlainText(fail);
delete reply;
}
and one more problem needs to be fixed i can only download 1 file but i need to download 4 files. how to do this?
i ran into the same problem, after a bit of poking around, I've isolated the problem down to the project configuration file (.pro), in the broken configuration I was linking the networking library explicitly with the statement : "LIBS += -lQtNetwork". In the working configuration, I used the more formal (and qt compilant) approach of delcaring what Qt components are included in the project, like so: "QT = core gui network xml", adjust accordingly for your sitiation, the netowkring slots did not work on windows when explicitly linked but did work on linux. Using the qt compilant approach works on both platforms.