删除文章

确定要删除这篇文章吗?

取消
确定

QNetworkAccessManager http请求

     阅读(231)  2019-09-27 17:04:37

使用QNetworkAccessManager可以发起http请求,下面列举三种常见的场景:

get请求

这个很简单传一个url就可以了

QNetworkReply *reply = manager_->get(QNetworkRequest(QUrl(url)));

post请求

这里post数据使用表单的形式

    QUrl aurl(url);
    QNetworkRequest req(aurl);
    req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    QNetworkReply *reply = manager_->post(req, data);
    connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError)));

post上传文件

注意part2和part3不是必须的,这个根据服务器的需要来填,从这里可以看出QHttpMultiPart使用方法

    QHttpPart part1;
    part1.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png"));
    part1.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"0.png\""));
    part1.setBody(data);

    QHttpPart part2;
    part2.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"engine\""));
    part2.setBody("t");

    QHttpPart part3;
    part3.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"type\""));
    part3.setBody("ocrtype_tencent");

    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    multiPart->append(part1);
    multiPart->append(part2);
    multiPart->append(part3);

    QUrl aurl(url);
    QNetworkRequest req(aurl);
    QNetworkReply *reply = manager_->post(req, multiPart);
    multiPart->setParent(reply);

    connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError)));

完整代码:

#pragma once

#include <QObject>
#include <QNetworkReply>

class QNetworkAccessManager;
class HttpRequest : public QObject
{
    Q_OBJECT

public:
    HttpRequest(QObject *parent);
    ~HttpRequest();
    void get(const QString &url);
    void post(const QString &url, const QByteArray &data);
    void ocrUpload(const QString &url, const QByteArray &data);

signals:
    void sigHttpResponse(int err, const QByteArray &data);

private slots:
    void replyFinished(QNetworkReply*);
    void slotError(QNetworkReply::NetworkError);

private:
    QNetworkAccessManager *manager_;
};

#include "HttpRequest.h"
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QHttpMultiPart>

HttpRequest::HttpRequest(QObject *parent)
    : QObject(parent)
    , manager_(new QNetworkAccessManager(this))
{
    connect(manager_, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)), Qt::QueuedConnection);
}

HttpRequest::~HttpRequest()
{
}

void HttpRequest::get(const QString &url)
{
    QNetworkReply *reply = manager_->get(QNetworkRequest(QUrl(url)));
    connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError)));
}

void HttpRequest::post(const QString &url, const QByteArray &data)
{
    qDebug() << "url:" << url << "," << data;
    QUrl aurl(url);
    QNetworkRequest req(aurl);
    req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    QNetworkReply *reply = manager_->post(req, data);
    connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError)));
}

void HttpRequest::ocrUpload(const QString &url, const QByteArray &data)
{
    QHttpPart part1;
    part1.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png"));
    part1.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"0.png\""));
    part1.setBody(data);

    QHttpPart part2;
    part2.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"engine\""));
    part2.setBody("t");

    QHttpPart part3;
    part3.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"type\""));
    part3.setBody("ocrtype_tencent");

    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    multiPart->append(part1);
    multiPart->append(part2);
    multiPart->append(part3);

    QUrl aurl(url);
    QNetworkRequest req(aurl);
    QNetworkReply *reply = manager_->post(req, multiPart);
    multiPart->setParent(reply);

    connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError)));
}

void HttpRequest::replyFinished(QNetworkReply *reply)
{
    if (!reply) {
        LOGGER_ERROR("replyFinished is null");
        return;
    }

    emit sigHttpResponse(0, reply->readAll());
    reply->deleteLater();
}

void HttpRequest::slotError(QNetworkReply::NetworkError err)
{
    if (err != QNetworkReply::NoError) {
        LOGGER_ERROR("http response error:" << err);
    }
}

文章评论

Keep it simple,stupid
文章数
306
总访问量
539274
今日访问
1014
最近评论

tujiaw : registerThreadInactive:如果当前没有激活的线程,就去激活线程,让等待的线程去执行任务。
hgzzx : 佩服佩服。 请教:registerThreadInactive的作用是什么?
xuehaoyun : 很不错,来围观一下
tujiaw : 抱歉csdn code服务关闭了,这个代码我也找不到了
于淞 : 你好,这个文章的源码能分享一下吗,songsong9181@163.com,谢谢了 上面的写错了
于淞 : 你好,这个文章的源码能分享一下吗,838106303@163.com,谢谢了 上面的链接不能用了
tujiaw : 多谢多谢
essaypinglun college-paper.org : 很好的博客,赞赞
Folly : 这个实现有点奇怪,Qt为什么没有统一的比对方法。
过多s : alert("hello, world!")
回到顶部