我试图制作一个简单的文件下载器。这是我下载URL的代码段;如果我尝试获取类似" http://stackoverflow.com"的简单网址,则此代码可以正常使用 但是,当我指定重定向的网址时,会引发错误,说明"连接已关闭"。
//Sets a sample URL which redirects.
QString url = "http://downloads.sourceforge.net/project/gretl/gretl/1.9.92/gretl-1.9.92.tar.xz"
QNetworkAccessManager * networkAccessManager = new QNetworkAccessManager(this);
QNetworkRequest * networkRequest = new QNetworkRequest(QUrl(url));
//Getting the URL
QNetworkReply * networkReply = networkAccessManager->get(*networkRequest);
此外,我已连接以下信号插槽以识别网络错误:
//Connects networkReply object's error signal to a slot which prints the error.
connect(networkReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(printError(QNetworkReply::NetworkError)));
如果发出networkReply的错误信号,这是打印错误的插槽:
void printError(QNetworkReply::NetworkError code)
{
qDebug() << "Network Error Code: " << code;
qDebug() << networkReply->errorString();
}
由于此错误, networkReply 对象永远不会收到任何标题。
我需要通过以下方法读取HTTP状态代码:QNetworkReply::attribute(QNetworkRequest::HttpStatusCodeAttribute)
我无法读取HTTP状态代码,因为我没有收到标题。
有谁可以说最新情况? 提前致谢! :)