Qt使用Http协议
在使用Qt框架进行应用开发的过程中,很多时候需要进行客户端与服务端的网络通信,这时候就需要Qt的internet模块。而网络通信中最常用的协议就是http协议,Qt对http协议的调用进行了封装,使用非常方便。这里对常用的http请求demo做一下总结,方便大家参考。
在使用internet模块时需要在pro文件中添加对应的模块。
QT += network
Qt中使用Http协议与服务端通信的请求主要分为GET和POST,GET是从指定的资源请求数据,而POST是向指定的资源提交要被处理的数据。
常用的请求类型包括五类:
1.多个独立参数GET请求
2.参数数组GET请求
3.上传参数数据POST请求
4.上传参数和文件POST请求
5.下载文件GET请求文章来源:https://www.toymoban.com/news/detail-629681.html
下面通过简单的Demo描述五种请求的调用方式。文章来源地址https://www.toymoban.com/news/detail-629681.html
多个独立参数GET请求
int NormalGetRequest(QString paramer1, QString paramer2)
{
//生成对应的网络请求
QNetworkRequest request;
QString scheme = "http";
QString serverAddr = "192.168.0.1";
QString port = "80";
QString requestHeader = scheme + QString("://") + serverAddr + QString(":") + port;
QString fullRequest = requestHeader + QString("/api/vi/user/getrequest?paramer1=%1¶mer2=%2").arg(paramer1).arg(paramer2);
request.setUrl(QUrl(fullRequest));
//发送Get请求
QNetworkAccessManager manager;
QNetworkReply *reply = manager.get(request);
QEventLoop eventLoop;
QObject::connect(reply, SIGNAL(finished()),&eventLoop, SLOT(quit()));
eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
//对请求的返回异常进行处理
if(reply->error() != QNetworkReply::NoError)
{
return reply->error();
}
//解析返回的Json结果
QByteArray replyData = reply->readAll();
QJsonParseError json_error;
QJsonDocument jsonDoc(QJsonDocument::fromJson(replyData, &json_error));
if(json_error.error != QJsonParseError::NoError)
{
return -1;
}
QJsonObject rootObj = jsonDoc.object();
QString codeStr = rootObj.value("code").toString();
if (codeStr.compare("200") == 0)
{
//返回代码为200的时候证明请求成功对包含的结构数据进行处理
if(rootObj.contains("result"))
{
}
return 0;
}
else
{
//请求失败对对应的处理
return codeStr.toInt();
}
}
参数数组GET请求
int GetRequestWithArray(QVector<QString> m_filePathVector)
{
QJsonArray array;
for(int index=0; index<m_filePathVector.size(); ++index)
{
QJsonObject object;
object.insert("index",QString::number(index));
object.insert("path",m_filePathVector.at(index));
array.append(object);
}
QJsonDocument document;
document.setArray(array);
QString arrayString = document.toJson(QJsonDocument::Compact);
//生成对应的网络请求
QNetworkRequest request;
QString scheme = "http";
QString serverAddr = "192.168.0.1";
QString port = "80";
QString requestHeader = scheme + QString("://") + serverAddr + QString(":") + port;
QString fullRequest = requestHeader + QString("/api/vi/user/getarray?paramers=%1").arg(arrayString);
request.setUrl(QUrl(fullRequest));
//获取错误
QNetworkAccessManager manager;
QNetworkReply *reply = manager.get(request);
QEventLoop eventLoop;
QObject::connect(reply, SIGNAL(finished()),&eventLoop, SLOT(quit()));
eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
if(reply->error() != QNetworkReply::NoError)
{
return reply->error();
}
//解析返回的Json结果
QByteArray replyData = reply->readAll();
QJsonParseError json_error;
QJsonDocument jsonDoc(QJsonDocument::fromJson(replyData, &json_error));
if(json_error.error != QJsonParseError::NoError)
{
return -1;
}
QJsonObject rootObj = jsonDoc.object();
QString codeStr = rootObj.value("code").toString();
if (codeStr.compare("200") == 0)
{
//返回代码为200的时候证明请求成功对包含的结构数据进行处理
if(rootObj.contains("result"))
{
}
return 0;
}
else
{
//请求失败对对应的处理
return codeStr.toInt();
}
}
上传参数数据POST请求
int NormalPostRequest(QString paramer1, QString paramer2)
{
//生成对应的网址请求
QNetworkRequest request;
QString scheme = "http";
QString serverAddr = "192.168.0.1";
QString port = "80";
QString requestHeader = scheme + QString("://") + serverAddr + QString(":") + port;
QString fullRequest = requestHeader + "/api/v1/user/postrequest";
request.setUrl(QUrl(fullRequest));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
//获取对应的参数数据
QByteArray data;
data.append("paramer1=");
data.append(paramer1.toUtf8());
data.append("¶mer2=");
data.append(paramer2.toUtf8());
//发送请求
QNetworkAccessManager manager;
QNetworkReply *reply = manager.post(request,data);
QEventLoop eventLoop;
QObject::connect(reply, SIGNAL(finished()),&eventLoop, SLOT(quit()));
eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
if(reply->error() != QNetworkReply::NoError)
{
return reply->error();
}
//解析返回的Json结果
QByteArray replyData = reply->readAll();
QJsonParseError json_error;
QJsonDocument jsonDoc(QJsonDocument::fromJson(replyData, &json_error));
if(json_error.error != QJsonParseError::NoError)
{
return -1;
}
QJsonObject rootObj = jsonDoc.object();
QString codeStr = rootObj.value("code").toString();
if (codeStr.compare("200") == 0)
{
//返回代码为200的时候证明请求成功对包含的结构数据进行处理
if(rootObj.contains("result"))
{
}
return 0;
}
else
{
//请求失败对对应的处理
return codeStr.toInt();
}
上传参数和文件POST请求
int PostRequestWithFile(QString paramer1, QString paramer2,QString filePath)
{
//生成对应的网址请求
QFileInfo fileInfo(filePath);
QString fileName =fileInfo.fileName();
QFile* inputFile = new QFile(filePath);
inputFile->open(QIODevice::ReadOnly);
//multipart请求
QHttpMultiPart* multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
//文件块
QHttpPart filePart;
filePart.setHeader(QNetworkRequest::ContentDispositionHeader,
QVariant(QString("form-data; name="uploadfile";filename="%1"").arg(fileName)));
filePart.setBodyDevice(inputFile);
inputFile->setParent(multiPart);
multiPart->append(filePart);
QHttpPart paramer1Part;
paramer1Part.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant("form-data; name="paramer1""));
paramer1Part.setBody(paramer1.toUtf8());
multiPart->append(paramer1Part);
QHttpPart paramer2Part;
paramer2Part.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant("form-data; name="paramer2""));
paramer2Part.setBody(paramer2.toUtf8());
multiPart->append(paramer2Part);
//生成对应的网址请求
QNetworkRequest request;
QString scheme = "http";
QString serverAddr = "192.168.0.1";
QString port = "80";
QString requestHeader = scheme + QString("://") + serverAddr + QString(":") + port;
QString fullRequest = requestHeader + "/api/v1/user/postrequestwithfile";
request.setUrl(QUrl(fullRequest));
//发送请求
QNetworkAccessManager manager;
QNetworkReply *reply = manager.post(request,multiPart);
multiPart->setParent(reply);
QEventLoop eventLoop;
QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)),&eventLoop, SLOT(quit()));
eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
if(reply->error() != QNetworkReply::NoError)
{
return reply->error();
}
//解析返回的Json结果
QByteArray replyData = reply->readAll();
QJsonParseError json_error;
QJsonDocument jsonDoc(QJsonDocument::fromJson(replyData, &json_error));
if(json_error.error != QJsonParseError::NoError)
{
return -1;
}
QJsonObject rootObj = jsonDoc.object();
QString codeStr = rootObj.value("code").toString();
if (codeStr.compare("200") == 0)
{
//返回代码为200的时候证明请求成功对包含的结构数据进行处理
if(rootObj.contains("result"))
{
}
return 0;
}
else
{
//请求失败对对应的处理
return codeStr.toInt();
}
}
下载文件GET请求
int DownloadFileFromWeb(QString fileUrl,QString&fileSavePath)
{
QNetworkRequest request;
QUrl url(fileUrl);
QFileInfo fileInfo(url.path());
QString fileName = fileInfo.fileName();
QNetworkAccessManager *accessManager=new QNetworkAccessManager();
request.setUrl(url);
QNetworkReply *reply = accessManager->get(request);
QEventLoop loop;
QObject::connect(accessManager, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
//如果需要下载进度需要关联对应的信号
//connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(LoadProgress(qint64, qint64)));
loop.exec(QEventLoop::ExcludeUserInputEvents);
//获取下载返回值
QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
if (statusCode.toInt() != 200)
{
return -1;
}
//保存下载的文件
QFile file(fileSavePath);
if(!file.open(QIODevice::WriteOnly))
{
return -2;
}
file.write(reply->readAll());
file.close();
return 0;
}
到了这里,关于Qt中运用HTTP协议的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!