首页 > 代码库 > Qt设置阻塞式网络请求,和超时处理

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设置阻塞式网络请求,和超时处理