update network

This commit is contained in:
zhuzichu 2023-12-06 18:23:51 +08:00
parent eed96b4c0e
commit bd8a561590

View File

@ -391,36 +391,51 @@ void FluNetwork::sendRequest(QNetworkAccessManager* manager,QNetworkRequest requ
QByteArray verb = params->method2String().toUtf8(); QByteArray verb = params->method2String().toUtf8();
switch (params->_type) { switch (params->_type) {
case NetworkParams::TYPE_FORM:{ case NetworkParams::TYPE_FORM:{
QHttpMultiPart *multiPart = new QHttpMultiPart(); bool isFormData = !params->_fileMap.isEmpty();
multiPart->setContentType(QHttpMultiPart::FormDataType); if(isFormData){
for (const auto& each : params->_paramMap.toStdMap()) QHttpMultiPart *multiPart = new QHttpMultiPart();
{ multiPart->setContentType(QHttpMultiPart::FormDataType);
QHttpPart part; for (const auto& each : params->_paramMap.toStdMap())
part.setHeader(QNetworkRequest::ContentDispositionHeader, QString("form-data; name=\"%1\"").arg(each.first)); {
part.setBody(each.second.toByteArray()); QHttpPart part;
multiPart->append(part); part.setHeader(QNetworkRequest::ContentDispositionHeader, QString("form-data; name=\"%1\"").arg(each.first));
} part.setBody(each.second.toByteArray());
for (const auto& each : params->_fileMap.toStdMap()) multiPart->append(part);
{ }
QString filePath = each.second.toString(); for (const auto& each : params->_fileMap.toStdMap())
QString name = each.first; {
QFile *file = new QFile(filePath); QString filePath = each.second.toString();
QString fileName = QFileInfo(filePath).fileName(); QString name = each.first;
file->open(QIODevice::ReadOnly); QFile *file = new QFile(filePath);
file->setParent(multiPart); QString fileName = QFileInfo(filePath).fileName();
QHttpPart part; file->open(QIODevice::ReadOnly);
part.setHeader(QNetworkRequest::ContentDispositionHeader, QString("form-data; name=\"%1\"; filename=\"%2\"").arg(name,fileName)); file->setParent(multiPart);
part.setBodyDevice(file); QHttpPart part;
multiPart->append(part); part.setHeader(QNetworkRequest::ContentDispositionHeader, QString("form-data; name=\"%1\"; filename=\"%2\"").arg(name,fileName));
} part.setBodyDevice(file);
reply = manager->sendCustomRequest(request,verb,multiPart); multiPart->append(part);
multiPart->setParent(reply); }
if(!params->_fileMap.isEmpty()){ reply = manager->sendCustomRequest(request,verb,multiPart);
multiPart->setParent(reply);
connect(reply,&QNetworkReply::uploadProgress,reply,[callable](qint64 bytesSent, qint64 bytesTotal){ connect(reply,&QNetworkReply::uploadProgress,reply,[callable](qint64 bytesSent, qint64 bytesTotal){
if(!callable.isNull() && bytesSent!=0 && bytesTotal!=0){ if(!callable.isNull() && bytesSent!=0 && bytesTotal!=0){
callable->uploadProgress(bytesSent,bytesTotal); Q_EMIT callable->uploadProgress(bytesSent,bytesTotal);
} }
}); });
}else{
request.setHeader(QNetworkRequest::ContentTypeHeader, QString("application/x-www-form-urlencoded"));
QString value;
for (const auto& each : params->_paramMap.toStdMap())
{
value += QString("%1=%2").arg(QString(QUrl::toPercentEncoding(each.first)),QString(QUrl::toPercentEncoding(each.second.toString())));
value += "&";
}
if(!params->_paramMap.isEmpty()){
value.chop(1);
}
qDebug()<<value;
QByteArray data = value.toUtf8();
reply = manager->sendCustomRequest(request,verb,data);
} }
break; break;
} }