This commit is contained in:
朱子楚\zhuzi 2023-07-23 20:56:04 +08:00
parent e4908e409b
commit 169ca17a6a
4 changed files with 13 additions and 2392 deletions

View File

@ -96,8 +96,7 @@ FluScrollablePage{
FluButton{
text:"Get请求"
onClicked: {
http_download.cancel()
// http_get.get({q:"FluentUI"})
http_get.get({q:"FluentUI"})
}
}
FluButton{

View File

@ -1,11 +1,12 @@
#include "FluHttp.h"
#include <QThreadPool>
#include "HttpClient.h"
#include <QEventLoop>
#include <QNetworkReply>
#include <QUrlQuery>
#include <QHttpMultiPart>
#include "FluApp.h"
using namespace AeaQt;
FluHttp::FluHttp(QObject *parent)
: QObject{parent}
{
@ -28,42 +29,6 @@ void FluHttp::handleReply(QNetworkReply* reply){
_cache.append(reply);
}
void FluHttp::postString(QString params,QVariantMap headers){
QVariantMap data = invokeIntercept(params,headers,"postString").toMap();
QThreadPool::globalInstance()->start([=](){
Q_EMIT start();
QNetworkAccessManager manager;
QUrl url(_url);
QNetworkRequest request(url);
addHeaders(&request,data["headers"].toMap());
QHttpMultiPart* multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QString contentType = QString("multipart/form-data;boundary=%1").arg(multiPart->boundary());
request.setHeader(QNetworkRequest::ContentTypeHeader, contentType);
for (const auto& each : data["params"].toMap().toStdMap())
{
const QString& key = each.first;
const QString& value = each.second.toString();
QString dispositionHeader = QString("form-data; name=\"%1\"").arg(key);
QHttpPart part;
part.setHeader(QNetworkRequest::ContentDispositionHeader, dispositionHeader);
part.setBody(value.toUtf8());
multiPart->append(part);
}
QEventLoop loop;
QNetworkReply* reply = manager.post(request,multiPart);
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() == QNetworkReply::NoError) {
Q_EMIT success(QString::fromUtf8(reply->readAll()));
}else{
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
}
reply->deleteLater();
reply = nullptr;
Q_EMIT finish();
});
}
void FluHttp::post(QVariantMap params,QVariantMap headers){
QVariantMap data = invokeIntercept(params,headers,"post").toMap();
QThreadPool::globalInstance()->start([=](){
@ -100,42 +65,6 @@ void FluHttp::post(QVariantMap params,QVariantMap headers){
});
}
void FluHttp::postJson(QVariantMap params,QVariantMap headers){
QVariantMap data = invokeIntercept(params,headers,"postJson").toMap();
QThreadPool::globalInstance()->start([=](){
Q_EMIT start();
QNetworkAccessManager manager;
QUrl url(_url);
QNetworkRequest request(url);
addHeaders(&request,data["headers"].toMap());
QHttpMultiPart* multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QString contentType = QString("multipart/form-data;boundary=%1").arg(multiPart->boundary());
request.setHeader(QNetworkRequest::ContentTypeHeader, contentType);
for (const auto& each : data["params"].toMap().toStdMap())
{
const QString& key = each.first;
const QString& value = each.second.toString();
QString dispositionHeader = QString("form-data; name=\"%1\"").arg(key);
QHttpPart part;
part.setHeader(QNetworkRequest::ContentDispositionHeader, dispositionHeader);
part.setBody(value.toUtf8());
multiPart->append(part);
}
QEventLoop loop;
QNetworkReply* reply = manager.post(request,multiPart);
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() == QNetworkReply::NoError) {
Q_EMIT success(QString::fromUtf8(reply->readAll()));
}else{
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
}
reply->deleteLater();
reply = nullptr;
Q_EMIT finish();
});
}
void FluHttp::get(QVariantMap params,QVariantMap headers){
QVariantMap data = invokeIntercept(params,headers,"get").toMap();
QThreadPool::globalInstance()->start([=](){
@ -145,16 +74,21 @@ void FluHttp::get(QVariantMap params,QVariantMap headers){
addQueryParam(&url,data["params"].toMap());
QNetworkRequest request(url);
addHeaders(&request,data["headers"].toMap());
QEventLoop loop;
QEventLoop *loop = new QEventLoop();
connect(&manager,&QNetworkAccessManager::finished,this,[=](QNetworkReply *reply){
loop->quit();
});
QNetworkReply* reply = manager.get(request);
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
_cache.append(reply);
loop->exec();
if (reply->error() == QNetworkReply::NoError) {
Q_EMIT success(QString::fromUtf8(reply->readAll()));
}else{
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
}
loop->deleteLater();
reply->deleteLater();
loop = nullptr;
reply = nullptr;
Q_EMIT finish();
});

View File

@ -27,8 +27,6 @@ public:
Q_SIGNAL void downloadProgress(qint64 recv, qint64 total);
Q_INVOKABLE void get(QVariantMap params = {},QVariantMap headers = {});
Q_INVOKABLE void post(QVariantMap params = {},QVariantMap headers = {});
Q_INVOKABLE void postJson(QVariantMap params = {},QVariantMap headers = {});
Q_INVOKABLE void postString(QString params = "",QVariantMap headers = {});
Q_INVOKABLE void download(QString path,QVariantMap params = {},QVariantMap headers = {});
Q_INVOKABLE void cancel();
private:

File diff suppressed because it is too large Load Diff