문제

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. :)

도움이 되었습니까?

해결책

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...

다른 팁

The following error message should be comprehensive:

"Protocol "" is unknown"

This means the qtnetwork module that you are trying to use has not found a protocol in the request URL, i.e. "http", "ftp", and so on.

That should give a hint that you forget to send that for some reason. Failing that, you have sent an unknown. If you take a look at your URL construction code, you can see that you miss the protocol and network location from there because you override that with the suburl rather than appending. This is the line I am referring to:

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

The right fix would to override the existing protocol name and network location rather than overriding. Thereby, you would be writing something like this:

url_str.append("/page_wanted?parameters=5&t=3");

This is one typical use case when using the comprehensive named methods can avoid the probably unintentional typo between = and +=.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top