bool get(QNetworkRequest request);
bool post(QNetworkRequest request, const QByteArray &data);
QString errorCode() const;
QString text() const;
private:
void processReply(QNetworkReply *reply);
private:
QNetworkAccessManager *m_networkAccessManager = nullptr;
QNetworkReply::NetworkError m_error;
QString m_text; // 存放网络请求返回的文本字符串
QString m_errorString;//存放错误信息
int m_networkErrorRetry = 0; // 网络错误重试次数
QEventLoop m_eventLoop;
QTimer *m_timer = nullptr;
QByteArray m_data; // 存放网络请求返回的原始数据
#endif // HTTPCLIENT_H
HttpClient.cpp
#include "HttpClient.h"
HttpClient::HttpClient(QObject *parent)
: QObject(parent)
m_networkAccessManager = new QNetworkAccessManager(this);
m_timer = new QTimer(this);
m_timer->setInterval(3000);
m_timer->setSingleShot(true);
connect(m_timer, SIGNAL(timeout()), &m_eventLoop, SLOT(quit()));
HttpClient::~HttpClient()
bool HttpClient::get(QNetworkRequest request)
QNetworkReply *reply;
bool success = false;
request.setPriority(QNetworkRequest::HighPriority);
for(int i = 0 ; i < m_networkErrorRetry + 1; i++)//
reply = m_networkAccessManager->get(request);
processReply(reply);
if (m_error == QNetworkReply::NoError)
success = true;
break;
return success;
bool HttpClient::post(QNetworkRequest request, const QByteArray &data)
QNetworkReply *reply;
bool success = false;
request.setPriority(QNetworkRequest::HighPriority);
for (int i = 0; i < m_networkErrorRetry + 1; i++)
reply = m_networkAccessManager->post(request, data);
processReply(reply);
if (m_error == QNetworkReply::NoError)
success = true;
break;
return success;
void HttpClient::processReply(QNetworkReply *reply)
connect(reply, SIGNAL(finished()), &m_eventLoop, SLOT(quit()));
m_text.clear();
m_timer->start();
m_eventLoop.exec();
if (m_timer->isActive())
m_timer->stop();
m_error = reply->error();
m_errorString = reply->errorString();
if (reply->bytesAvailable() > 0)
m_data = reply->readAll();
QTextCodec *codec = QTextCodec::codecForHtml(m_data, QTextCodec::codecForName("utf-8"));
if (codec)
m_text = codec->toUnicode(m_data);
m_data.clear();
m_text.clear();
reply->abort();
m_error = QNetworkReply::TimeoutError;
delete reply;
QString HttpClient::errorCode() const
switch (m_error)
case QNetworkReply::NoError: return "NoError";
case QNetworkReply::ConnectionRefusedError: return "ConnectionRefusedError";
case QNetworkReply::RemoteHostClosedError: return "RemoteHostClosedError";
case QNetworkReply::HostNotFoundError: return "HostNotFoundError";
case QNetworkReply::TimeoutError: return "TimeoutError";
case QNetworkReply::OperationCanceledError: return "OperationCanceledError";
case QNetworkReply::SslHandshakeFailedError: return "SslHandshakeFailedError";
case QNetworkReply::TemporaryNetworkFailureError: return "TemporaryNetworkFailureError";
case QNetworkReply::ProxyConnectionRefusedError: return "ProxyConnectionRefusedError";
case QNetworkReply::ProxyConnectionClosedError: return "ProxyConnectionClosedError";
case QNetworkReply::ProxyNotFoundError: return "ProxyNotFoundError";
case QNetworkReply::ProxyTimeoutError: return "ProxyTimeoutError";
case QNetworkReply::ProxyAuthenticationRequiredError: return "ProxyAuthenticationRequiredError";
case QNetworkReply::ContentAccessDenied: return "ContentAccessDenied";
case QNetworkReply::ContentOperationNotPermittedError: return "ContentOperationNotPermittedError";
case QNetworkReply::ContentNotFoundError: return "ContentNotFoundError";
case QNetworkReply::AuthenticationRequiredError: return "AuthenticationRequiredError";
case QNetworkReply::ContentReSendError: return "ContentReSendError";
case QNetworkReply::ProtocolUnknownError: return "ProtocolUnknownError";
case QNetworkReply::ProtocolInvalidOperationError: return "ProtocolInvalidOperationError";
case QNetworkReply::UnknownNetworkError: return "UnknownNetworkError";
case QNetworkReply::UnknownProxyError: return "UnknownProxyError";
case QNetworkReply::UnknownContentError: return "UnknownContentError";
case QNetworkReply::ProtocolFailure: return "ProtocolFailure";
return "UnknownError";
QString HttpClient::text() const
return m_text;
3.使用方式
#include <QCoreApplication>
#include "HttpClient.h"
int main(int argc, char *argv[])
QCoreApplication a(argc, argv);
QNetworkRequest request;
request.setUrl(QUrl("http://www.baidu.com"));
request.setRawHeader("Content-Type", "application/json");
if(HttpClient::getInstance()->get(request))
qDebug()<<HttpClient::getInstance()->text();
qDebug()<<HttpClient::getInstance()->errorCode();
return a.exec();
1.Http客户端功能1.支持get,post请求方式.2.支持连接超时处理.3.支持网络错误,尝试重连.2.源码HttpClient.h//HttpClient.h#ifndef HTTPCLIENT_H#define HTTPCLIENT_H#include <QObject>#include <QNetworkAccessManager>#include <QNetworkReply>#include <QEv.
QNetworkReply 类封装了使用 QNetworkAccessManager 发布的请求相关的回复信息。
QNetworkReply 是QIODevice的子类,这意味着一旦从对象中读取数据,它就不再由设备保留。因此,如果需要,应用程序有责任保留这些数据。
注意:不要删除连接到errorOccurred() 或finished() 信号的槽中的对象。应该使用使用 deleteLater()。
二、类型成员
1、enum QNetworkReply::NetworkError:指.
Qt5移除了QFtp API,原因是其实现质量,QNetworkAccessManager可以用来提供ftpurl的上传下载操作; 但是美中不足的是QNetworkAccessManager没有提供list()、cd()、remove()、mkdir()、rmdir()、rename() 和 rawCommand()等操作。
先来看看操作结果:
Qt Network 模块中提供了一些高级别的类,例如:QNetworkRequest、QNetworkReply 和 QNetworkAccessManager,使用常见的协议执行网络操作。
在分享的过程中,顺便介绍下 Fiddler,便于我们调试。Fiddler 是一个 HTTP 协议调试代理工具。当然,也可以使用其它 W...