-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientHttp.cpp
183 lines (154 loc) · 5.08 KB
/
ClientHttp.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "ClientHttp.h"
ClientHttp::ClientHttp()
{
}
//To connect to the server and post data(username, Password) and file
void ClientHttp::ConnectServer()
{
m_oNetworkManager = new QNetworkAccessManager(this);
QUrl m_oUrl(m_sUrlServer);
QString bound="margin";
QByteArray m_bDataToSend(QString("--" + bound + "\r\n").toUtf8());
m_bDataToSend.append("--" + bound + "--\r\n");
QNetworkRequest m_oRequest = QNetworkRequest(m_oUrl);
m_oRequest.setRawHeader("User", m_sUserName.toUtf8());
m_oRequest.setRawHeader("Password", m_sUserPassword.toUtf8());
m_oRequest.setRawHeader(QString("Content-Type").toUtf8(),QString("multipart/form-data; boundary=" + bound).toUtf8());
m_oRequest.setRawHeader(QString("Content-Lenght").toUtf8(), QString::number(m_bDataToSend.length()).toUtf8());
QEventLoop loop;
m_oReply = m_oNetworkManager->post(m_oRequest, m_bDataToSend);
connect(m_oNetworkManager, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
loop.exec();
DataRetrieve(m_oReply);
}
// Connect to the server and get a response
void ClientHttp::CommunicationChannel()
{
m_oNetworkManager = new QNetworkAccessManager(this);
QUrl m_oUrl(m_sUrlSite);
m_oReply = m_oNetworkManager->get(QNetworkRequest(m_oUrl));
QEventLoop loop;
connect(m_oReply, SIGNAL(readyRead()), &loop, SLOT(quit()));
loop.exec();
DataRetrieve(m_oReply);
}
void ClientHttp::UploadFileToServer()
{
m_oNetworkManager = new QNetworkAccessManager(this);
QUrl m_oUrl(m_sUrlServer);
QString bound="margin";
QByteArray m_bDataToSend(QString("--" + bound + "\r\n").toUtf8());
//file to upload
QFile m_oMyFile(m_sFileNameCom);
if(!m_oMyFile.open(QIODevice::ReadOnly))
{
return;
}
m_bDataToSend.append(m_oMyFile.readAll());
m_bDataToSend.append("--" + bound + "--\r\n");
QNetworkRequest m_oRequest = QNetworkRequest(m_oUrl);
m_oRequest.setRawHeader(QString("Content-Lenght").toUtf8(), QString::number(m_bDataToSend.length()).toUtf8());
QEventLoop loop;
m_oReply = m_oNetworkManager->post(m_oRequest, m_bDataToSend);
connect(m_oNetworkManager, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
loop.exec();
DataRetrieve(m_oReply);
}
QString ClientHttp::ErrorDescription(QNetworkReply::NetworkError m_sErr)
{
QString m_sResult;
switch(m_sErr)
{
case QNetworkReply::NoError:
qDebug() << "No Error";
m_sResult = "No Error";
break;
case QNetworkReply::ConnectionRefusedError:
qDebug() << "The connection is not allowed";
m_sResult = "The connection is not allowed";
break;
case QNetworkReply::HostNotFoundError:
qDebug() << "The Host is not Found";
m_sResult = "The Host is not Found";
break;
case QNetworkReply::TimeoutError:
qDebug() << "Connection Time Out";
m_sResult = "Connection Time Out";
break;
case QNetworkReply::RemoteHostClosedError:
qDebug() << "The remote Host is closed";
m_sResult = "The remote Host is closed";
break;
default:
qDebug() << "There is an error"+m_sErr;
m_sResult = "There is an error"+m_sErr;
}
return m_sResult;
}
void ClientHttp::DataRetrieve(QNetworkReply *m_oReplyVal)
{
qDebug() << "Hello" << m_oReplyVal->isFinished() << m_oReplyVal->error() << "\n";
if(m_oReplyVal){
m_bVal = true;
if(m_oReplyVal->error() == QNetworkReply::NoError) //If there is no error
{
qDebug() << "Received Network Reply\n";
qDebug() << "Here is the error: " << ErrorDescription(m_oReplyVal->error()) << "\n";
QByteArray m_oData = m_oReplyVal->readAll();
str = m_oData;
qDebug() << m_oData;
}else //there is an error
{
m_bVal = true;
qDebug() << "Here is the error: " << ErrorDescription(m_oReplyVal->error()) << "\n";
}
m_oReplyVal->deleteLater();
}else{
m_bVal = false;
qDebug() << "Error";
qDebug() << "Here is the error: " << ErrorDescription(m_oReplyVal->error());
}
}
QString ClientHttp::GetUrl(QString m_sValPath)
{
return m_sUrlServer+"/"+m_sValPath;
}
/*QFile ClientHttp::GetFile()
{
QFile m_oFile(m_sFileNameCom);
return m_oFile;
}*/
void ClientHttp::SetServer(QString m_sServerUrl)
{
m_sUrlServer = m_sServerUrl;
}
void ClientHttp::SetUser(QString m_sUser, QString m_sPass)
{
m_sUserName = m_sUser;
m_sUserPassword = m_sPass;
}
bool ClientHttp::IsOk()
{
return m_bVal;
}
void ClientHttp::SetUrl(QString m_sSiteUrl)
{
m_sUrlSite = m_sSiteUrl;
}
void ClientHttp::SetUserName(QString m_sNameUser)
{
m_sUserName = m_sNameUser;
}
void ClientHttp::SetUserPassword(QString m_sPasswordUser)
{
m_sUserPassword = m_sPasswordUser;
}
void ClientHttp::SetFileName(QString m_sNameFile)
{
m_sFileNameCom = m_sNameFile;
}
void ClientHttp::PostFile(QString m_sUrl, QString m_sFileName)
{
m_sUrlServer = m_sUrl;
m_sFileNameCom = m_sFileName;
}