-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkuaipanhttp.cpp
99 lines (87 loc) · 2.63 KB
/
kuaipanhttp.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "kuaipanhttp.h"
using namespace Kuaipan;
Http::Http(QObject *parent) :
QObject(parent)
{
;
}
QString Http::get(QString url,QMap<QString, QString> params)
{
_buffer = "";
QNetworkAccessManager manager(0);
url = _makeStdBaseUrl(url) + _makeQuery(params);
_reply = manager.get(QNetworkRequest(QUrl(url)));
QObject::connect(_reply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()));
QObject::connect(_reply, SIGNAL(finished()), this, SLOT(httpFinished()));
_readLoop.exec();
return _buffer;
}
bool Http::downloadGet(QString url,QMap<QString, QString> params, QString filePath)
{
_downloadfile = new QFile(filePath);
_downloadfile->open(QFile::ReadWrite);
//QNetworkAccessManager manager(0);
url = _makeStdBaseUrl(url) + _makeQuery(params);
QString cmd = "wget -c -O \"" + filePath + "\" \"" + url + "\"";
system(cmd.toUtf8());
//_reply = manager.get(QNetworkRequest(QUrl(url)));
//QObject::connect(_reply, SIGNAL(readyRead()), this, SLOT(downFileReadyRead()));
//QObject::connect(_reply, SIGNAL(finished()), this, SLOT(downFileFinished()));
return true;
}
bool Http::uploadPost(QString url,QMap<QString, QString> params,QString filePath)
{
url = _makeStdBaseUrl(url) + _makeQuery(params);
QString cmd = "curl -F \"file=@" + filePath + "\" \"" + url + "\"";
system(cmd.toUtf8());
return true;
}
QString Http::post(QString url,QMap<QString, QString> params)
{
_buffer = "";
QNetworkAccessManager manager(0);
QString query = _makeQuery(params);
_reply = manager.post(QNetworkRequest(QUrl(url)),query.toUtf8());
QObject::connect(_reply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()));
QObject::connect(_reply, SIGNAL(finished()), this, SLOT(httpFinished()));
_readLoop.exec();
return _buffer;
}
QString Http::_makeQuery(QMap<QString, QString> params)
{
QStringList paramList;
QMap<QString,QString>::iterator it;
for ( it = params.begin(); it != params.end(); ++it ) {
paramList << QUrl::toPercentEncoding(it.key()) + "=" + QUrl::toPercentEncoding(it.value());
}
return paramList.join('&');
}
QString Http::_makeStdBaseUrl(QString url)
{
if(url.indexOf("?")>0){
if(url.right(1)!="?" && url.right(1)!="&"){
return url+"&";
}
}else{
return url+"?";
}
return url;
}
void Http::httpReadyRead()
{
QString string(_reply->readAll());
_buffer+=string;
}
void Http::httpFinished()
{
_readLoop.exit();
_reply->deleteLater();
}
void Http::downFileReadyRead()
{
_downloadfile->write(_reply->readAll());
}
void Http::downFileFinished()
{
_downloadfile->close();
}