Qt 上传下载

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();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值