From 6c86e61916e13cd5a15b4914814ed84676963281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E5=AD=90=E6=A5=9A=5Czhuzi?= Date: Fri, 8 Dec 2023 23:30:41 +0800 Subject: [PATCH] FluNetwork add Head Request --- example/qml-Qt6/page/T_Network.qml | 14 ++++++++++++++ example/qml/page/T_Network.qml | 14 ++++++++++++++ src/FluNetwork.cpp | 23 +++++++++++++++++------ src/FluNetwork.h | 2 ++ 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/example/qml-Qt6/page/T_Network.qml b/example/qml-Qt6/page/T_Network.qml index 0b2c6f6c..e1f1acac 100644 --- a/example/qml-Qt6/page/T_Network.qml +++ b/example/qml-Qt6/page/T_Network.qml @@ -64,6 +64,20 @@ FluContentPage{ .go(callable) } } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Head" + onClicked: { + text_info.text = "" + FluNetwork.head("https://httpbingo.org/head") + .addQuery("name","孙悟空") + .addQuery("age",500) + .addQuery("address","花果山水帘洞") + .bind(root) + .go(callable) + } + } FluButton{ implicitWidth: parent.width implicitHeight: 36 diff --git a/example/qml/page/T_Network.qml b/example/qml/page/T_Network.qml index 1509e53a..f3e87548 100644 --- a/example/qml/page/T_Network.qml +++ b/example/qml/page/T_Network.qml @@ -65,6 +65,20 @@ FluContentPage{ .go(callable) } } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Head" + onClicked: { + text_info.text = "" + FluNetwork.head("https://httpbingo.org/head") + .addQuery("name","孙悟空") + .addQuery("age",500) + .addQuery("address","花果山水帘洞") + .bind(root) + .go(callable) + } + } FluButton{ implicitWidth: parent.width implicitHeight: 36 diff --git a/src/FluNetwork.cpp b/src/FluNetwork.cpp index 78d6a23b..a9d459a2 100644 --- a/src/FluNetwork.cpp +++ b/src/FluNetwork.cpp @@ -227,23 +227,26 @@ void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){ disconnect(conn_quit); } QString response; - if(reply->isOpen()){ - response = QString::fromUtf8(reply->readAll()); + if(params->_method == NetworkParams::METHOD_HEAD){ + response = headerList2String(reply->rawHeaderPairs()); + }else{ + if(reply->isOpen()){ + response = QString::fromUtf8(reply->readAll()); + } } - QNetworkReply::NetworkError error = reply->error(); - if(error == QNetworkReply::NoError){ + int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + if(httpStatus == 200){ if(!callable.isNull()){ if(params->_cacheMode != FluNetworkType::CacheMode::NoCache){ saveResponse(cacheKey,response); } - callable->success(response); + callable->success(response); } printRequestEndLog(request,params,reply,response); break; }else{ if(i == params->getRetry()-1){ if(!callable.isNull()){ - int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); if(params->_cacheMode == FluNetworkType::CacheMode::RequestFailedReadCache && cacheExists(cacheKey)){ if(!callable.isNull()){ callable->cache(readCache(cacheKey)); @@ -401,6 +404,14 @@ QString FluNetwork::getCacheFilePath(const QString& key){ return cacheDir.absoluteFilePath(key); } +QString FluNetwork::headerList2String(const QList& data){ + QJsonObject object; + for (auto it = data.constBegin(); it != data.constEnd(); ++it) { + object.insert(QString(it->first),QString(it->second)); + } + return QJsonDocument(object).toJson(QJsonDocument::Compact); +} + QString FluNetwork::map2String(const QMap& map){ QStringList parameters; for (auto it = map.constBegin(); it != map.constEnd(); ++it) { diff --git a/src/FluNetwork.h b/src/FluNetwork.h index 97316a7e..4a8ed8e3 100644 --- a/src/FluNetwork.h +++ b/src/FluNetwork.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "Def.h" #include "stdafx.h" #include "singleton.h" @@ -135,6 +136,7 @@ private: bool cacheExists(const QString& key); QString getCacheFilePath(const QString& key); QString map2String(const QMap& map); + QString headerList2String(const QList& data); void printRequestStartLog(QNetworkRequest request,NetworkParams* params); void printRequestEndLog(QNetworkRequest request,NetworkParams* params,QNetworkReply*& reply,const QString& response); public: