networkAccessManager = new QNetworkAccessManager(); connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(processNetworkReply(QNetworkReply*))); ui->rawReponseView->page()->setNetworkAccessManager(networkAccessManager);
void MainWindow::processNetworkReply(QNetworkReply* networkReply){ if (!networkReply->error()) { QByteArray content = networkReply->readAll(); qDebug() << "content.length(): " << content.length(); qDebug() << "content: " << content; QList<QByteArray> listHeaders; listHeaders = networkReply->rawHeaderList(); QByteArray header; qDebug() << "*******************************"; foreach(header, listHeaders) { qDebug("%s: %s", header.data(), networkReply->rawHeader(header).data()); } qDebug() << "*******************************"; qDebug() << "Status code: " << networkReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString(); qDebug() << "*******************************"; qDebug() << "networkReply->isFinished(): " << networkReply->isFinished(); qDebug() << "networkReply->bytesAvailable(): " << networkReply->bytesAvailable(); qDebug() << "networkReply->isReadable(): " << networkReply->isReadable(); qDebug() << "ContentLengthHeader: " << networkReply->header(QNetworkRequest::ContentLengthHeader).toString(); qDebug() << "------------------------"; }}
C++ (Qt)qDebug() << "content: " << content;
C++ (Qt)qDebug() << "content: " << content.toHex();
QNetworkReply* AdvancedNetworkAccessManager::createRequest( Operation op, const QNetworkRequest &request, QIODevice *outgoingData){ QNetworkReply* networkReply = QNetworkAccessManager::createRequest(op, request, outgoingData); connect(networkReply, SIGNAL(readyRead()), this, SLOT(processReply())); return networkReply;}void AdvancedNetworkAccessManager::processReply(){ QNetworkReply* currentReply = qobject_cast<QNetworkReply*>(sender()); int bytesAvailable = currentReply->bytesAvailable(); QByteArray content = currentReply->peek(bytesAvailable); qDebug() << "content size: " << content.size(); qDebug() << "content: " << content.toHex();}
C++ (Qt) class AdvancedNetworkAccessManager : ...{... QByteArray content;}; void AdvancedNetworkAccessManager::processReply(){ QNetworkReply* currentReply = qobject_cast<QNetworkReply*>(sender()); if( !currentReply ) return; int bytesAvailable = currentReply->bytesAvailable(); content += currentReply->peek(bytesAvailable);}
C++ (Qt)class ReplySaver : public QObject{ ReplySaver( const QUrl &url, ... ) : m_url( url ), m_finish( false ) {}...public: bool isFinished() const { return m_finish; } QByteArray content() const { return m_content; } public slots: void processReply() { ... } void finished() { m_finish = true; } private: QByteArray m_content; QUrl m_url; bool m_finish;}; QNetworkReply* AdvancedNetworkAccessManager::createRequest( Operation op, const QNetworkRequest &request, QIODevice *outgoingData){ QNetworkReply* networkReply = QNetworkAccessManager::createRequest(op, request, outgoingData); ReplySaver *saver = new ReplySaver( networkReply->url() ); connect(networkReply, SIGNAL(readyRead()), saver, SLOT(processReply())); connect(networkReply, SIGNAL(finished()), saver, SIGNAL(finished())); // Сохраняем в списке // В класс добавляем методы для доступа к элементам списка saverList saverList.append( saver ); return networkReply;}