添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
#include "QtTestRestFulDemo.h"
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QtCore/QJsonObject>
#include <QtCore/QJsonDocument>
QtTestRestFulDemo::QtTestRestFulDemo(QWidget *parent)
	: QMainWindow(parent)
	ui.setupUi(this);
	connect(ui.pushButton_GET, SIGNAL(clicked()), this, SLOT(OnGETSlot()));
	connect(ui.pushButton_POST, SIGNAL(clicked()), this, SLOT(OnPOSTSlot()));
void QtTestRestFulDemo::SendAndGetText(QString strUrl, MyMethod thod, QString strInput, QString &strMessage,QString &strResult)
	QNetworkRequest oNetRequest;
	oNetRequest.setUrl(QUrl(strUrl));
	oNetRequest.setRawHeader("Content-Type", "application/octet-stream");
	//oNetRequest.setRawHeader("Content-Type", "application/json");
	//oNetRequest.setRawHeader("Content-Type", "application/xml");
	//oNetRequest.setRawHeader("Content-Type", "application/octet-stream");
	//oNetRequest.setRawHeader("Authorization", "Bearer global-0b9ad652-9671-4cd8-86e9-e1b288dfe1da"); //token时用
	oNetRequest.setRawHeader("CLIENTVERSION", ("V1"));
	//oNetReply = oNetAccessManager.put(oNetRequest, *request.getRequestData());
	//oNetReply = oNetAccessManager.post(oNetRequest, *request.getRequestData());
	//oNetReply = oNetAccessManager.get(oNetRequest);
	//oNetReply = oNetAccessManager.deleteResource(oNetRequest);
	QNetworkAccessManager oNetAccessManager;
	QNetworkReply* oNetReply = NULL;
	QByteArray inputTmp;
	if (thod == POST)
		oNetReply = oNetAccessManager.post(oNetRequest, strInput.toLocal8Bit());
	else if (thod == GET )
		oNetReply = oNetAccessManager.get(oNetRequest);
	QEventLoop loop;
	connect(oNetReply, SIGNAL(finished()), &loop, SLOT(quit()));
	loop.exec();
	//记录httpCode
	int httpsCode = oNetReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
	QNetworkReply::NetworkError e = oNetReply->error();
	strResult = oNetReply->readAll();
	if (e)
		strMessage = oNetReply->errorString();
void QtTestRestFulDemo::OnPOSTSlot()
	//传Json时可以用下列操作
	QJsonObject oSendObject;
	oSendObject.insert("id1", 1);
	oSendObject.insert("name1", "a");
	QJsonObject oSendObject2;
	oSendObject2.insert("id2", 2);
	oSendObject2.insert("name2", "b");
	int Tmp1 = oSendObject.value("id1").toInt();
	oSendObject.insert("second", oSendObject2);
	int Tmp2 = oSendObject.value("second").toObject().value("id2").toInt();
	if ( oSendObject.contains("name1") )
		int h = 1;
	QJsonDocument doc;
	doc.setObject(oSendObject);
	QByteArray body = doc.toJson();
	QString str = body;
	QString strMessage;
	QString strResult;
	QString strUrl;
	QString strInput = ui.textEdit_input->toPlainText();
	strUrl += "http://" + ui.lineEdit_Url->text();
	SendAndGetText(strUrl, POST, strInput,strMessage,strResult);
	ui.textEdit_errorMessage->setText(strMessage);
	ui.textEdit_Result->setText(strResult);
void QtTestRestFulDemo::OnGETSlot()
	QString strMessage;
	QString strResult;
	QString strUrl;
	QString strInput = ui.textEdit_input->toPlainText();
	strUrl += "http://" + ui.lineEdit_Url->text();
	SendAndGetText(strUrl, GET, strInput, strMessage, strResult);
	ui.textEdit_errorMessage->setText(strMessage);
	ui.textEdit_Result->setText(strResult);

