C++ (Qt)void writeSocket (QTcpSocket *socket, const QString &data){ socket->write ( (data + "\n").toUtf8 ());}
C++ (Qt)void Functions::GET(QNetworkAccessManager &qnam, bool redirect, bool readAnswer, bool showHeaders){ QEventLoop loopGet; int badProxyCount=0; request.setRawHeader("Content-Type", QByteArray()); REPEATGET: reply = qnam.get(request); //if (proxyType == "NoUseProxy")// QTimer timer;// timer.start(30000);// connect(&timer, SIGNAL(timeout()), &loopGet, SLOT(quit())); QObject::connect(reply, SIGNAL(finished()), &loopGet, SLOT(quit())); loopGet.exec(); //if (proxyType == "NoUseProxy")// if (timer.isActive())// timer.stop(); reply->deleteLater(); if (reply->error()) { qDebug() << reply->errorString() << reply->error(); if (badProxyCount<3) { badProxyCount++; qDebug() << "badProxyCountGet: " << badProxyCount; goto REPEATGET; } } if (showHeaders) HTTPHEADERS(reply); if (redirect) { REDIRECT(qnam, readAnswer, showHeaders); return; } if (readAnswer) { if (reply->rawHeader("Content-Encoding").contains("gzip")) { //if (gzip) answer = uncompress(reply->readAll()); } else { answer = reply->readAll(); } } lastUrl = reply->url().toString();} void Functions::POST(QNetworkAccessManager &qnam, bool redirect, bool readAnswer, bool showHeaders){ QEventLoop loopPost; int badProxyCount=0; REPEATPOST: request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); reply = qnam.post(request, postData); //if (proxyType == "NoUseProxy")// QTimer timer;// timer.start(30000);// connect(&timer, SIGNAL(timeout()), &loopPost, SLOT(quit())); QObject::connect(reply, SIGNAL(finished()), &loopPost, SLOT(quit())); loopPost.exec(); //if (proxyType == "NoUseProxy")// if (timer.isActive())// timer.stop(); reply->deleteLater(); if (reply->error()) { qDebug() << reply->errorString() << reply->error(); if (badProxyCount<3) { badProxyCount++; qDebug() << "badProxyCountPost: " << badProxyCount; goto REPEATPOST; } } if (showHeaders) HTTPHEADERS(reply); if (redirect) { REDIRECT(qnam, readAnswer, showHeaders); return; } if (readAnswer) { if (reply->rawHeader("Content-Encoding").contains("gzip")) { //if (gzip) answer = uncompress(reply->readAll()); } else { answer = reply->readAll(); } } lastUrl = reply->url().toString(); } void Functions::HTTPHEADERS(QNetworkReply *reply){ if (!SHOWHEADERS) return; qDebug() << endl << 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; }} void Functions::CLEARREQUEST(QNetworkRequest request){ //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; request.setRawHeader(reqName, QByteArray()); } }
C++ (Qt) public: void GET(QNetworkAccessManager&, bool redirect = false, bool readAnswer = false, bool showHeaders = false); void POST(QNetworkAccessManager&, bool redirect = false, bool readAnswer = false, bool showHeaders = false); void REDIRECT(QNetworkAccessManager&, bool readAnswer = false, bool showHeaders = false); void HTTPHEADERS(QNetworkReply*); void CLEARREQUEST(QNetworkRequest); QNetworkReply *reply; QNetworkRequest request; QString answer; QString lastUrl; QByteArray postData; bool SHOWHEADERS;