Qt http
一、服务器端
二、客户端
1.Pro里添加
Qt += network
2.引入头文件
#include <QNetworkAccessManager> #include <QNetworkRequest> #include <QNetworkReply> #include <QHttpPart> #include <QHttpMultiPart>
3.初始化
auto manager = new QNetworkAccessManager(this); connect(manager, &QNetworkAccessManager::finished, this, &MainWidget::receiveReply);
4.接收槽函数
void MainWidget::receiveReply(QNetworkReply *reply) { qDebug()<<"operation:"<<reply->operation(); //路径 qDebug()<<"url:"<<reply->url(); //状态码 const int status_code=reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); qDebug()<<"status code:"<<status_code; if(status_code==0 && reply->errorString()=="Operation canceled"){ qDebug()<<"timeout"; } if(reply->error()!=QNetworkReply::NoError){ //处理中的错误信息 qDebug()<<"reply error:"<<reply->errorString(); }else{ //获取响应信息并展示到ui,这里没有解析 const QByteArray reply_data=reply->readAll(); qDebug()<<"read all:"<<reply_data; } reply->deleteLater(); }
5.get
//构建请求对象 QNetworkRequest request; request.setUrl(QUrl(ui->editGetUrl->text())); //设置请求头 //已知头类型参见文档QNetworkRequest::KnownHeaders request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json"); //自定义类型 request.setRawHeader("self-header","gongjianbo"); //发送请求 manager->get(request);
6.post
//构建请求对象 QNetworkRequest request; request.setUrl(QUrl(ui->editPostUrl->text())); //设置请求头 request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json"); //发送请求 manager->post(request,ui->editPostData->toPlainText().toUtf8());
参考:https://blog.csdn.net/gongjianbo1992/article/details/97568863
长风破浪会有时,直挂云帆济沧海!
可通过下方链接找到博主
https://www.cnblogs.com/judes/p/10875138.html