代码下载地址:https://download.csdn.net/download/u014378771/11452227

GitHub代码下载:https://github.com/xiefgen/QTRestFulDemo.git

代码测试结果:

GET方法测试:

GET方法查下测试

POST方法测试:input内容

{"article":{"content":"GET,POST,PUT","id":2,"title":"the way to REST"}}

服务器是用Python的flask实现的,代码如下:

from flask import Flask,jsonify,abort,make_response,request,json
app = Flask(__name__)
articles = [
        'id' : 1,
        'title':'the way to pythoon',
        'content':'tuple,list,dict'
        'id' : 2,
        'title':'the way to REST',
        'content':'GET,POST,PUT'
@app.route('/api/articles',methods = ['GET'])
def get_articles():
    return jsonify( { 'articles':articles } )
@app.route('/api/articles/<int:article_id>',methods = ['GET'])
def get_article(article_id):
    article = list(filter( lambda a : a['id'] == article_id,articles ))
    return  jsonify({'article':article[0]})
@app.route('/api/json',methods = ['POST'])
def getRequest():
    data = json.loads(request.get_data())
    return data['article']
if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8877)
                    #include "QtTestRestFulDemo.h"#include &lt;QtNetwork/QNetworkRequest&gt;#include &lt;QtNetwork/QNetworkReply&gt;#include &lt;QtCore/QJsonObject&gt;#include &lt;QtCore/QJsonDocument&gt;QtTestRes...
				
Qt+Cutelyst学习笔记(二十七)win10+Qt5.15.2+qmake实现一个简单的RESTful服务器 示例源码 https://blog.csdn.net/aggs1990/article/details/124115368 CSDN审核可能较慢,如无法下载,可以过段时间再回来看下
Qt http同步请求测试(restfull中常用的get/post/put/delete、文件上传/下载) 示例源码 https://blog.csdn.net/aggs1990/article/details/124104548 CSDN审核可能较慢,如无法下载,可以过段时间再回来看下
REST与RESTful web services是从网络获取数据的最常用的方法。 Qt和V-Player提供了简单的方法来通过REST来获取数据。 这篇文章将告诉你创建App与连接到REST服务的重要步骤。另外,也将会提供一些有用的代码。 Spoiler:V-Player简单的REST例子 在我们进入到创建整个示例App的细节之前,让我们先看一个小型App的代码。 函数#g...
在 Java 以及其他语言中,处理与后端的 HTTP 通讯,有专门的工具库,使用起来特别方便。比如使用 Java 中的 Retrifit/OkHttp,只需要声明一下服务 API 接口,就可以基于它与后端通信了。而且 OkHttp 提供了拦截器模式,可以支持自定义策略模式。这些功能能不能在 Qt/C++ 里面实现呢? public interface GitHubService { @GET("users/{user}/repos") Call<List<Repo&g...
AnılÖzbek于2019年1月12日编辑了该页面 This tutorial was written by Daniel Nicoletti and was first published oncutelyst.organdhis personal blog. Creating REST... 关于如何创建完整的工程,可以参考笔者之前的文档,在此不再重复 Qt+Cutelyst学习笔记(二十四)win10+Qt5.15.2+qmake实现一个简单的http服务器_aggs1990的博客-CSDN博客 Qt+Cutelyst学习笔记(二十五)win10+Qt5.15.2+qmake实现一个简单的https服务器_aggs1990的博客-CSDN博客
基于Redfish的服务器管理小应用前言关于ipmi关于Redfish关于RedfishAPI效果开发环境功能介绍技术Qt样式表信号槽treeWidget的使用https请求及解析QSlite数据库的使用参考 本文介绍了近期完成的一个小项目——基于Redfish的服务器管理小应用。一方面记录一下自己的开发经历;另一方面本项目适用于准备学习样式表,信号槽,treeWidget,https请求,SQlite数据库的人。代码在最下方。 注:公司内部网络才可以访问redfish接口,项目的网络请求无法发送成.