From 08247bdadbf382409ca80bd897ed10aa5724c2ec Mon Sep 17 00:00:00 2001 From: Alexander-Porter Date: Tue, 9 Jul 2024 16:24:56 +0800 Subject: [PATCH] Refactor COSClient to support POST requests --- cosclient.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ cosclient.h | 11 +++++++++++ 2 files changed, 63 insertions(+) diff --git a/cosclient.cpp b/cosclient.cpp index 962abcf..51e8054 100644 --- a/cosclient.cpp +++ b/cosclient.cpp @@ -181,6 +181,30 @@ preResponse COSClient::invokeDeleteRequest(const QString& path, const preRequest return response; } +preResponse COSClient::invokePostRequest(const QString &path, const preRequest &request) +{ + preResponse response; + // 构建请求 + QNetworkRequest networkRequest = buildPostRequest(path, request.queryParams, request.data); // 使用提供的代码片段构建POST请求 + networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, request.contentType); + networkRequest.setRawHeader("Content-MD5",_getContentMD5(request.data).toUtf8()); + // 发送请求并等待响应 + QNetworkReply* reply = manager->post(networkRequest, request.data); + // 等待响应完成 + QEventLoop loop; + connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit); + loop.exec(); + // 处理响应 + response.data = reply->readAll(); + response.statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + const QList rawHeaderList = reply->rawHeaderList(); + for (const QByteArray& header : rawHeaderList) { + response.headers.insert(QString(header), QString(reply->rawHeader(header))); + } + reply->deleteLater(); + return response; +} + QString COSClient::buildTagXmlFromMap(const QMap &map) { QDomDocument doc; QDomElement root = doc.createElement("Tagging"); @@ -461,3 +485,31 @@ QNetworkRequest COSClient::buildDeleteRequest(const QString &path, const QMap queryParams, const QByteArray &data) +{ + if (!preCheckSession()) + { + return QNetworkRequest(); + } + QUrl url; + QUrlQuery query; + url=QUrl(generalApiUrl+path); + for(auto it=queryParams.begin();it!=queryParams.end();it++) + { + query.addQueryItem(it.key(),it.value()); + } + url.setQuery(query); + QNetworkRequest request(url); + request.setRawHeader("Host",endpoint.toUtf8()); + //设置请求方法 + request.setAttribute(QNetworkRequest::CustomVerbAttribute,"POST"); + QString sig=this->signHelper->generateSignature(request,300); + request.setRawHeader("Authorization",sig.toUtf8()); + if(token!="") + { + request.setRawHeader("x-cos-security-token",token.toUtf8()); + } + return request; +} + + diff --git a/cosclient.h b/cosclient.h index 91ebabb..f96b631 100644 --- a/cosclient.h +++ b/cosclient.h @@ -19,7 +19,16 @@ struct preRequest{ struct preResponse{ QByteArray data; QMap headers; + QMap getMetaDatas(){ + // x-cos-meta-* QMap metaDatas; + for(auto it=headers.begin();it!=headers.end();it++){ + if(it.key().startsWith("x-cos-meta-")){ + metaDatas[it.key().mid(11)]=it.value(); + } + } + return metaDatas; + }; int statusCode; }; class COSClient : public QObject @@ -73,10 +82,12 @@ class COSClient : public QObject QNetworkRequest buildPutRequest(const QString& path,const QMap queryParams, const QByteArray& data); QNetworkRequest buildHeadRequest(const QString& path,const QMap queryParams); QNetworkRequest buildDeleteRequest(const QString& path,const QMap queryParams); + QNetworkRequest buildPostRequest(const QString& path,const QMap queryParams, const QByteArray& data); preResponse invokeGetFileRequest(const QString& path, const preRequest& request); preResponse invokePutRequest(const QString& path, const preRequest& request); preResponse invokeHeadRequest(const QString& path, const preRequest& request); preResponse invokeDeleteRequest(const QString& path, const preRequest& request); + preResponse invokePostRequest(const QString& path, const preRequest& request); // 构建XML字符串 QString buildTagXmlFromMap(const QMap &map);