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. How to create an HTTP server with QNetworkAccessManager?
Forum Updated to NodeBB v4.3 + New Features

How to create an HTTP server with QNetworkAccessManager?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 250 Views 2 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.
  • F Offline
    F Offline
    Fahad-Cattchi
    wrote on 16 Nov 2021, 18:31 last edited by Fahad-Cattchi
    #1

    I can send a get request on the client side with:

    HttpClient::HttpClient()
    {
        this->nam = QNetworkAccessManager{};
        this->reply = nam.get(QNetworkRequest(QUrl("http://www.example.com")));
        connect(reply, &QNetworkReply::finished, this, &HttpClient::replyFinished);
    }
    
    void HttpClient::replyFinished()
    {
        doSomethingWith(this->reply);
    }
    

    But I'm having trouble setting up the server side. I assumed the QNetworkAccessManager would have some function for listening for messages on a port like other communication libraries I've used. And I can't figure out how to send a response as a server would, instead of a client. I can only get, put, post, etc... I don't think that I'm supposed to use an http verb in the server response, but I don't know.

    HttpServer::HttpServer()
    {
        this->nam = QNetworkAccessManager{};
        connect(&nam, &QNetworkAccessManager::finished, this, &HttpServer::replyFinished);
        this->nam.listen(80); // Listen for incoming connections.
    }
    
    void HttpServer::replyFinished(QNetworkReply* reply)
    {
        doSomethingWith(reply);
        QNetworkRequest request;
        request.setRawHeader("content-length", "100");
        request.write( ... ); // Put data in the request body somehow.
        this->nam.respond(url, request); // Respond the way a server should, not a client.
    }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 16 Nov 2021, 18:38 last edited by
      #2

      Hi and welcome to devnet,

      You have it wrong, QNetworkAccessManager is on the client side of the communication.

      Depending on what you want to do on your server, you should consider using something like Cutelyst.

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

      1 Reply Last reply
      2

      1/2

      16 Nov 2021, 18:31

      • Login

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