Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QNetworkAccessManager: Qt 6 and networkAccessible() replacement?

QNetworkAccessManager: Qt 6 and networkAccessible() replacement?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Robert Hairgrove
    wrote on 17 Jan 2021, 15:19 last edited by
    #1

    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?

    S 1 Reply Last reply 17 Jan 2021, 17:00
    0
    • R Robert Hairgrove
      17 Jan 2021, 15:19

      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?

      S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 17 Jan 2021, 17:00 last edited by
      #2

      @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 or google.com.

      (Z(:^

      R 1 Reply Last reply 17 Jan 2021, 17:24
      2
      • S sierdzio
        17 Jan 2021, 17:00

        @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 or google.com.

        R Offline
        R Offline
        Robert Hairgrove
        wrote on 17 Jan 2021, 17:24 last edited by
        #3

        @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 ping www.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.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 18 Jan 2021, 06:53 last edited by
          #4

          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.

          (Z(:^

          R 1 Reply Last reply 19 Jan 2021, 17:14
          1
          • S sierdzio
            18 Jan 2021, 06:53

            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.

            R Offline
            R Offline
            Robert Hairgrove
            wrote on 19 Jan 2021, 17:14 last edited by
            #5

            @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 NULL QNetworkReply 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 the QNetworkReply::error() function...

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sierdzio
              Moderators
              wrote on 19 Jan 2021, 20:06 last edited by
              #6

              No idea, to be honest, I've never used head().

              Heh, that sounds a bit funny now that I read it out loud :-)

              (Z(:^

              R 1 Reply Last reply 20 Jan 2021, 09:01
              0
              • S sierdzio
                19 Jan 2021, 20:06

                No idea, to be honest, I've never used head().

                Heh, that sounds a bit funny now that I read it out loud :-)

                R Offline
                R Offline
                Robert Hairgrove
                wrote on 20 Jan 2021, 09:01 last edited by
                #7

                @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.

                A 1 Reply Last reply 28 Feb 2024, 15:14
                0
                • R Robert Hairgrove
                  20 Jan 2021, 09:01

                  @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.

                  A Offline
                  A Offline
                  agha
                  wrote on 28 Feb 2024, 15:14 last edited by
                  #8

                  @Robert-Hairgrove
                  How did you solve your problem?

                  R 1 Reply Last reply 28 Feb 2024, 20:45
                  0
                  • A agha
                    28 Feb 2024, 15:14

                    @Robert-Hairgrove
                    How did you solve your problem?

                    R Offline
                    R Offline
                    Robert Hairgrove
                    wrote on 28 Feb 2024, 20:45 last edited by
                    #9

                    @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.

                    A 1 Reply Last reply 1 Mar 2024, 18:28
                    0
                    • R Robert Hairgrove
                      28 Feb 2024, 20:45

                      @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.

                      A Offline
                      A Offline
                      agha
                      wrote on 1 Mar 2024, 18:28 last edited by
                      #10

                      @Robert-Hairgrove can you share sample code?

                      R 1 Reply Last reply 1 Mar 2024, 22:36
                      0
                      • A agha
                        1 Mar 2024, 18:28

                        @Robert-Hairgrove can you share sample code?

                        R Offline
                        R Offline
                        Robert Hairgrove
                        wrote on 1 Mar 2024, 22:36 last edited by
                        #11

                        @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 the QNetworkAccessManager::head() method, passing the QNetworkRequest 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.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved