0

i'm using QNetworkReply in order to issue a simple GET request to my router interface. Basically if the post data is empty i issue a GET otherwise i will issue a POST. Let's stick with the GET

QString url=ui->lineEdit_url->text();
QString paras=ui->pTextEdit_paras->toPlainText();
qDebug()<< "paras" << paras;
QByteArray post_data;
post_data.append(paras);
QNetworkRequest request = QNetworkRequest(QUrl(url));
request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
if(post_data.isEmpty())
{
    //nam->head(request);
    nam->get(request);
}
else
{
    nam->post(request,post_data);
}

now with

...
connect(nam,
        SIGNAL(finished(QNetworkReply*)),
        this,
        SLOT(finished(QNetworkReply*)));
...

void HttppostWindow::finished(QNetworkReply *reply)
{
    if(reply->error() == QNetworkReply::NoError)
    {
        ui->textEdit_result->setText(QObject::tr(reply->readAll()));
    }
    else
    {
        ui->textEdit_result->setPlainText(reply->errorString());
    }
}

i show the answer in the ui. Right now the local interface asks for a login and a pw. The problem is that the retrieved text with the GET command is the one that the interface would show if the user would have insterted a wrong password (autentication failed please try again and so on). Moreover with the code nam->head(request) i should be able to retrieve the header, but the content of replyAll is empty.

Any ideas?

user217354
  • 145
  • 1
  • 14
  • 1
    Are you sending the router's username/password in your request? if your router uses HTTP Basic authentication, refer to [this answer](https://stackoverflow.com/a/1700751/2666212) for information on how to do so. – Mike Aug 31 '16 at 21:19
  • @Mike Well, in the code it is clearly visible that im not sending anything if the paras variable is empty. As i said it is just a GET request – user217354 Sep 01 '16 at 06:19
  • Whatever request it is, you have to authenticate yourself to the router. try sending the header specified in the answer linked in my previous comment. – Mike Sep 01 '16 at 20:01

1 Answers1

0

After nam->head(request) you don't need to use reply->readAll(). Instead of this you should use methods like:

QByteArray  rawHeader(const QByteArray &headerName) const;
QList<QByteArray> rawHeaderList() const;
const QList<RawHeaderPair> &rawHeaderPairs() const;

With this methods you can view content of your head request.

Also your sample code has memory leak. You should delete the reply whis reply->deleteLater() inside your finished slot.

Evgeny
  • 3,910
  • 2
  • 20
  • 37
  • Could it be a matter of cookies? Does QNetworkManager implement a cookie handler? – user217354 Sep 01 '16 at 13:31
  • @user217354 , this depends on your router. If its web management interface uses HTTP basic authentication, it does not need cookies, you just have to send the `Authorization` header with every request you do. If it uses cookies, you may need to `POST` your username/password first, then you'll have access to the web management as long as you use the same `QNetworkAccessManager`. Again, it all depends on the method the router uses. . . – Mike Sep 01 '16 at 22:14