#include "include/HttpClient.h" #include "include/httpThreadNum1_global.h" HttpClient::HttpClient() { } //传文件用的,暂时没有用 bool HttpClient::post(QString strUrl, QString filePath, QString *errString) { bool Result = true; const QUrl url = QUrl::fromUserInput(strUrl); m_Manager_ = new QNetworkAccessManager(this); QNetworkRequest network_request; network_request.setUrl(url); QFile *file = new QFile(filePath); file->open(QIODevice::ReadOnly); QFileInfo info(file->fileName()); QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::RelatedType); //http发送文件 QHttpPart filePart; filePart.setBodyDevice(file); file->setParent(multiPart); filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=%1;filename=%2").arg(info.fileName()).arg(info.fileName()))); multiPart->append(filePart); network_request.setRawHeader("fileName",info.fileName().toLatin1()); QNetworkReply* reply = m_Manager_->post(network_request,multiPart); multiPart->setParent(reply); QEventLoop eventLoop; connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit); eventLoop.exec(QEventLoop::ExcludeUserInputEvents); QByteArray replyData = reply->readAll(); int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); QVariant redirectAttr = reply->attribute(QNetworkRequest::RedirectionTargetAttribute); if (reply->error() || 300 == statusCode //状态码300 Multiple Choices,既不是错误也不算重定向,应该是qt bug || !redirectAttr.isNull()) { *errString = reply->error() ? reply->errorString() : QString("发生重定向(%1),不允许此情况").arg(statusCode); replyData.clear(); Result = false; } file->close(); delete file; delete multiPart; reply->deleteLater(); reply = nullptr; delete m_Manager_; return Result; } //连接服务器,发送和接收数据 //参数:url接口地址,请求信息data,错误信息,接收信息data bool HttpClient::postQString(QString strUrl, QString strName, QString *errString ,QString *getCardBox) { bool Result = true; const QUrl url = QUrl::fromUserInput(strUrl); m_Manager_QString = new QNetworkAccessManager(this); //http的固定请求类型 QNetworkRequest network_request; network_request.setUrl(url); QString passQStringJsonTemp=strName; QByteArray passQStringJson=passQStringJsonTemp.toUtf8(); //使用net请求的格式 network_request.setHeader(QNetworkRequest::ContentTypeHeader, ("application/json")); network_request.setRawHeader("binform-auth","b602b640be404c8497f0fecc01d1f814"); g_QMutex_token.lock(); network_request.setRawHeader("token",g_qsToken.toUtf8().data()); //将来扩展token使用 g_QMutex_token.unlock(); //使用qt标准post请求 QNetworkReply* reply = m_Manager_QString->post(network_request,passQStringJson); //循环接收,知道包完整在进行下一步,所以做了一个子线程 //这个循环是一个阻塞 QEventLoop eventLoop; connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit); eventLoop.exec(QEventLoop::ExcludeUserInputEvents); QByteArray replyData = reply->readAll(); // qDebug()<attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); QVariant redirectAttr = reply->attribute(QNetworkRequest::RedirectionTargetAttribute); // qDebug()<<"error"<error()<<"状态"<error() || 300 == statusCode //状态码300 Multiple Choices,既不是错误也不算重定向,应该是qt bug || !redirectAttr.isNull()) { *errString = reply->error() ? reply->errorString() : QString("发生重定向(%1),不允许此情况").arg(statusCode); replyData.clear(); Result = false; qDebug()<error()<<"错误"; } reply->deleteLater(); reply = nullptr; delete m_Manager_QString; return Result; }