PDA

View Full Version : Sending HTTP POST network request



Ashley
1st April 2015, 14:34
I'm trying to send a HTTP POST login request in the following format:


POST /login HTTP/1.1
Content-Type: text/xml
Content-Length: 450
Host: 123.456.78.91:80
KeepAlive-Interval: 5

[CONTENT (contains security key)]


Currently, my code is structured like:


qUrl = new QUrl();
qUrl->setHost(ipAddress);
qUrl->setPort(port);
qUrl->setPath("/login");

request = QNetworkRequest(qUrl)
request.setRawHeader("Content-Length", QString::number(securityKey.size()).toLocal8Bit);
... more headers ...

QString content = "contents..."

QNetworkAccessManager* networkManager = new QNetworkAccessManager();
networkManager->connectToHost(ipAddress, port);

networkManager->post(request, content.toLocal8Bit());

Currently I'm not receiving any response from the server so something's wrong. I'm wondering if anyone is able to identify what it may be? I haven't seen any examples online of people connecting to ip addresses/ports with Qt networking so I'm not sure if what I'm doing is correct.

Any help would be much appreciated! :)

anda_skoa
1st April 2015, 15:35
Is your actual code maybe also missing the setting of the scheme for the URL?

Cheers,
_

Ashley
1st April 2015, 15:43
hi, something is happening now - I will investigate to see if it's a positive thing or not, will get back in a moment, thanks for the fast reponse

ChrisW67
1st April 2015, 21:41
You are not setting the Content-Type to" text/xml", maybe causing the server to discard the request.
You are setting Content-length to the length of one thing and putting a different thing in the content. You do not need to explicitly set the content length anyway (IIRC).