qt http upload/download (inherit Qnetworkaccessmanager, including use of authenticationrequired authentication signal)

Source: Internet
Author: User
Tags emit

Effect

Qnetworkaccessmanager

Downloadnetworkmanager::D Ownloadnetworkmanager (Qobject *parent): Qnetworkaccessmanager (parent) {//gets the current timestamp, setting the download temp file nameQdatetime DateTime =Qdatetime::currentdatetime (); QString Date= DateTime.ToString ("yyyy-mm-dd-hh-mm-ss-zzz"); M_strfilename= QString ("e:/%1.tmp"). Arg (date); Connect ( This, SIGNAL (Finished (qnetworkreply *)), This, SLOT (replyfinished (qnetworkreply *)));} Downloadnetworkmanager::~Downloadnetworkmanager () {//Terminate Download    if(M_preply! =NULL) {m_preply-abort (); M_preply-Deletelater (); }}//set the URL and message header to start the requestvoidDownloadnetworkmanager::execute () {M_url= Qurl ("Http://192.168.*.*/download/2.0.0.zip");    Qnetworkrequest request;    Request.seturl (M_url); Request.setheader (Qnetworkrequest::contenttypeheader,"Application/zip"); Connect ( This, SIGNAL (authenticationrequired (qnetworkreply *, Qauthenticator *)), This, SLOT (Onauthenticationrequest (qnetworkreply *, Qauthenticator *))); M_preply=Get(Request); Connect (m_preply, SIGNAL (DownloadProgress (Qint64, Qint64)), This, SIGNAL (DownloadProgress (Qint64, Qint64)); Connect (m_preply, SIGNAL (Readyread ()), This, SLOT (Readyread ()));}voidDownloadnetworkmanager::replyfinished (qnetworkreply *reply) {    //Gets the response information, the status code of 200 indicates normalQvariant StatusCode = reply->attribute (Qnetworkrequest::httpstatuscodeattribute); //No error return    if(Reply->error () = =qnetworkreply::noerror) {        //Renaming Temporary Filesqfileinfo FileInfo (m_strfilename); Qfileinfo Newfileinfo= Fileinfo.absolutepath () +M_url.filename ();        Qdir dir; if(Dir.exists (Fileinfo.absolutepath ())) {if(Newfileinfo.exists ()) Newfileinfo.dir (). Remove (Newfileinfo.filename ());        Qfile::rename (M_strfilename, Newfileinfo.absolutefilepath ()); }    }    Else{QString strerror= reply->errorstring (); Qdebug ()<<"Error:"<<strerror; } Emit replyfinished (Statuscode.toint ());}//user authenticationvoidDownloadnetworkmanager::onauthenticationrequest (qnetworkreply *reply, Qauthenticator *authenticator)    {Qbytearray password; Password.append ("123456"); Password=qbytearray::frombase64 (password);    QString strpassword (password); Authenticator->setuser ("Wang"); Authenticator-SetPassword (strpassword);}//Write files locallyvoidDownloadnetworkmanager::readyread () {qfileinfo fileInfo (m_strfilename); Qfileinfo Newfileinfo= Fileinfo.absolutepath () +M_url.filename (); QString strFileName=Newfileinfo.absolutefilepath ();    Emit FileName (strFileName); //Write file-form is appendQFile file (m_strfilename); if(File.Open (qiodevice::append)) File.write (m_preply-ReadAll ()); File.close ();}
Use

Call the download () interface to start downloading, correlate downloadprogress signals and slots to get real-time information such as download size, speed, time remaining.

//Start DownloadvoidMainWindow::d ownload () {if(M_pnetworkmanager = =NULL) {M_pnetworkmanager=NewDownloadnetworkmanager ( This); Connect (M_pnetworkmanager, SIGNAL (DownloadProgress (Qint64, Qint64)), This, SLOT (DownloadProgress (Qint64, Qint64)), qt::queuedconnection); Connect (M_pnetworkmanager, SIGNAL (replyfinished (int)), This, SLOT (replyfinished (int) ), qt::queuedconnection); Connect (M_pnetworkmanager, SIGNAL (QString)), M_pfileinfolabel, SLOT (SetText (QString)), Qt::    Queuedconnection); } M_pnetworkmanager-execute (); Downloadtime.start ();}//Calculate download size, speed, remaining timevoidMainWindow::d ownloadprogress (Qint64 bytesreceived, Qint64 bytestotal) {//Total Time    intNtime =downloadtime.elapsed (); //time taken for this downloadNtime-=M_ntime; //Download Speed    DoubleDbytesspeed = (bytesreceived *1000.0) /Ntime; DoubleDspeed =Dbytesspeed; //Time RemainingQint64 leftbytes = (Bytestotal-bytesreceived); DoubleDlefttime = (Leftbytes *1.0) /Dbytesspeed; M_pspeedinfolabel-SetText (Speed (dspeed)); M_plefttimeinfolabel-SetText (TimeFormat (Qceil (Dlefttime))); M_pfilesizeinfolabel-setText (Size (bytestotal)); M_pdownloadinfolabel-setText (Size (bytesreceived)); M_pprogressbar-Setmaximum (bytestotal); M_pprogressbar-SetValue (bytesreceived); //gets the last timeM_ntime =ntime;}//Download CompletevoidMainwindow::replyfinished (intStatusCode) {M_nstatuscode=StatusCode; QString strstatus= (StatusCode = = $) ? Qstringliteral ("Download Successful"): Qstringliteral ("Download Failed"); M_pstatuslabel-SetText (strstatus);}

http://blog.csdn.net/liang19890820/article/details/50814339

qt http upload/download (inherit Qnetworkaccessmanager, including use of authenticationrequired authentication signal)

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.