Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QNetworkAccessManager fails while curl work
Qt 6.12 LTS is out! See what's new in the release blog

QNetworkAccessManager fails while curl work

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.1k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    Paddlaren
    wrote on last edited by
    #1

    Hi,

    I am experimenting with REST server and client in Qt.

    I develop on Linux and Qt 6.5 and run both client and server on local host.

    As server software I have the simple http server example with fixated http port to 8080 and https port to 4430. The problem was there with the original code.
    I have also added a self signed certificate.

    On the client side I have the following code from the HTTP Client example:

        QUrl                    url("https://<fqdn to localhost>");
        QNetworkAccessManager*  nam = new QNetworkAccessManager(this);
    
        QNetworkRequest         request(url);
        url.setPort(4430);
    
        connect(nam, &QNetworkAccessManager::finished, this, &MainWindow::finished);
        connect(nam, &QNetworkAccessManager::sslErrors, this, &MainWindow::sslErrors);
    
        QSslConfiguration sslConf = request.sslConfiguration();
    
        QList<QSslCertificate> caCerts = QSslCertificate::fromPath(QStringLiteral(":/assets/rootCA.crt"));
    
        sslConf.addCaCertificates(caCerts);
        request.setSslConfiguration(sslConf);
    
    
        nam->get(request);
    

    I have added my own CA root certificate to accept the server certificate.

    When I run the client, I get QNetworkReply::ConnectionRefusedError in MainWindow::finished.

    Using curl with the supplied CA root certifiacte and the sam URL and port works like a charm.

    And I am clueless.

    Any suggestions what I have made wrong or how I can find the problem?

    SGaistS 1 Reply Last reply
    0
    • P Paddlaren

      Hi,

      I am experimenting with REST server and client in Qt.

      I develop on Linux and Qt 6.5 and run both client and server on local host.

      As server software I have the simple http server example with fixated http port to 8080 and https port to 4430. The problem was there with the original code.
      I have also added a self signed certificate.

      On the client side I have the following code from the HTTP Client example:

          QUrl                    url("https://<fqdn to localhost>");
          QNetworkAccessManager*  nam = new QNetworkAccessManager(this);
      
          QNetworkRequest         request(url);
          url.setPort(4430);
      
          connect(nam, &QNetworkAccessManager::finished, this, &MainWindow::finished);
          connect(nam, &QNetworkAccessManager::sslErrors, this, &MainWindow::sslErrors);
      
          QSslConfiguration sslConf = request.sslConfiguration();
      
          QList<QSslCertificate> caCerts = QSslCertificate::fromPath(QStringLiteral(":/assets/rootCA.crt"));
      
          sslConf.addCaCertificates(caCerts);
          request.setSslConfiguration(sslConf);
      
      
          nam->get(request);
      

      I have added my own CA root certificate to accept the server certificate.

      When I run the client, I get QNetworkReply::ConnectionRefusedError in MainWindow::finished.

      Using curl with the supplied CA root certifiacte and the sam URL and port works like a charm.

      And I am clueless.

      Any suggestions what I have made wrong or how I can find the problem?

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Did you get something from the sslErrors slot ?
      Did you get any information server side about that connection being tried ?
      Did you also check whether the encrypted signal was emitted ?
      While it should work, you should also check that the call to addCaCertificates was successful.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply
      0
      • hskoglundH Offline
        hskoglundH Offline
        hskoglund
        wrote on last edited by
        #3

        To add to @SGaist, if you have a working curl command line, append `` --libcurl test.c``` to it. Then you can study the contents of test.c and compare it to your Qt code...

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

          Did you get something from the sslErrors slot ?
          Did you get any information server side about that connection being tried ?
          Did you also check whether the encrypted signal was emitted ?
          While it should work, you should also check that the call to addCaCertificates was successful.

          P Offline
          P Offline
          Paddlaren
          wrote on last edited by
          #4

          @SGaist I used to get a lot of certificate problems until I changed to use the FQDN instead of localhost and added the CA-cert. I thought I got the hang of it when those messages was gone.

          I'll look into the other checkpoints.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Paddlaren
            wrote on last edited by Paddlaren
            #5

            Thanks for the tips and @hskoglund , the crul --lib option is a really cool trick I will never forget.

            And @SGaist, thanks for the checklist, I'll keep it in mind. 30 years of C++, quite some time with C-level sockets and newbie on modern networking in Qt I need all help I can get.

            I found the problem and it is a typo.

            I rearranged the code and the URL-port is set after I have used the object to initiate the network request. I shifted the lines and things are working as supposed.

            I figure I just needed some time away from the code to actually READ the code ;)

            1 Reply Last reply
            2
            • P Paddlaren has marked this topic as solved on

            • Login

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