├── httpclient.cpp └── httpclient.h /httpclient.cpp: -------------------------------------------------------------------------------- 1 | #include "cookieshandler.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | HttpClient::HttpClient(QObject *parent) : QObject(parent) 11 | { 12 | m_manager = new QNetworkAccessManager(this); 13 | m_manager->setCookieJar(new QNetworkCookieJar(this)); 14 | m_textCodec = NULL; 15 | } 16 | 17 | HttpClient::~HttpClient() 18 | { 19 | delete m_manager; 20 | } 21 | 22 | QString HttpClient::post(const QString &url, const QByteArray &postData) 23 | { 24 | QNetworkRequest request; 25 | request.setUrl(QUrl(url)); 26 | request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); 27 | 28 | QNetworkReply *reply = m_manager->post(request, postData); 29 | waitForFinish(reply); 30 | 31 | // check redirect 32 | QByteArray location = reply->rawHeader(QString("Location").toAscii()); 33 | if (location.size() > 0) { 34 | return get(QString(location)); 35 | } else { 36 | QByteArray replyData = reply->readAll(); 37 | if (m_textCodec != NULL) { 38 | return m_textCodec->toUnicode(replyData); 39 | } else { 40 | return QString(replyData); 41 | } 42 | } 43 | } 44 | 45 | QString HttpClient::post(const QString &url, QMap &postData) 46 | { 47 | QStringList data; 48 | foreach (QString key, postData.keys()) { 49 | data.append(key + "=" + postData[key]); 50 | } 51 | return post(url, data.join("&").toAscii()); 52 | } 53 | 54 | QString HttpClient::get(const QString &url) 55 | { 56 | QNetworkRequest request; 57 | request.setUrl(QUrl(url)); 58 | QNetworkReply* reply = m_manager->get(request); 59 | waitForFinish(reply); 60 | 61 | // check redirect 62 | QByteArray location = reply->rawHeader(QString("Location").toAscii()); 63 | if (location.size() > 0) { 64 | return get(QString(location)); 65 | } else { 66 | QByteArray replyData = reply->readAll(); 67 | if (m_textCodec != NULL) { 68 | return m_textCodec->toUnicode(replyData); 69 | } else { 70 | return QString(replyData); 71 | } 72 | } 73 | } 74 | 75 | void HttpClient::setTextCodec(const QString &encoding) 76 | { 77 | m_textCodec = QTextCodec::codecForName(encoding.toAscii()); 78 | } 79 | 80 | QMap HttpClient::allCookies(const QString &url) 81 | { 82 | QMap map; 83 | QList list = m_manager->cookieJar()->cookiesForUrl(QUrl(url)); 84 | foreach (QNetworkCookie cookie, list) { 85 | map.insert(QString(cookie.name()), QString(cookie.value())); 86 | } 87 | return map; 88 | } 89 | 90 | QMap HttpClient::defaultValuesFromName(const QString &html, const QString &name) 91 | { 92 | QMap map; 93 | 94 | // get form 95 | QRegExp rx(".*"); 96 | rx.setMinimal(true); 97 | if (rx.indexIn(html, 0) == -1) { 98 | return map; 99 | } 100 | QString form = rx.cap(0); 101 | 102 | // get inputs 103 | rx.setPattern("<(input|select).*name=\"([^\"]+)\".*(value=\"([^\"]+)\")?.*/?>"); 104 | int pos = 0; 105 | while ((pos = rx.indexIn(form, pos)) != -1) { 106 | map.insert(rx.cap(2), rx.cap(4)); 107 | pos += rx.matchedLength(); 108 | } 109 | 110 | return map; 111 | } 112 | 113 | void HttpClient::waitForFinish(QNetworkReply *reply) 114 | { 115 | QEventLoop loop; 116 | connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); 117 | loop.exec(); 118 | } 119 | -------------------------------------------------------------------------------- /httpclient.h: -------------------------------------------------------------------------------- 1 | #ifndef COOKIESHANDLER_H 2 | #define COOKIESHANDLER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | class SleepThread : public QThread 10 | { 11 | public: 12 | static void usleep(int usec) {QThread::usleep(usec);} 13 | static void msleep(int msec) {QThread::msleep(msec);} 14 | static void sleep(int sec) {QThread::sleep(sec);} 15 | }; 16 | 17 | class HttpClient : public QObject 18 | { 19 | Q_OBJECT 20 | 21 | public: 22 | explicit HttpClient(QObject *parent = 0); 23 | ~HttpClient(); 24 | 25 | QString post(const QString &url, const QByteArray &postData); 26 | QString post(const QString &url, QMap &postData); 27 | QString get(const QString &url); 28 | void setTextCodec(const QString &encoding); 29 | QMap allCookies(const QString &url); 30 | QMap defaultValuesFromName(const QString &html, const QString &name); 31 | 32 | private: 33 | void waitForFinish(QNetworkReply *reply); 34 | 35 | QNetworkAccessManager *m_manager; 36 | QTextCodec *m_textCodec; 37 | }; 38 | 39 | #endif // COOKIESHANDLER_H 40 | --------------------------------------------------------------------------------