如何使用qt获得正确的http文件长度?

时间:2018-07-04 09:25:57

标签: c++ qt

我想用Qt来获取url“ https://iso.mirrors.ustc.edu.cn/qtproject/archive/qt/5.11/5.11.1/qt-opensource-windows-x86-5.11.1.exe”文件的长度,

但是QNetworkAccessManager请求无法获得Content-Length。内容是

<html><head><script>document.cookie="addr=113.57.128.130";setTimeout("location.reload();", 2);</script></head><body>This page requires JavaScript. Please wait 2 seconds.
</body></html>

如果我添加cookie="addr=113.57.128.130"以手动请求,我将得到正确的信息。如何获取文件长度?

1 个答案:

答案 0 :(得分:1)

尝试以下示例:

widget.h

<!DOCTYPE html>
<html>
<style>
div.alertbox{padding: 10px;bottom: 40px;left: 5px; max-width: 800px; margin-left: auto;margin-right: auto;z-index: 99; max-width: 700px; color: #000;background-color: #EEE;border-radius: 10px; padding-right: 5px; padding-left: 10px;}
span#closebtn {margin-left: 15px;color: black;font-weight: bold;float: right;font-size: 22px;line-height: 20px; cursor: pointer; transition: 0.3s;}
</style>
<body>
<div id="divMessageContainer"></div>
<button onclick="displayError()">button</button>
</body>
<script>
var messageCount = 0;
var messageTimeout = 3000;
function displayError(errorMessage) {
    var messageObject = "<div class='alertbox' id='alertbox"+messageCount+"'><span class='closebtn' id='closebtn'>&times;</span><strong><font color='#f44336;'>Error!</font></strong>"+errorMessage+"</div>"
    document.getElementById("divMessageContainer").innerHTML += messageObject;
    var messageID = messageCount;
    setTimeout(function(){removeElement("alertbox" + messageID); }, messageTimeout);
    closeAlertbox();    
}

function removeElement(id) {
    if (document.getElementById(id)) {      
        return (elem=document.getElementById(id)).parentNode.removeChild(elem);     
    }
}


function closeAlertbox() {
    var close = document.getElementsByClassName("closebtn");
    var i;
    for (i = 0; i < close.length; i++) {
        close[i].onclick = function(){
            var div = this.parentElement;
            div.style.opacity = "0";
            setTimeout(function()
            { div.style.display = "none"; 
            }, 10);
        }
    }
} 
</script>
</html>

widget.cpp

public:
    void requestFileSize(const QString &url);
    QNetworkAccessManager *m_netmanager;
    QNetworkReply *m_reply;

public slots:
    void fileSize();

现在通话:

void Widget::requestFileSize(const QString &url)
{
    QNetworkRequest req;
    m_netmanager = new QNetworkAccessManager(this);
    req.setUrl(QUrl(url));
    m_reply = m_netmanager->get(req);
    connect(m_reply, SIGNAL(metaDataChanged()), this, SLOT(fileSize()));
}

void Widget::fileSize()
{
    qDebug() << "Content Length: " << m_reply->header(QNetworkRequest::ContentLengthHeader).toString();
    m_reply->deleteLater();
    m_netmanager->deleteLater();
}

以字节为单位的输出:

requestFileSize("https://as5.cdn.asset.aparat.com/aparat-video/c6f1e63e310060e70574f1692c2322395584619-360p__37462.mp4");

及其大约Content Length: "12186677"

我用一些11.6 MB对其进行了测试,但是我不知道为什么用这个url https://iso.mirrors.ustc.edu.cn/qtproject/archive/qt/5.11/5.11.1/qt-opensource-windows-x86-5.11.1.exe总是返回url