43 lines
850 B
C++
43 lines
850 B
C++
#ifndef TCPREQUEST_H
|
|
#define TCPREQUEST_H
|
|
|
|
#include <QTcpSocket>
|
|
class TcpRequest {
|
|
public:
|
|
bool readFromSocket(QTcpSocket* socket);
|
|
QString getUrl();
|
|
QVariantMap getBodyParams();
|
|
QString getErrorString();
|
|
|
|
private:
|
|
QString url; // 请求路径
|
|
qint64 contentLength = 0; // 请求体长度
|
|
QString body; // 请求体
|
|
|
|
QString errorString;
|
|
|
|
private:
|
|
inline QVariantMap parseParams();
|
|
};
|
|
|
|
/**
|
|
* @brief 解析请求体参数
|
|
*/
|
|
inline QVariantMap TcpRequest::parseParams()
|
|
{
|
|
QVariantMap map;
|
|
QStringList list = body.split("&");
|
|
for (QString str : list) {
|
|
if (str.isEmpty()) {
|
|
continue;
|
|
} else {
|
|
QString key = str.split("=")[0];
|
|
QString value = str.split("=")[1];
|
|
map[key] = value;
|
|
}
|
|
}
|
|
return map;
|
|
}
|
|
|
|
#endif // TCPREQUEST_H
|