Changeset 769 for trunk/examples/network/http/httpwindow.cpp
- Timestamp:
- Aug 2, 2010, 9:27:30 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:mergeinfo changed
/branches/vendor/nokia/qt/4.6.3 (added) merged: 768 /branches/vendor/nokia/qt/current merged: 767 /branches/vendor/nokia/qt/4.6.2 removed
- Property svn:mergeinfo changed
-
trunk/examples/network/http/httpwindow.cpp
r651 r769 50 50 { 51 51 #ifndef QT_NO_OPENSSL 52 urlLineEdit = new QLineEdit("https:// ");52 urlLineEdit = new QLineEdit("https://qt.nokia.com/"); 53 53 #else 54 urlLineEdit = new QLineEdit("http:// ");54 urlLineEdit = new QLineEdit("http://qt.nokia.com/"); 55 55 #endif 56 56 … … 71 71 progressDialog = new QProgressDialog(this); 72 72 73 http = new QHttp(this);74 75 73 connect(urlLineEdit, SIGNAL(textChanged(QString)), 76 74 this, SLOT(enableDownloadButton())); 77 connect(http, SIGNAL(requestFinished(int,bool)), 78 this, SLOT(httpRequestFinished(int,bool))); 79 connect(http, SIGNAL(dataReadProgress(int,int)), 80 this, SLOT(updateDataReadProgress(int,int))); 81 connect(http, SIGNAL(responseHeaderReceived(QHttpResponseHeader)), 82 this, SLOT(readResponseHeader(QHttpResponseHeader))); 83 connect(http, SIGNAL(authenticationRequired(QString,quint16,QAuthenticator*)), 84 this, SLOT(slotAuthenticationRequired(QString,quint16,QAuthenticator*))); 75 76 connect(&qnam, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), 77 this, SLOT(slotAuthenticationRequired(QNetworkReply*,QAuthenticator*))); 85 78 #ifndef QT_NO_OPENSSL 86 connect( http, SIGNAL(sslErrors(QList<QSslError>)),87 this, SLOT(sslErrors(Q List<QSslError>)));79 connect(&qnam, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), 80 this, SLOT(sslErrors(QNetworkReply*,QList<QSslError>))); 88 81 #endif 89 82 connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload())); … … 105 98 } 106 99 100 void HttpWindow::startRequest(QUrl url) 101 { 102 reply = qnam.get(QNetworkRequest(url)); 103 connect(reply, SIGNAL(finished()), 104 this, SLOT(httpFinished())); 105 connect(reply, SIGNAL(readyRead()), 106 this, SLOT(httpReadyRead())); 107 connect(reply, SIGNAL(downloadProgress(qint64,qint64)), 108 this, SLOT(updateDataReadProgress(qint64,qint64))); 109 } 110 107 111 void HttpWindow::downloadFile() 108 112 { 109 QUrl url(urlLineEdit->text()); 113 url = urlLineEdit->text(); 114 110 115 QFileInfo fileInfo(url.path()); 111 116 QString fileName = fileInfo.fileName(); … … 133 138 } 134 139 135 QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;136 http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());137 138 if (!url.userName().isEmpty())139 http->setUser(url.userName(), url.password());140 141 httpRequestAborted = false;142 QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/");143 if (path.isEmpty())144 path = "/";145 httpGetId = http->get(path, file);146 140 147 141 progressDialog->setWindowTitle(tr("HTTP")); 148 142 progressDialog->setLabelText(tr("Downloading %1.").arg(fileName)); 149 143 downloadButton->setEnabled(false); 144 145 // schedule the request 146 httpRequestAborted = false; 147 startRequest(url); 150 148 } 151 149 … … 154 152 statusLabel->setText(tr("Download canceled.")); 155 153 httpRequestAborted = true; 156 http->abort();154 reply->abort(); 157 155 downloadButton->setEnabled(true); 158 156 } 159 157 160 void HttpWindow::httpRequestFinished(int requestId, bool error) 161 { 162 if (requestId != httpGetId) 163 return; 158 void HttpWindow::httpFinished() 159 { 164 160 if (httpRequestAborted) { 165 161 if (file) { … … 169 165 file = 0; 170 166 } 171 167 reply->deleteLater(); 172 168 progressDialog->hide(); 173 169 return; 174 170 } 175 171 176 if (requestId != httpGetId)177 return;178 179 172 progressDialog->hide(); 173 file->flush(); 180 174 file->close(); 181 175 182 if (error) { 176 177 QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute); 178 if (reply->error()) { 183 179 file->remove(); 184 180 QMessageBox::information(this, tr("HTTP"), 185 181 tr("Download failed: %1.") 186 .arg(http->errorString())); 182 .arg(reply->errorString())); 183 downloadButton->setEnabled(true); 184 } else if (!redirectionTarget.isNull()) { 185 QUrl newUrl = url.resolved(redirectionTarget.toUrl()); 186 if (QMessageBox::question(this, tr("HTTP"), 187 tr("Redirect to %1 ?").arg(newUrl.toString()), 188 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { 189 url = newUrl; 190 reply->deleteLater(); 191 file->open(QIODevice::WriteOnly); 192 file->resize(0); 193 startRequest(url); 194 return; 195 } 187 196 } else { 188 197 QString fileName = QFileInfo(QUrl(urlLineEdit->text()).path()).fileName(); 189 198 statusLabel->setText(tr("Downloaded %1 to current directory.").arg(fileName)); 190 } 191 192 downloadButton->setEnabled(true); 199 downloadButton->setEnabled(true); 200 } 201 202 reply->deleteLater(); 203 reply = 0; 193 204 delete file; 194 205 file = 0; 195 206 } 196 207 197 void HttpWindow::readResponseHeader(const QHttpResponseHeader &responseHeader) 198 { 199 switch (responseHeader.statusCode()) { 200 case 200: // Ok 201 case 301: // Moved Permanently 202 case 302: // Found 203 case 303: // See Other 204 case 307: // Temporary Redirect 205 // these are not error conditions 206 break; 207 208 default: 209 QMessageBox::information(this, tr("HTTP"), 210 tr("Download failed: %1.") 211 .arg(responseHeader.reasonPhrase())); 212 httpRequestAborted = true; 213 progressDialog->hide(); 214 http->abort(); 215 } 216 } 217 218 void HttpWindow::updateDataReadProgress(int bytesRead, int totalBytes) 208 void HttpWindow::httpReadyRead() 209 { 210 // this slot gets called everytime the QNetworkReply has new data. 211 // We read all of its new data and write it into the file. 212 // That way we use less RAM than when reading it at the finished() 213 // signal of the QNetworkReply 214 if (file) 215 file->write(reply->readAll()); 216 } 217 218 void HttpWindow::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes) 219 219 { 220 220 if (httpRequestAborted) … … 230 230 } 231 231 232 void HttpWindow::slotAuthenticationRequired( const QString &hostName, quint16,QAuthenticator *authenticator)232 void HttpWindow::slotAuthenticationRequired(QNetworkReply*,QAuthenticator *authenticator) 233 233 { 234 234 QDialog dlg; … … 236 236 ui.setupUi(&dlg); 237 237 dlg.adjustSize(); 238 ui.siteDescription->setText(tr("%1 at %2").arg(authenticator->realm()).arg(hostName)); 239 238 ui.siteDescription->setText(tr("%1 at %2").arg(authenticator->realm()).arg(url.host())); 239 240 // Did the URL have information? Fill the UI 241 // This is only relevant if the URL-supplied credentials were wrong 242 ui.userEdit->setText(url.userName()); 243 ui.passwordEdit->setText(url.password()); 244 240 245 if (dlg.exec() == QDialog::Accepted) { 241 246 authenticator->setUser(ui.userEdit->text()); … … 245 250 246 251 #ifndef QT_NO_OPENSSL 247 void HttpWindow::sslErrors( const QList<QSslError> &errors)252 void HttpWindow::sslErrors(QNetworkReply*,const QList<QSslError> &errors) 248 253 { 249 254 QString errorString; … … 254 259 } 255 260 256 if (QMessageBox::warning(this, tr("HTTP Example"),261 if (QMessageBox::warning(this, tr("HTTP"), 257 262 tr("One or more SSL errors has occurred: %1").arg(errorString), 258 263 QMessageBox::Ignore | QMessageBox::Abort) == QMessageBox::Ignore) { 259 http->ignoreSslErrors();264 reply->ignoreSslErrors(); 260 265 } 261 266 }
Note:
See TracChangeset
for help on using the changeset viewer.