QNetworkAccessManager: Qt 6 and networkAccessible() replacement?
-
wrote on 17 Jan 2021, 15:19 last edited by
I am implementing a dialog which can send error reports to an email address as an attachment. Actually, it does not send the email directly, but via a special PHP script I have installed on the server;
QNetworkAccessManager
only posts the data to it, and the script sends the email.How do I know if I can connect to the internet in Qt 6? In Qt 5, we could call
QNetworkAccessManager::networkAccessible()
and enable or disable certain controls depending on the status.I know that there is some discussion about it in the Qt bug reports, but there still seems to be no suitable replacement. Do we just try to send requests and wait for them to time out?
-
I am implementing a dialog which can send error reports to an email address as an attachment. Actually, it does not send the email directly, but via a special PHP script I have installed on the server;
QNetworkAccessManager
only posts the data to it, and the script sends the email.How do I know if I can connect to the internet in Qt 6? In Qt 5, we could call
QNetworkAccessManager::networkAccessible()
and enable or disable certain controls depending on the status.I know that there is some discussion about it in the Qt bug reports, but there still seems to be no suitable replacement. Do we just try to send requests and wait for them to time out?
@Robert-Hairgrove said in QNetworkAccessManager: Qt 6 and networkAccessible() replacement?:
QNetworkAccessManager::networkAccessible()
This method is / was totally completely not what people intuitively believe it to be. It checked network status (LAN or WiFi), but that does not have any bearing on the state of connection to the Internet.
In order to know if Internet is available, do not use
networkAccessible()
. Use native methods (if available: for example on Android) or manually check by pinging well-known server like your mail server orgoogle.com
. -
@Robert-Hairgrove said in QNetworkAccessManager: Qt 6 and networkAccessible() replacement?:
QNetworkAccessManager::networkAccessible()
This method is / was totally completely not what people intuitively believe it to be. It checked network status (LAN or WiFi), but that does not have any bearing on the state of connection to the Internet.
In order to know if Internet is available, do not use
networkAccessible()
. Use native methods (if available: for example on Android) or manually check by pinging well-known server like your mail server orgoogle.com
.wrote on 17 Jan 2021, 17:24 last edited by@sierdzio Thanks, once again. I think
ping
will be the simplest to implement since there are a few open-source libraries out there that can do this on Windows and Linux.But some servers can be configured to ignore
ping
requests; is there any server that is guaranteed NOT to ignore? For example, google.com answers, but microsoft.com doesn't (have to pingwww.microsoft.com
to get a response). The server I use to send the email also doesn't respond. I would like to make this as robust as possible. -
Google always responds to pings, but using it regularly in a commercial application - I think it's a bit rude ;-) If you have any control over your target mail server, make it respond to
ping
. Or, add a small web service there which will respond to some simple GET request. This way you won't open yourself to pings from random people but will still be able to check. -
Google always responds to pings, but using it regularly in a commercial application - I think it's a bit rude ;-) If you have any control over your target mail server, make it respond to
ping
. Or, add a small web service there which will respond to some simple GET request. This way you won't open yourself to pings from random people but will still be able to check.wrote on 19 Jan 2021, 17:14 last edited by@sierdzio Thanks again. I think I will just send a HEAD request every 10 seconds or so to the actual PHP resource I will need, then I won't need to install yet another library.
If the internet is down, what kind of response would I have to expect from the call to
QNetworkAccessManager::head()
? A NULLQNetworkReply
pointer? Or a 404 HTTP response? The documentation about this is not clear, but would seem to indicate that a valid object/pointer is returned in any case. In that case, I suppose I should call theQNetworkReply::error()
function... -
No idea, to be honest, I've never used
head()
.Heh, that sounds a bit funny now that I read it out loud :-)
-
No idea, to be honest, I've never used
head()
.Heh, that sounds a bit funny now that I read it out loud :-)
wrote on 20 Jan 2021, 09:01 last edited by@sierdzio said in QNetworkAccessManager: Qt 6 and networkAccessible() replacement?:
Heh, that sounds a bit funny now that I read it out loud :-)
LOL ... neither have I! :)
From just a little reading about it, it is similar to GET but without any text in the reply, just the HTTP headers.
-
@sierdzio said in QNetworkAccessManager: Qt 6 and networkAccessible() replacement?:
Heh, that sounds a bit funny now that I read it out loud :-)
LOL ... neither have I! :)
From just a little reading about it, it is similar to GET but without any text in the reply, just the HTTP headers.
wrote on 28 Feb 2024, 15:14 last edited by@Robert-Hairgrove
How did you solve your problem? -
@Robert-Hairgrove
How did you solve your problem?wrote on 28 Feb 2024, 20:45 last edited by@agha I send a HEAD request to the script and see if it is successful. Otherwise, I get a timeout, and I assume that there is no internet connection.
Really it's pretty simple, much better than the way I was trying to do it first.
-
@agha I send a HEAD request to the script and see if it is successful. Otherwise, I get a timeout, and I assume that there is no internet connection.
Really it's pretty simple, much better than the way I was trying to do it first.
wrote on 1 Mar 2024, 18:28 last edited by@Robert-Hairgrove can you share sample code?
-
@Robert-Hairgrove can you share sample code?
wrote on 1 Mar 2024, 22:36 last edited by@agha You need to connect a slot to the
QNetworkAccessManager::finished()
signal and check for an error code in the slot function, that's all. Call theQNetworkAccessManager::head()
method, passing theQNetworkRequest
object you have initialized with the required URL and any custom HTTP headers, etc. If no error is returned, you are good to go. If there is no internet connection, you will get a timeout.