C++ (Qt)void Thread::GET(QNetworkAccessManager &qnam, int howReadAnswer, int headers, int redirect, QByteArray contentType, bool repeat){ QEventLoop loopGet; int badProxyCount=0; request.setRawHeader("Content-Type", contentType); REPEATGET: reply = qnam.get(request); reply->ignoreSslErrors(); //if (proxyType == "NoUseProxy")// QTimer timer;// timer.start(30000);// connect(&timer, SIGNAL(timeout()), &loopGet, SLOT(quit())); QTimer::singleShot(60000, &loopGet, SLOT(quit())); connect(reply, SIGNAL(finished()), &loopGet, SLOT(quit())); loopGet.exec(); //if (proxyType == "NoUseProxy")// if (timer.isActive())// timer.stop(); //reply->deleteLater(); if (reply->error()) { qDebug() << "REPLY ERROR:" << reply->errorString() << reply->error(); if (repeat && badProxyCount<3) { badProxyCount++; qDebug() << "badProxyCountGet: " << badProxyCount; delete reply; goto REPEATGET; } } ///////////////////////////////// ЗАПИСЬ ЗАГОЛОВКОВ НАЧАЛО //////////////////////// replyHeaders.clear(); QList<QByteArray> listHeaders = reply->rawHeaderList(); foreach(QByteArray nameHeader, listHeaders) { QByteArray valueHeader = reply->rawHeader(nameHeader); replyHeaders.insert(nameHeader, valueHeader); }///////////////////////////////// ЗАПИСЬ ЗАГОЛОВКОВ КОНЕЦ //////////////////////// if (headers == showHeaders) HTTPHEADERS(reply); location = QByteArray::fromPercentEncoding(reply->rawHeader("Location")); if (!reply->rawHeader("Ajax-Location").isEmpty()) { location = QByteArray::fromPercentEncoding(reply->rawHeader("Ajax-Location")); } if (redirect == autoRedirect) { if (!location.isEmpty()) { lastUrl = reply->url().toString(); redirectCount = 0; REDIRECT(qnam, howReadAnswer, showHeaders); return; } } // if (readAnswer) {// if (reply->rawHeader("Content-Encoding").contains("gzip")) { //if (gzip)// answer = uncompress(reply->readAll());// } else {// answer = reply->readAll();// }// } else {// answerBytes = reply->readAll();// } answer.clear(); answerBytes.clear(); if (howReadAnswer == readAnswerToString) { if (reply->rawHeader("Content-Encoding").contains("gzip")) { //if (gzip) answer = uncompress(reply->readAll()); } else { answer = reply->readAll(); } } else if (howReadAnswer == readAnswerToByteArray) { if (reply->rawHeader("Content-Encoding").contains("gzip")) { //if (gzip) answerBytes = uncompress(reply->readAll()); } else { answerBytes = reply->readAll(); } } else if (howReadAnswer == readAnswerToOther) { if (reply->rawHeader("Content-Type").contains("image/gif")) { if (reply->rawHeader("Content-Encoding").contains("gzip")) { //if (gzip) answerBytes = uncompress(reply->readAll()); } else { answerBytes = reply->readAll(); } } else { if (reply->rawHeader("Content-Encoding").contains("gzip")) { //if (gzip) answer = uncompress(reply->readAll()); } else { answer = reply->readAll(); } } } lastUrl = reply->url().toString(); delete reply;} void Thread::POST(QNetworkAccessManager &qnam, int howReadAnswer, int headers, int redirect, QByteArray contentType, bool repeat){ QEventLoop loopPost; int badProxyCount=0; REPEATPOST: request.setRawHeader("Content-Type", contentType); reply = qnam.post(request, postData); reply->ignoreSslErrors(); //if (proxyType == "NoUseProxy")// QTimer timer;// timer.start(30000);// connect(&timer, SIGNAL(timeout()), &loopPost, SLOT(quit())); QTimer::singleShot(60000, &loopPost, SLOT(quit())); connect(reply, SIGNAL(finished()), &loopPost, SLOT(quit())); loopPost.exec(); //if (proxyType == "NoUseProxy")// if (timer.isActive())// timer.stop(); //reply->deleteLater(); if (reply->error()) { qDebug() << "REPLY ERROR:" << reply->errorString() << reply->error(); if (repeat && badProxyCount<3) { badProxyCount++; qDebug() << "badProxyCountPost: " << badProxyCount; delete reply; goto REPEATPOST; } } ///////////////////////////////// ЗАПИСЬ ЗАГОЛОВКОВ НАЧАЛО //////////////////////// replyHeaders.clear(); QList<QByteArray> listHeaders = reply->rawHeaderList(); foreach(QByteArray nameHeader, listHeaders) { QByteArray valueHeader = reply->rawHeader(nameHeader); replyHeaders.insert(nameHeader, valueHeader); }///////////////////////////////// ЗАПИСЬ ЗАГОЛОВКОВ КОНЕЦ //////////////////////// if (headers == showHeaders) HTTPHEADERS(reply); location = QByteArray::fromPercentEncoding(reply->rawHeader("Location")); if (!reply->rawHeader("Ajax-Location").isEmpty()) { location = QByteArray::fromPercentEncoding(reply->rawHeader("Ajax-Location")); } if (redirect == autoRedirect) { if (!location.isEmpty()) { lastUrl = reply->url().toString(); redirectCount = 0; REDIRECT(qnam, howReadAnswer, showHeaders); return; } } // if (readAnswer) {// if (reply->rawHeader("Content-Encoding").contains("gzip")) { //if (gzip)// answer = uncompress(reply->readAll());// } else {// answer = reply->readAll();// }// } if (howReadAnswer == readAnswerToString) { if (reply->rawHeader("Content-Encoding").contains("gzip")) { //if (gzip) answer = uncompress(reply->readAll()); } else { answer = reply->readAll(); } } else if (howReadAnswer == readAnswerToByteArray) { if (reply->rawHeader("Content-Encoding").contains("gzip")) { //if (gzip) answerBytes = uncompress(reply->readAll()); } else { answerBytes = reply->readAll(); } } lastUrl = reply->url().toString(); delete reply;} void Thread::REDIRECT(QNetworkAccessManager &qnam, int howReadAnswer, int headers){ QUrl redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();// if (redirectUrl.isEmpty()) {// if (reply->rawHeader("Ajax-Location") == "registrationConfirm.html") {// redirectUrl = reply->url().resolved(QUrl(QString(reply->rawHeader("Ajax-Location"))));// //referer = redirectUrl.toEncoded();// } else {// redirectUrl = QUrl(QString(reply->rawHeader("Ajax-Location")));// request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");// request.setRawHeader("Wicket-Ajax", QByteArray());// request.setRawHeader("Content-Type", QByteArray());// }// } qDebug() << "redirect url do: " << redirectUrl; //request.setRawHeader("Referer", "");// if (redirectUrl.toEncoded().startsWith("/")) {// redirectUrl = reply->url().resolved(redirectUrl);// qDebug() << "redirect url posle: " << redirectUrl;// } if (!redirectUrl.isEmpty() && redirectUrl.isRelative()) { //redirectUrl.toEncoded().startsWith("/") redirectUrl = reply->url().resolved(redirectUrl); qDebug() << "redirect url posle: " << redirectUrl; } else if (!reply->rawHeader("Ajax-Location").isEmpty()) {// if (redirectUrl.toString().endsWith(".html")) { request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); request.setRawHeader("Wicket-Ajax", QByteArray());// } redirectUrl = reply->url().resolved(QUrl::fromPercentEncoding(reply->rawHeader("Ajax-Location"))); qDebug() << "redirect url Ajax-Location posle: " << redirectUrl; } else if (redirectUrl.isEmpty()) { qDebug() << endl << this->objectName() << endl << endl << "NO REDIRECT!!!" << endl << endl << endl; return; } //request.setRawHeader("Referer", lastUrl.toUtf8()); request.setRawHeader("Content-Type", QByteArray()); request.setUrl(redirectUrl); QEventLoop loopRedirect; int badProxyCount=0; delete reply; REPEATREDIRECT: reply = qnam.get(request); reply->ignoreSslErrors(); //if (proxyType == "NoUseProxy")// QTimer timer;// timer.start(30000);// connect(&timer, SIGNAL(timeout()), &loopRedirect, SLOT(quit())); QTimer::singleShot(60000, &loopRedirect, SLOT(quit())); connect(reply, SIGNAL(finished()), &loopRedirect, SLOT(quit())); loopRedirect.exec(); //if (proxyType == "NoUseProxy")// if (timer.isActive())// timer.stop(); //reply->deleteLater(); qDebug() << reply->url(); if (reply->error()) { qDebug() << "REPLY ERROR:" << reply->errorString() << reply->error(); if (badProxyCount<3) { badProxyCount++; qDebug() << "badProxyCountRedirect: " << badProxyCount; delete reply; goto REPEATREDIRECT; } } ///////////////////////////////// ЗАПИСЬ ЗАГОЛОВКОВ НАЧАЛО //////////////////////// replyHeaders.clear(); QList<QByteArray> listHeaders = reply->rawHeaderList(); foreach(QByteArray nameHeader, listHeaders) { QByteArray valueHeader = reply->rawHeader(nameHeader); replyHeaders.insert(nameHeader, valueHeader); }///////////////////////////////// ЗАПИСЬ ЗАГОЛОВКОВ КОНЕЦ //////////////////////// if (headers == showHeaders) HTTPHEADERS(reply); if ((!reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl().isEmpty() || !reply->rawHeader("Ajax-Location").isEmpty()) && redirectCount < 33) { redirectCount++; REDIRECT(qnam, howReadAnswer, showHeaders); } else {// if (readAnswer) {// if (reply->rawHeader("Content-Encoding").contains("gzip")) { //if (gzip)// answer = uncompress(reply->readAll());// } else {// answer = reply->readAll();// }// } if (howReadAnswer == readAnswerToString) { if (reply->rawHeader("Content-Encoding").contains("gzip")) { //if (gzip) answer = uncompress(reply->readAll()); } else { answer = reply->readAll(); } } else if (howReadAnswer == readAnswerToByteArray) { if (reply->rawHeader("Content-Encoding").contains("gzip")) { //if (gzip) answerBytes = uncompress(reply->readAll()); } else { answerBytes = reply->readAll(); } } locationRedirect = QByteArray::fromPercentEncoding(reply->rawHeader("Location")); if (!reply->rawHeader("Ajax-Location").isEmpty()) { locationRedirect = QByteArray::fromPercentEncoding(reply->rawHeader("Ajax-Location")); } lastUrl = reply->url().toString(); delete reply; }} void Thread::HTTPHEADERS(QNetworkReply *reply){ if (!SHOWHEADERS) return; qDebug() << endl << this->objectName() << reply->url(); qDebug() << "Request headers: "; QList<QByteArray> reqHeaders = reply->request().rawHeaderList(); //QList<QByteArray> reqHeaders = request.rawHeaderList(); foreach (QByteArray reqName, reqHeaders) { QByteArray reqValue = reply->request().rawHeader(reqName); //QByteArray reqValue = request.rawHeader(reqName); qDebug() << reqName << ": " << reqValue; } qDebug() << "Reply headers: "; QList<QByteArray> reqHeadersReply = reply->rawHeaderList(); foreach( QByteArray reqName, reqHeadersReply ) { QByteArray reqValue = reply->rawHeader( reqName ); qDebug() << reqName << ": " << reqValue; }} QByteArray Thread::uncompress(const QByteArray &data){ if (data.size() <= 4) { qWarning("uncompress: Input data is truncated"); return QByteArray(); } QByteArray result; int ret; z_stream strm; static const int CHUNK_SIZE = 1024; char out[CHUNK_SIZE]; /* allocate inflate state */ strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; strm.avail_in = data.size(); strm.next_in = (Bytef*)(data.data()); ret = inflateInit2(&strm, 15 + 32); // gzip decoding if (ret != Z_OK) return QByteArray(); // run inflate() do { strm.avail_out = CHUNK_SIZE; strm.next_out = (Bytef*)(out); ret = inflate(&strm, Z_NO_FLUSH); Q_ASSERT(ret != Z_STREAM_ERROR); // state not clobbered switch (ret) { case Z_NEED_DICT: ret = Z_DATA_ERROR; // and fall through case Z_DATA_ERROR: case Z_MEM_ERROR: (void)inflateEnd(&strm); return QByteArray(); } result.append(out, CHUNK_SIZE - strm.avail_out); } while (strm.avail_out == 0); // clean up and return inflateEnd(&strm); return result;}
C++ (Qt)QByteArray & QByteArray::append ( const QByteArray & ba )