void WebLoadManager::onMeta() { QNetworkReply *reply = qobject_cast<QNetworkReply*>(QObject::sender()); if (!reply) return; Replies::iterator j = _replies.find(reply); if (j == _replies.cend()) { // handled already return; } webFileLoaderPrivate *loader = j.value(); typedef QList<QNetworkReply::RawHeaderPair> Pairs; Pairs pairs = reply->rawHeaderPairs(); for (Pairs::iterator i = pairs.begin(), e = pairs.end(); i != e; ++i) { if (QString::fromUtf8(i->first).toLower() == "content-range") { QRegularExpressionMatch m = QRegularExpression(qsl("/(\\d+)([^\\d]|$)")).match(QString::fromUtf8(i->second)); if (m.hasMatch()) { loader->setProgress(qMax(qint64(loader->data().size()), loader->already()), m.captured(1).toLongLong()); if (!handleReplyResult(loader, WebReplyProcessProgress)) { _replies.erase(j); _loaders.remove(loader); delete loader; reply->abort(); reply->deleteLater(); } } } } }
PyObject *PythonQtDecorator::retrieve(QNetworkAccessManager *self, QString url) { //qDebug("QNAM %p retrieve %s", self, qPrintable(url)); QNetworkRequest request; request.setUrl(QUrl(url)); request.setRawHeader("Connection", "keep-alive"); QNetworkReply *reply = syncGet(self, request); if (reply->error() != QNetworkReply::NoError) { qWarning("Network Error: %s", qPrintable(reply->errorString())); QByteArray reason = reply->errorString().toUtf8(); char *creason = (char*)malloc(reason.size() + 1); strcpy(creason, reason.constData()); PyErr_SetString(PyExc_IOError, creason); reply->deleteLater(); return NULL; } // Reply #if 0 qDebug("Reply: %s %i", qPrintable(reply->url().toString()), reply->isFinished()); foreach(QNetworkReply::RawHeaderPair header, reply->rawHeaderPairs()) qDebug("HDR: %s: %s", qPrintable(header.first), qPrintable(header.second)); #endif QByteArray barr = reply->readAll(); reply->deleteLater(); return toBytes(&barr); }
QNetworkReply::NetworkError NetworkServiceAccessor::get(const QNetworkRequest &request, QByteArray &replyData, QList<QNetworkReply::RawHeaderPair> &headers) const { QNetworkReply::NetworkError result = QNetworkReply::NoError; QFuture<void> future = QtConcurrent::run(QThreadPool::globalInstance(), [&](){ QEventLoop loop; QNetworkAccessManager networkAccessManager; QNetworkReply *reply = networkAccessManager.get(request); connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); LoggingManager::getSingleton().log() << "NetworkService Get: " << request.url().toString(); qDebug() << "NetworkService Get:" << request.url().toString(); loop.exec(); result = reply->error(); if (result != QNetworkReply::NoError) { qDebug() << "Network Service Accessor Error: " << reply->errorString(); qDebug() << "error code:" << reply->error(); LoggingManager::getSingleton().log() << "NetworkService: " << reply->errorString(); } headers = reply->rawHeaderPairs(); replyData = reply->readAll(); qDebug() << replyData; reply->deleteLater();}); future.waitForFinished(); return result; }
void MainWindows::tryLoinTriggered() { QNetworkRequest newRequest(QUrl(BOX_LOGIN_URL_TTPS)); QEventLoop eventLoop; QString replyText; QNetworkReply * reply = nullptr; QString postData = QString(BOX_LOGIN_POST_DATA) .arg(this->_autToken.toHtmlEscaped()) .arg(QString("*****@*****.**").toHtmlEscaped()) .arg(QString("*********")); newRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); newRequest.setRawHeader("origin", "https://box.es"); newRequest.setSslConfiguration(*this->_sslConfig); reply = this->_networkManager->post(newRequest, QByteArray(postData.toStdString().c_str())); QObject::connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit); eventLoop.exec(); if (reply->error() != QNetworkReply::NoError) { qDebug() << "Network Error:" << reply->errorString(); return; } replyText = reply->readAll(); const QList<QPair<QByteArray, QByteArray>> headers = reply->rawHeaderPairs(); for each (auto pair in headers) { qDebug() << "header name:" << pair.first << "header data" << pair.second; } if (replyText.isNull() || replyText.isEmpty()) { qDebug() << "Answer for" << BOX_BASE_URL_HTTPS << "was empty or null"; this->_ui.statusBar->setStatusTip(QString("Answer for ").append(BOX_BASE_URL_HTTPS).append(" was empty or null")); return; } }
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QNetworkRequest req; QUrl *login_url = new QUrl("https://www.secure.pixiv.net/login.php?return_to=%2F"); req.setUrl(*login_url); req.setHeader(QNetworkRequest::UserAgentHeader, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36"); req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); QNetworkAccessManager man; QNetworkCookieJar *cookies = new QNetworkCookieJar(); man.setCookieJar(cookies); QByteArray *data = new QByteArray(); data->append("mode=login&return_to=http%3A%2F%2Fwww.pixiv.net%2F&pixiv_id=beta168921%40gmail.com&pass=xjy168921&skip=1"); QNetworkReply *reply = man.post(req,*data); QObject::connect(reply,&QNetworkReply::finished, [&reply, &man](){ QTextStream qerr(stderr); QList<QNetworkReply::RawHeaderPair> headers(reply->rawHeaderPairs()); for (int i = 0; i < headers.size(); ++i) { qerr << "HEADER: " << headers[i].first << " CONTENT: " << headers[i].second << "\n"; } // man(reply->manager()); // qerr << "\n\n"; // QList<QNetworkCookie> cookie(man->cookieJar()->cookiesForUrl(QUrl("http://www.pixiv.net"))); // for (int i = 0; i > cookie.size(); ++i) { // qerr << cookie[i].toRawForm(); // } // qerr << man.cookieJar()->cookiesForUrl(QUrl("http://www.pixiv.net")); }); // QTextStream qerr(stderr); return a.exec(); }