Search code examples
c++qtqstringqtnetworkqurl

Http POST, selecting the page using Qt


i'm trying to authenticate in a online server, but i can't select the page that i want to download, here is my code:

The Get request:

QObject::connect(&access, SIGNAL(finished(QNetworkReply*)), &loop_get, SLOT(quit()));

url_str = QString("http://") + info + QString(".server.com");
server_url = QString("t") + info + QString("-") + info + QString(".server.com");

request.setUrl(QUrl(url_str));

reply = access.get(request);
loop_get.exec();

if(reply->error() != QNetworkReply::NoError)
{
    throw std::runtime_error(reply->errorString().toStdString());
}

delete reply;

here the post request, is called right after the get request finish:

QObject::connect(&access, SIGNAL(finished(QNetworkReply*)), &loop_post, SLOT(quit()));

url_str = "/page_wanted?parameters=5&t=3";

//Assembly the message budy
msg_body = QString("message");

//assembly HTTP header
request.setUrl(QUrl(url_str));
request.setHeader(QNetworkRequest::ServerHeader, server_url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlenconded");
request.setHeader(QNetworkRequest::ContentLengthHeader, QString::number(message.size()));

reply = access.post(request, QByteArray(message.toStdString().c_str()));

loop_post.exec();

if(reply->error() == QNetworkReply::NoError)
{
    qDebug() <<"that's ok:\n"<<reply->readAll();
}
else
{
    qDebug() <<"That's not ok:\n"<<reply->errorString();
}

delete reply;

the error returned is:

That's not ok:
"Protocol "" is unknown"

i don't know how to do that, searching on google i only found examples that takes me to that way, so if someone could help i'll thank you. :)


Solution

  • The line

    url_str = "/page_wanted?parameters=5&t=3";
    

    is missing a plus sign :D try

    url_str += "/page_wanted?parameters=5&t=3";
    

    and you should be good to go...