Qt设置阻塞式网络请求,和超时处理
Posted 阿兴
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt设置阻塞式网络请求,和超时处理相关的知识,希望对你有一定的参考价值。
QNetworkProxy proxy;
proxy.setType(QNetworkProxy::Socks5Proxy);
proxy.setHostName(LOCAPROXYIP);
proxy.setPort(LOCAPROXYPORT);
proxy.setUser(PROXYUSERNAME);
proxy.setPassword(PROXYPWD);
QTimer timer;
timer.setInterval(CHECKPROXYNETWORKTIMES);
timer.setSingleShot(true);
QNetworkAccessManager manager;
manager.setProxy(proxy);
QNetworkRequest request;
request.setUrl(QUrl(CHECKREMOTEURL));
QNetworkReply *pReply = manager.get(request);
QEventLoop loop;
QObject::connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
QObject::connect(pReply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
timer.start();
loop.exec();
if (timer.isActive()) {
timer.stop();
if (pReply->error() != QNetworkReply::NoError) { //访问异常
//CLIENT_APP->commMessageBoxInt(CLIENT_APP->_listPollIndex);
CLIENT_APP->startShadowProxyApp();
Sleep(CHECKTHREADTIMES);
}
else
{
//CLIENT_APP->commMessageBoxInt(CLIENT_APP->_listPollIndex);
Sleep(CHECKPROXYNETWORKSUCCTIMES);//成功了 设置延迟
}
}
else {//超时
//CLIENT_APP->commMessageBoxInt(CLIENT_APP->_listPollIndex);
QObject::disconnect(pReply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
pReply->abort();
pReply->deleteLater();
CLIENT_APP->startShadowProxyApp();
Sleep(CHECKTHREADTIMES);
}
以上是关于Qt设置阻塞式网络请求,和超时处理的主要内容,如果未能解决你的问题,请参考以下文章