Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

@@

Sign up Sign up Sign in Sign in


Files Commits Branches
hnaohiro / qt-httpclient
g
Code Code Network Network Pull Requests Pull Requests Issues Issues Graphs Graphs
++
00 00
Tags
yy master master
1
Explore Explore Features Features Enterprise Enterprise Blog Blog
1 0
Star Star
yy Fork Fork
This repository This repository @@
branch branch
Search or type a comman
qt-httpclient/httpclient.cpp at master hnaohiro/qt-httpcl... https://github.com/hnaohiro/qt-httpclient
1 of 3 06/30/2013 08:27 PM
qt-httpclient / httpclient.cpp _
hnaohiro 5 months ago first
1 contributor
__ file file 119 lines (102 sloc) 119 lines (102 sloc) 3.191 kb 3.191 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "cookieshandler.h"
#include <QApplication>
#include <QEventLoop>
#include <QRegExp>
#include <QStringList>
#include <QtNetwork/QNetworkCookieJar>
#include <QtNetwork/QNetworkRequest>
HttpClient::HttpClient(QObject *parent) : QObject(parent)
{
m_manager = new QNetworkAccessManager(this);
m_manager->setCookieJar(new QNetworkCookieJar(this));
m_textCodec = NULL;
}
HttpClient::~HttpClient()
{
delete m_manager;
}
QString HttpClient::post(const QString &url, const QByteArray &postData)
{
QNetworkRequest request;
request.setUrl(QUrl(url));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QNetworkReply *reply = m_manager->post(request, postData);
waitForFinish(reply);
// check redirect
QByteArray location = reply->rawHeader(QString("Location").toAscii());
if (location.size() > 0) {
return get(QString(location));
} else {
QByteArray replyData = reply->readAll();
if (m_textCodec != NULL) {
return m_textCodec->toUnicode(replyData);
} else {
return QString(replyData);
}
}
}
QString HttpClient::post(const QString &url, QMap<QString, QString> &postData)
{
QStringList data;
foreach (QString key, postData.keys()) {
data.append(key + "=" + postData[key]);
}
return post(url, data.join("&").toAscii());
}
QString HttpClient::get(const QString &url)
{
QNetworkRequest request;
request.setUrl(QUrl(url));
QNetworkReply* reply = m_manager->get(request);
waitForFinish(reply);
Edit Edit Raw Raw Blame Blame History History
qt-httpclient/httpclient.cpp at master hnaohiro/qt-httpcl... https://github.com/hnaohiro/qt-httpclient
2 of 3 06/30/2013 08:27 PM
Status Developer Training Shop Blog About 2013 GitHub, Inc. Terms Privacy Security Contact
[
qt-httpclient/httpclient.cpp at master hnaohiro/qt-httpcl... https://github.com/hnaohiro/qt-httpclient
3 of 3 06/30/2013 08:27 PM

You might also like