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{ FluButton{
text:"Get请求" text:"Get请求"
onClicked: { onClicked: {
http_download.cancel() http_get.get({q:"FluentUI"})
// http_get.get({q:"FluentUI"})
} }
} }
FluButton{ FluButton{

View File

@ -1,11 +1,12 @@
#include "FluHttp.h" #include "FluHttp.h"
#include <QThreadPool> #include <QThreadPool>
#include "HttpClient.h" #include <QEventLoop>
#include <QNetworkReply>
#include <QUrlQuery>
#include <QHttpMultiPart>
#include "FluApp.h" #include "FluApp.h"
using namespace AeaQt;
FluHttp::FluHttp(QObject *parent) FluHttp::FluHttp(QObject *parent)
: QObject{parent} : QObject{parent}
{ {
@ -28,80 +29,8 @@ void FluHttp::handleReply(QNetworkReply* reply){
_cache.append(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){ void FluHttp::post(QVariantMap params,QVariantMap headers){
QVariantMap data = invokeIntercept(params,headers,"post").toMap(); QVariantMap data = invokeIntercept(params,headers,"post").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::postJson(QVariantMap params,QVariantMap headers){
QVariantMap data = invokeIntercept(params,headers,"postJson").toMap();
QThreadPool::globalInstance()->start([=](){ QThreadPool::globalInstance()->start([=](){
Q_EMIT start(); Q_EMIT start();
QNetworkAccessManager manager; QNetworkAccessManager manager;
@ -145,16 +74,21 @@ void FluHttp::get(QVariantMap params,QVariantMap headers){
addQueryParam(&url,data["params"].toMap()); addQueryParam(&url,data["params"].toMap());
QNetworkRequest request(url); QNetworkRequest request(url);
addHeaders(&request,data["headers"].toMap()); 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); QNetworkReply* reply = manager.get(request);
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit); _cache.append(reply);
loop.exec(); loop->exec();
if (reply->error() == QNetworkReply::NoError) { if (reply->error() == QNetworkReply::NoError) {
Q_EMIT success(QString::fromUtf8(reply->readAll())); Q_EMIT success(QString::fromUtf8(reply->readAll()));
}else{ }else{
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString()); Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
} }
loop->deleteLater();
reply->deleteLater(); reply->deleteLater();
loop = nullptr;
reply = nullptr; reply = nullptr;
Q_EMIT finish(); Q_EMIT finish();
}); });

View File

@ -27,8 +27,6 @@ public:
Q_SIGNAL void downloadProgress(qint64 recv, qint64 total); Q_SIGNAL void downloadProgress(qint64 recv, qint64 total);
Q_INVOKABLE void get(QVariantMap params = {},QVariantMap headers = {}); Q_INVOKABLE void get(QVariantMap params = {},QVariantMap headers = {});
Q_INVOKABLE void post(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 download(QString path,QVariantMap params = {},QVariantMap headers = {});
Q_INVOKABLE void cancel(); Q_INVOKABLE void cancel();
private: private:

File diff suppressed because it is too large Load Diff