qt的http之post和get请求超时处理方案

说的再多,不如贴源码。

get请求: 

QByteArray HttpUtil::getData(QNetworkRequest request /*= QNetworkRequest()*/, const QByteArray & data /*= QByteArray()*/)
{
	QTimer timer;
	timer.setInterval(5000);  // 设置超时时间 5 秒
	timer.setSingleShot(true);  // 单次触发
	m_networkReply = m_networkAccessManager->get(request);
	//connect(m_networkReply, &QIODevice::readyRead, this, &HttpUtil::onReadyRead);
	QEventLoop eventLoop;
	connect(&timer, &QTimer::timeout, &eventLoop, &QEventLoop::quit);
	connect(m_networkReply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
	timer.start();
	eventLoop.exec();
	if (timer.isActive())
	{
		timer.stop();
		QVariant httpStatusCode =
			m_networkReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
		QVariant redirectionTargetUrl =
			m_networkReply->attribute(QNetworkRequest::RedirectionTargetAttribute);
		int totlalLength = m_networkReply->rawHeader("Content-Length").toInt();

		QByteArray  contentDisposition = m_networkReply->rawHeader("Content-Disposition");

		QString stringContentDisposition = QString::fromUtf8(contentDisposition);

		qDebug() << "stringFileName###:" << stringContentDisposition;
		int reciveLength = 0;

		if (m_networkReply->error() == QNetworkReply::NoError)
		{
			while (true)
			{
				QByteArray byteArray = m_networkReply->readAll();
				QString rlt = QString::fromUtf8(byteArray);
				qDebug() << "rlt###:" << rlt;
				reciveLength += byteArray.count();
				if (m_networkReply->isFinished())
				{
					qDebug() << QString::fromUtf8("下载数据完成的。");
					return byteArray;
				}
				if (totlalLength == reciveLength)
				{
					qDebug() << "length == reciveLength:";
					qDebug() << QString::fromUtf8("下载数据完成的。");
					break;
				}
				return byteArray;
			}
		}
	}
	else
	{
		disconnect(m_networkReply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
		m_networkReply->abort();
		m_networkReply->deleteLater();
		qDebug() << QStringLiteral("请求超时!");
	}
	return QByteArray();
}

post请求:

QByteArray HttpUtil::postData(QNetworkRequest request /*= QNetworkRequest()*/, const QByteArray & data /*= QByteArray()*/)
{
	QTimer timer;
	timer.setInterval(5000);  // 设置超时时间 5 秒
	timer.setSingleShot(true);  // 单次触发
	m_networkReply = m_networkAccessManager->post(request, data);
	//connect(m_networkReply, &QIODevice::readyRead, this, &HttpUtil::onReadyRead);
	QEventLoop eventLoop;
	connect(&timer, &QTimer::timeout, &eventLoop, &QEventLoop::quit);
	connect(m_networkReply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
	timer.start();
	eventLoop.exec();
	if (timer.isActive())
	{
		timer.stop();
		typedef QPair<QByteArray, QByteArray> RawHeaderPair;
		QList<RawHeaderPair>rawHeaderPairs = m_networkReply->rawHeaderPairs();

		QVariant httpStatusCode =
			m_networkReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
		QVariant redirectionTargetUrl =
			m_networkReply->attribute(QNetworkRequest::RedirectionTargetAttribute);
		if (m_networkReply->error() == QNetworkReply::NoError)
		{
			QByteArray byteArray = m_networkReply->readAll();
			QString rlt = QString::fromUtf8(byteArray);
			qDebug() << "rlt###:" << rlt;
			if (m_networkReply->isFinished())
			{
				qDebug() << QString::fromUtf8("下载数据完成的。");
				m_networkReply->deleteLater();
				return byteArray;
			}
		}
	}
	else
	{
		disconnect(m_networkReply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
		m_networkReply->abort();
		m_networkReply->deleteLater();
		qDebug() << QStringLiteral("请求超时!");
	}
	
	return QByteArray();
}

完毕,谢谢!

猜你喜欢

转载自blog.csdn.net/weixin_42101997/article/details/84840905