How to create an HTTP server with QNetworkAccessManager?
-
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. }