链接断开/链接后QNetworkAccessManager无法正常工作

时间:2018-02-12 14:00:23

标签: c++ qt embedded

我在嵌入式设备上的客户端使用QNetworkAccessManager与我们的后端服务器进行通信。当我长时间从设备中删除修补线后,插入后我的QNetworkAccessManager对象无法正常工作。在服务器上发布数据后,它返回错误“找不到主机”。从路由器上卸下修补线不会产生这样的错误。此外,在某些类型的设备上也会出现这种情况,但对于PC或其他类型的嵌入式设备,一切都很好。

在一切都好的设备上我使用 target = sh4 Linux 2.6.32.59 qt 4.7.2 在有错误的设备上:Linux 2.6.32.59-b2067,qt 4.8.4

以下是使用QNetworkAccessManager的代码:

void CmdUrlRequest::execute()
{
        static QNetworkAccessManager n_manager;
#if QT_VERSION>=0x040700
        if(QUrl(*_url).host()=="localhost" || QUrl(*_url).host().right(3)=="zet")
            QNetworkProxyFactory::setUseSystemConfiguration(false);
        else
            QNetworkProxyFactory::setUseSystemConfiguration(true);
#endif
    if (_timeout_tm==NULL)  
    {
        _timeout_tm=new QTimer(this);
        _timeout_tm->setInterval(_timeout);
        _timeout_tm->setSingleShot(true);
        connect(_timeout_tm, SIGNAL(timeout()), this, SLOT(timeoutedDxprequest()));
    }
    qDebug() << "CmdUrlRequest::execute" << "command ID" << _command_id << "URL execute()" << this << *_url << QThread::currentThread();

    assert(n_manager.thread()->currentThreadId()==_timeout_tm->thread()->currentThreadId());

    if (_url && _method)
    {
        QString b;
        if (_body)
            b=*_body;

        _array_ptr->clear();
        _manager = &n_manager;// new QNetworkAccessManager();
        _manager->setCookieJar(StoredCookieJar::Instance());
        StoredCookieJar::Instance()->setParent(0);
        _nreq = QNetworkRequest();
        _nreq.setUrl(QUrl(*_url));


        if ((*_url).startsWith("https"))
        {           
            QSslConfiguration sslc;
            sslc.setPeerVerifyMode(QSslSocket::VerifyNone);
            sslc.setProtocol(QSsl::AnyProtocol);
            _nreq.setSslConfiguration(sslc);
        }
        if(*_method != "get")
        {
            _nreq.setRawHeader("Content-Type","application/x-www-form-urlencoded");
            _nreq.setRawHeader("Authorization","Basic YXJlczpGWGEwc2tsNGQ=");
        }

        QList<QPair<QByteArray,QByteArray> >::iterator hid;

        if(_headers!=NULL)
            for(hid=_headers->begin(); hid!=_headers->end(); ++hid)
            {
                _nreq.setRawHeader(hid -> first, hid -> second);
                qDebug() << this -> metaObject() -> className() << hid -> first << hid -> second;
            }
        _post_data=_body->toUtf8();
        qDebug() << this -> metaObject() -> className() << "method =" << *_method << _body->toUtf8();

        _req_timeresp = QDateTime::currentDateTime().toTime_t();

        if(*_method == "post")
            _nres = _manager -> post(_nreq, _post_data);
        else if(*_method == "head")
            _nres = _manager -> head(_nreq);
        else if(*_method == "get")
            _nres = _manager -> get(_nreq);
        qDebug() << this -> metaObject() -> className() << "res =" << _nres ;
        connect(_nres, SIGNAL(finished()), this, SLOT(getResponse()));
        connect(_nres, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(progress(qint64,qint64)));
        connect(_nres, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(error(QNetworkReply::NetworkError)));
        connect(_nres, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>)));

        _timeout_tm->start();
    }
    else
    {
        onResult(CMD_ERROR);
    }

}

0 个答案:

没有答案