我學習Qt
,我想從Internet下載XML文件。 QUrl
取決於其他值value1和value2;例如:Qt從Web請求動態XML
http://www.example.org/path/the_xml_file.xml?value1=1&value2=2
在這裏,我設置QString
destination_XML_URL
void AClass::setUrlString(quint32 t_value1, quint32 t_value2) {
destination_XML_URL = QString("http://www.anyweb.com/path/the_xml_file.xml?value1=%1&value2=%2").arg(t_value1).arg(t_value2);
}
然後我請求與在方法的QUrl
位置:
QNetworkAccessManager url_network_access_manager;
//..;
setTarget(12, 34);
QUrl xml_Url(destination_XML_URL);
QNetworkRequest request(xml_Url);
QNetworkReply url_network_reply = url_network_access_manager.get(request);
最後連接連接:
connect(&url_network_access_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slot_disconnect2Net(QNetworkReply*)));
然後插槽,從網頁檢索XML文件後:
void AClass::slot_disconnect2Net(QNetworkReply *data) {
qint64 t_size1 = data->size();
int error_code = data->error();
switch(error_code) {
case 0:
transform_Raw_2_XML(data);
break;
case 3:
break;
case 301:
break;
default:
QMessageBox::critical(this, "AClass::disconnect2Net()", QString("Connection error %1:\n %2").arg(error_code).arg(data->errorString()), QMessageBox::Ok);
}
}
,但我得到ERROR_CODE 3:
http://doc.qt.io/qt-4.8/qnetworkreply.html#NetworkError-enum
「QNetworkReply :: HostNotFoundError = 3 :遠程主機名稱不是 找到(無效主機名)「
任何想法?
是的,有代理:// QNetworkProxy代理; //proxy.setType(QNetworkProxy::HttpProxy); //proxy.setHostName("web.name.net「); //proxy.setPort(8080); //proxy.setUser("username「); //proxy.setPassword("*******「); // QNetworkProxy :: setApplicationProxy(代理); //url_network_access_manager.setProxy(proxy);隨着我得到QNetworkReply :: ProxyAuthenticationRequiredError 代理服務器需要以接受該請求認證,但沒有接受任何一個憑證(如有) – 2012-03-13 10:57:29
又是什麼QNetworkReply :: Errorstring,則()說什麼? – salomon 2012-03-13 11:12:42
文中說:「代理服務器要求身份驗證」 – 2012-03-13 11:47:28