Qt - How to get responseText with QNetworkAccessmanager

纵然是瞬间 提交于 2020-01-02 07:02:10

问题


Here is my code:

Widget::Widget()
{
    manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));

    manager->get(QNetworkRequest(QUrl("http://qt.nokia.com")));
}
void Widget::replyFinished(QNetworkReply* reply)
{
    //some other code here
}

I hope that reply will have some method like getrespnsetext() but it not...
Can some one show me an example, all the thing i need is print out the response text (is ther any way like in Javascript Ajax)
Thanks for your help!


回答1:


You only need to use reply->readAll() inside the replyFinished(...) function to read all the returned text. It returns a QByteArray, so you can do wathever you want from there.




回答2:


Looking at the documentation for QNetworkReply here, specifically at the finished signal, it mentions that you can use readAll() to get a QByteArray of all of the data. Assuming that you know whether or not such a conversion is valid, QString does have a constructor that takes a QByteArray as a parameter, as documented here.



来源:https://stackoverflow.com/questions/7981239/qt-how-to-get-responsetext-with-qnetworkaccessmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!