FluNetwork add Head Request

This commit is contained in:
朱子楚\zhuzi 2023-12-08 23:30:41 +08:00
parent 33c203fcb3
commit 6c86e61916
4 changed files with 47 additions and 6 deletions

View File

@ -64,6 +64,20 @@ FluContentPage{
.go(callable) .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{ FluButton{
implicitWidth: parent.width implicitWidth: parent.width
implicitHeight: 36 implicitHeight: 36

View File

@ -65,6 +65,20 @@ FluContentPage{
.go(callable) .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{ FluButton{
implicitWidth: parent.width implicitWidth: parent.width
implicitHeight: 36 implicitHeight: 36

View File

@ -227,23 +227,26 @@ void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){
disconnect(conn_quit); disconnect(conn_quit);
} }
QString response; QString response;
if(reply->isOpen()){ if(params->_method == NetworkParams::METHOD_HEAD){
response = QString::fromUtf8(reply->readAll()); response = headerList2String(reply->rawHeaderPairs());
}else{
if(reply->isOpen()){
response = QString::fromUtf8(reply->readAll());
}
} }
QNetworkReply::NetworkError error = reply->error(); int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(error == QNetworkReply::NoError){ if(httpStatus == 200){
if(!callable.isNull()){ if(!callable.isNull()){
if(params->_cacheMode != FluNetworkType::CacheMode::NoCache){ if(params->_cacheMode != FluNetworkType::CacheMode::NoCache){
saveResponse(cacheKey,response); saveResponse(cacheKey,response);
} }
callable->success(response); callable->success(response);
} }
printRequestEndLog(request,params,reply,response); printRequestEndLog(request,params,reply,response);
break; break;
}else{ }else{
if(i == params->getRetry()-1){ if(i == params->getRetry()-1){
if(!callable.isNull()){ if(!callable.isNull()){
int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(params->_cacheMode == FluNetworkType::CacheMode::RequestFailedReadCache && cacheExists(cacheKey)){ if(params->_cacheMode == FluNetworkType::CacheMode::RequestFailedReadCache && cacheExists(cacheKey)){
if(!callable.isNull()){ if(!callable.isNull()){
callable->cache(readCache(cacheKey)); callable->cache(readCache(cacheKey));
@ -401,6 +404,14 @@ QString FluNetwork::getCacheFilePath(const QString& key){
return cacheDir.absoluteFilePath(key); return cacheDir.absoluteFilePath(key);
} }
QString FluNetwork::headerList2String(const QList<QNetworkReply::RawHeaderPair>& 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<QString, QVariant>& map){ QString FluNetwork::map2String(const QMap<QString, QVariant>& map){
QStringList parameters; QStringList parameters;
for (auto it = map.constBegin(); it != map.constEnd(); ++it) { for (auto it = map.constBegin(); it != map.constEnd(); ++it) {

View File

@ -7,6 +7,7 @@
#include <QJsonValue> #include <QJsonValue>
#include <QJSValue> #include <QJSValue>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QNetworkReply>
#include "Def.h" #include "Def.h"
#include "stdafx.h" #include "stdafx.h"
#include "singleton.h" #include "singleton.h"
@ -135,6 +136,7 @@ private:
bool cacheExists(const QString& key); bool cacheExists(const QString& key);
QString getCacheFilePath(const QString& key); QString getCacheFilePath(const QString& key);
QString map2String(const QMap<QString, QVariant>& map); QString map2String(const QMap<QString, QVariant>& map);
QString headerList2String(const QList<QNetworkReply::RawHeaderPair>& data);
void printRequestStartLog(QNetworkRequest request,NetworkParams* params); void printRequestStartLog(QNetworkRequest request,NetworkParams* params);
void printRequestEndLog(QNetworkRequest request,NetworkParams* params,QNetworkReply*& reply,const QString& response); void printRequestEndLog(QNetworkRequest request,NetworkParams* params,QNetworkReply*& reply,const QString& response);
public: public: