Qt Http get

 

1、直接建立连接,向网站发送http请求

QNetworkAccessManager *accessManager = new QNetworkAccessManager(this);
connect(accessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
accessManager->get(QNetworkRequest(QUrl("http:xxxxx")));

2、在槽函数中打印【注意:如果返回字节数过大,qDebug是打印不出来的,可通过size查看返回字节数】

void MainWindow::replyFinished(QNetworkReply *reply)
{
    if (reply->error() == QNetworkReply::NoError)
    {
        QByteArray bytes = reply->readAll();
        qDebug() << bytes;
    }
    else
    {
        qDebug() << "finishedSlot errors here";
        qDebug( "found error .... code: %d\n", (int)reply->error());
        qDebug(qPrintable(reply->errorString()));
    }
}

  

posted @ 2019-10-08 16:28  朱小勇  阅读(1230)  评论(0编辑  收藏  举报