1.客户端
头文件
#ifndef SQL_MYWIDGET_H
#define SQL_MYWIDGET_H
#include <QWidget>
#include <QNetworkAccessManager>
#include <QNetworkReply>
QT_BEGIN_NAMESPACE
namespace Ui { class MyWidget; }
QT_END_NAMESPACE
class MyWidget : public QWidget {
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = nullptr);
~MyWidget() override;
private:
Ui::MyWidget *ui;
QNetworkAccessManager* networkManager;
QNetworkReply* getReply;
QNetworkReply* postReply;
private slots:
void sendGetRequest();
void sendPostRequest();
void processGetReply();
void processPostReply();
};
#endif //SQL_MYWIDGET_H
实现类
#include <QJsonDocument>
#include <QJsonObject>
#include <QUrlQuery>
#include "MyWidget.h"
#include "ui_MyWidget.h"
MyWidget::MyWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MyWidget) {
ui->setupUi(this);
this->networkManager = new QNetworkAccessManager(this);
connect(this->ui->getButton,&QPushButton::clicked,this,&MyWidget::sendGetRequest);
connect(this->ui->postButton,&QPushButton::clicked,this,&MyWidget::sendPostRequest);
}
MyWidget::~MyWidget() {
delete ui;
delete networkManager;
}
void MyWidget::sendGetRequest(){
QNetworkRequest request;
QString url = "http://127.0.0.1:8080/api/processGet";
url.append("?param1=param1¶m2=param2");
request.setUrl(QUrl(url));
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
this->getReply = this->networkManager->get(request);
connect(this->getReply,&QNetworkReply::finished,this,&MyWidget::processGetReply);
}
void MyWidget::processGetReply(){
if (this->getReply->error() == QNetworkReply::NoError){
QByteArray array = this->getReply->readAll();
qInfo()<<"data:"<<QString::fromUtf8(array)<<"\n";
}else{
qInfo()<<"error:"<<this->getReply->error();
}
this->getReply->deleteLater();
}
void MyWidget::sendPostRequest(){
QNetworkRequest request;
QString url = "http://127.0.0.1:8080/api/processPost";
request.setUrl(QUrl(url));
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
/* 设置参数 */
/* 这种方式传递json字符串,用@RequestBody接收 */
// QJsonDocument document;
// QJsonObject obj;
// obj.insert("param1", "postParam1");
// obj.insert("param2", "postParam2");
// document.setObject(obj);
// QByteArray postData = document.toJson(QJsonDocument::Compact);
// this->postReply = this->networkManager->post(request,postData);
/* 这种方式传递param1=value1¶m2=value2,用@RequestParam接收 */
QUrlQuery postData;
postData.addQueryItem("param1", "value1");
postData.addQueryItem("param2", "value2");
QByteArray postDataBytes = postData.toString(QUrl::FullyEncoded).toUtf8();
this->postReply = this->networkManager->post(request,postDataBytes);
/* 参数未写入url中 */
qInfo()<<request.url().toString();
qInfo()<<postDataBytes;
connect(this->postReply,&QNetworkReply::finished,this,&MyWidget::processPostReply);
}
void MyWidget::processPostReply(){
if (this->postReply->error() == QNetworkReply::NoError){
QByteArray array = this->postReply->readAll();
qInfo()<<"data:"<<QString::fromUtf8(array)<<"\n";
}else{
qInfo()<<"error:"<<this->postReply->error();
}
this->postReply->deleteLater();
}
2.服务端
package org.qt.request.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author lzy
*/
@Controller
@ResponseBody
public class TestController {
@RequestMapping(value = "/api/processGet",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
public String processGet(@RequestParam("param1")String param1,@RequestParam("param2")String param2){
String result = "收到请求:param1="+param1+",param2="+param2;
System.out.println(result);
return result;
}
@RequestMapping(value = "/api/processPost",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
public String processPost(@RequestParam("param1")String param1,@RequestParam("param2")String param2){
String result = "收到请求:param1="+param1+",param2="+param2;
System.out.println(result);
return result;
}
}