如何从QNetworkReply中提取数据?

时间:2018-03-28 07:12:42

标签: qt qnetworkaccessmanager qnetworkreply

我有一个POST请求,如下所示,返回给定名称的地址结果。

void MyClass::searchRequest(QString strname)
{
    QUrl url(QString("http://myurl/searchaddress"));
    QNetworkRequest req(url);

    req.setRawHeader("Host", url.toAce(url.host()));
    req.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
    req.setRawHeader("Accept", "*/*");
    req.setRawHeader("Accept-Language", "en-IN,en;q=0.9,en-GB;q=0.8,en-US;q=0.7,ml;q=0.6");
    req.setRawHeader("Accept-Encoding", "gzip,deflate");
    req.setRawHeader("Keep-Alive", "300");
    req.setRawHeader("Connection", "keep-alive");
    req.setRawHeader("Referer", returl.toLatin1());
    req.setRawHeader("Content-Length", "37");
    req.setRawHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

    QString form = QString("name=%1&action=searchaddress").arg(strname);
    QByteArray formencoded = QUrl::toPercentEncoding(form, "&=", "");
    m_reply = manager->post(req, formencoded);
    connect(m_reply, SIGNAL(finished()), this, SLOT(onDataArrived()));
}

我已经检查过wireshark并请求返回以下格式。

{"result":"found","address":"my_address"}

我要做的是获取地址和结果分开的字符串。下面显示了我的onDataArrived SLOT代码

void MyClass::onDataArrived()
{
    disconnect(m_reply, SIGNAL(finished()), this, SLOT(onDataArrived()));
    if (!reply->error())
    {
        QByteArray data = reply->readAll();
        qDebug() << QString(data);   // this gives me some output like "\u001F?b". 
        //I don't know how to extract the data here.
    }
}

有人可以帮助我将地址和结果提取到单独的字符串中吗?

0 个答案:

没有答案