Qt 上传下载

来源:互联网 发布:android 崩溃数据丢失 编辑:程序博客网 时间:2024/06/14 00:47
void InternetResourceCommunicater::SendData(const QString &url, QString &sendData, QByteArray &reply){    QNetworkRequest orequest;    QByteArray postData = sendData.toLatin1();//    QSslConfiguration osslCon = orequest.sslConfiguration();//    osslCon.setPeerVerifyMode(QSslSocket::VerifyNone);//    orequest.setSslConfiguration(osslCon);    orequest.setHeader(QNetworkRequest::ContentTypeHeader,                       QString("application/x-www-form-urlencoded"));    orequest.setHeader(QNetworkRequest::ContentLengthHeader, postData.length());    orequest.setUrl(QUrl(url));    QEventLoop* pEventLoop = new QEventLoop();    connect(m_pNetManager, SIGNAL(finished(QNetworkReply*)), pEventLoop, SLOT(quit()));    QNetworkReply *pReply = m_pNetManager->post(orequest,postData);    pReply->ignoreSslErrors();    _setEventLoops.insert(pEventLoop);    pEventLoop->exec();       //block until finish    _setEventLoops.erase(pEventLoop);    delete pEventLoop;    QVariant status_code = pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);    reply = pReply->readAll();}long InternetResourceCommunicater::DownFlie(const QString &strResID, const QString &strFileFullName){    QString strUrl = GetSCEngineWebUrl(m_strDownloadPrepareResource);    QString strData = ACCESS_TOKEN + "=" + m_strToken            + "&rec_resource_id=" + strResID;    QNetworkRequest orequest;    QByteArray postData = strData.toLatin1();    orequest.setHeader(QNetworkRequest::ContentTypeHeader,                       QString("application/x-www-form-urlencoded"));    orequest.setHeader(QNetworkRequest::ContentLengthHeader, postData.length());    orequest.setUrl(QUrl(strUrl));    QEventLoop *pEventLoop = new QEventLoop();    QNetworkReply* pReply = m_pNetManager->post(orequest,postData);    pReply->ignoreSslErrors();    connect(pReply, SIGNAL(finished()), pEventLoop, SLOT(quit()));    connect(pReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(onDownLoad(qint64,qint64)));    QFile fileDown(strFileFullName);    fileDown.open(QFile::WriteOnly);    _mapDownload[pReply] = &fileDown;    //存储eventloop于set中    _setEventLoops.insert(pEventLoop);    pEventLoop->exec();       //block until finish    //清理eventloop,使用完毕清除出set    _setEventLoops.erase(pEventLoop);    disconnect(m_pNetManager,SIGNAL(finished(QNetworkReply*)),pEventLoop,SLOT(quit()));    delete pEventLoop;    QVariant status_code = pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);    fileDown.close();    //d读取完毕的清除出map    _mapDownload.erase(pReply);    disconnect(pReply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(onDownLoad(qint64,qint64)));    return status_code.toInt();}

0 0
原创粉丝点击