如果代码不是200,我如何从QNetworkReply读取正文

时间:2015-03-18 16:17:26

标签: qt qnetworkreply

如果reply->error() != QNetworkReply::NoError

回复会返回空身内容, 但实际上响应有内容。

我怎么读?

void MainWindow::on_pushButton_clicked()
{
   manager = new QNetworkAccessManager(this);

   connect( manager, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(replyFinished(QNetworkReply*)));

   manager->get(QNetworkRequest(QUrl("http://...")));
}

void MainWindow::replyFinished(QNetworkReply* reply)
{
  if (reply->error() == QNetworkReply::NoError)
    {
      QByteArray content= reply->readAll();
      QDebug() << QString body(content); // ok
    } else {
       QByteArray content= reply->readAll();
       QDebug() << QString body(content); //empty, but must be exist
    }
}

0 个答案:

没有答案