I have a URL of a file and I want to download it from the internet. Before downloading, I want to make sure that the file I am going to download actually exists i.e, the URL is correct or the file has not been deleted by the owner. How am I supposed to do so in Qt?
Asked
Active
Viewed 225 times
0
-
1Why do you need to query its existence first, if you are going to download it anyway? Just download it normally, and the response will tell you whether it exists or not. You don't need to do extra work unnecessarily. – Remy Lebeau Feb 21 '22 at 18:54
-
I have many URLs(some of them do not work) in 1 file I want to download those which are in working condition and skip the other. The way I am doing this is first separating the working URLs and then download 1 by 1 – M. Abdul Rehman Feb 21 '22 at 19:04
-
2"*I want to download those which are in working condition and skip the other*" - so just download them normally and handle any errors as they occur. "*The way I am doing this is first separating the working URLs and then download 1 by 1*" - that is just wasted overhead that you don't actually need. There is no guarantee that the "working" urls will still be working by the time you have separated them for downloading. But whatever. To answer your original question, to test a URL without actually downloading its data, send a `HEAD` request to the server, rather than a `GET` request. – Remy Lebeau Feb 21 '22 at 19:21
-
Maybe this answer of [How in Qt5 to check if url is available?](https://stackoverflow.com/a/28498623/3972710) ... but sure as Remy Lebeau stated your file may be gone once you start to try download it – NGI Feb 21 '22 at 22:30
-
There's nothing in the Qt library that will get you around having to check for every file. As Remy suggested, you could do a HEAD instead of a GET, but if your intention is to download all files that do exist, this doesn't really gain anything. Just do GET and check for 404 errors. EDIT: this isn't entirely correct in all cases, as you could use QDir::entryInfoList in some cases. – mzimmers Feb 21 '22 at 22:48