qt5构造Http请求(Get/Post)

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&param2=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&param2=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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黎汝聪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值