Sending HTTP Header Information Using Qt QNetworkAccessManager

I have the following code and I would like to add some HTTP header information along with the call. Anyway, can I do this?

void NeoAPI::call(QString apiCall) { if (this->ApiCall.contains(apiCall)) { QNetworkAccessManager* manager = new QNetworkAccessManager(0); connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(netReplyFinished(QNetworkReply*))); QUrl url = this->ApiCall[apiCall]; url.addQueryItem("memberid","76710"); // Set for backdoor debugging manager->get(QNetworkRequest(url)); } else { this->requestResultText = QString("Call %1 doesn't exist").arg(apiCall); } } void NeoAPI::netReplyFinished(QNetworkReply *netReply) { if (netReply->error() == QNetworkReply::NoError) { this->requestResultText = netReply->readAll(); } else { this->requestResultText = "API Call Failed"; } QMessageBox messageBox; messageBox.setText(this->requestResultText); messageBox.exec(); //delete netReply; } 

In addition, if I had not used them inside the class, what would this in connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(netReplyFinished(QNetworkReply*))); to be?

Thanks!

+4
source share
1 answer

Yes, see the QNetworkRequest documentation.

You need to do something like:

 QNetworkRequest request(url); request.setHeader( QNetworkRequest::ContentTypeHeader, "some/type" ); request.setRawHeader("Last-Modified", "Sun, 06 Nov 1994 08:49:37 GMT"); manager->get( header ); 

In addition, if I did not use them inside the class, what would it be in connection (manager, SIGNAL (finished (QNetworkReply *)), this is SLOT (netReplyFinished (QNetworkReply *))); to be?

It will not be anything. To connect a signal to a slot, this slot must be a member function of an object. This explains the Qt primer for signals and slots .

+7
source

Source: https://habr.com/ru/post/1315002/


All Articles