Add timeout reminders for Qnetworkaccessmanager (log the number of bytes downloaded for a period of time, and periodically detect with a timer to determine if the timeout is exceeded)

Source: Internet
Author: User

In the update, in the test exception, during the download process, found that if the direct system disabled the network, will report errors, can be captured. But if third-party software restricts the networking of programs, the problem comes.

The program will always be there waiting, no exceptions, and no Qnetworkaccessmanager::finsh and Qnetworkreply::error and Qnetworkreply::finsh signals are sent.

So the idea is to add a timeout to the download, but qnetworkaccessmanager,qnetworkreply and qnetworkrequest do not have the relevant setting function. In fact, think about it, there is no way to directly provide timeouts, because the download of the file has a lot of small, the network is fast and slow, what is the time-out? only to find a way to add. Because the exception must be found and handled, the problem must be solved.

First say my solution, using qnetworkreply::d ownloadprogress signal to achieve, their own record for a period of time the number of downloaded bytes, with a timer to regular detection, if a period of time, a little extra download is not, then I think the network timeout is abnormal.

Below the code, you can simply use the code to describe the following:

Statement:

01 classNetWorkDownload : publicQObject
02 {
03     Q_OBJECT
04 public:
05     explicitNetWorkDownload(QObject *parent = 0);
06 signals:
07     voidtimeOut();
08     //····你的其他需要传递出去的信号
09 publicslots:
10     voidstartDownload(constQString & url);
11     
12 protectedslots:
13     voidhandleReply(QNetworkReply * rep);//处理下载完成的数据
14     voidhandSize(qint64 size, qint64 all);
15     voidhandleTimeOut();
16 private:
17     QNetworkAccessManager manger;
18     intfileDownSize,//已经下载的字节数
19         lastDownSize;//定时器上次下载的字节数
20     QTimer tm;//定时器
21 };

Realize:

01 NetWorkDownload::NetWorkDownload(QObject *parent) :
02     QObject(parent)
03 {
04     //这个connect你也可以用QNetworkReply来链接,QNetworkReply::finsh,QNetworkReply::readyReady这些信号。
05     connect(&manger,&QNetworkAccessManager::finished,this,&NetWorkDownload::handleReply);
06     connect(&tm,&QTimer::timeout,this,&NetWorkDownload::timeOut);
07 }
08
09 voidNetWorkDownload::startDownload(constQString & url)
10 {
11     QNetworkRequest req;
12     req.setUrl(QUrl(url));
13     auto rep = manger.get(req);
14     connect(rep,&QNetworkReply::downloadProgress,this,&NetWorkDownload::handSize);
15     //启动超时定时,30s。
16     if(tm.isActive())
17         tm.stop();
18     tm.start(30000);
19 }
20
21 voidNetWorkDownload::handSize(qint64 size,qint64 /* all*/)
22 {
23     fileDownSize = size;
24 }
25
26 voidNetWorkDownload::handleReply(QNetworkReply *rep)
27 {
28     /*
29     下载完成的您的处理代码。
30     */
31     fileDownSize = 0;
32     lastDownSize = 0;
33     if(tm.isActive())//如果还在定时,就停止
34         tm.stop();
35 }
36
37 voidNetWorkDownload::timeOut()
38 {
39     if(lastDownSize != fileDownSize)//如果过了30s,一点字节也没有下载过来,认为超时了。
40         lastDownSize = fileDownSize;
41     else
42         emit timeOut();
43 }

Http://www.dushibaiyu.com/2014/10/qnetworkaccessmanager-timeout.html

Http://stackoverflow.com/questions/13207493/qnetworkreply-and-qnetworkaccessmanager-timeout-in-http-request

Add timeout reminders for Qnetworkaccessmanager (log the number of bytes downloaded for a period of time, and periodically detect with a timer to determine if the timeout is exceeded)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.