Qt 4.8.4 how to check if file exists on http server
-
need to check the if file exists on http server, i have the full path and when i try it via browser all works but when i try in code to do :
@if(QFile::exists("http://www.foo.com/hidden/Support/myapp_1.1.2_installer.exe" ))
{
qDebug("file exists");
return true;
}
else
{
qDebug("file not exists");
}@as it writen here :
"Your text to link here...":http://www.qtcentre.org/archive/index.php/t-43712.html?s=b9ae49962c9219aec93b43c514e2ba33it allways returns me false no matter what .. what im doing wrong and is it the right way to do this ?
-
Hi,
No it's not the right way, QFile is not designed for this purpose, it's for local files. You should rather look into QNetworkAccessManager for what you want to achieve
-
try using HEAD method
@bool fileExist(const QUrl &url)
{
QTcpSocket socket;
socket.connectToHost(url.host(), 80);
if (socket.waitForConnected()) {
socket.write("HEAD " + url.path().toUtf8() + " HTTP/1.1\r\n"
"Host: " + url.host().toUtf8() + "\r\n"
"\r\n");
if (socket.waitForReadyRead()) {
QByteArray bytes = socket.readAll();
if (bytes.contains("200 OK"))
return true;
}
}
return false;
}@@qDebug() << fileExist(QUrl("http://www.foo.com/hidden/Support/myapp_1.1.2_installer.exe"));@