#include<QCoreApplication>
#include<QDebug>
#include<QtNetwork/QNetworkAccessManager>
#include<QtNetwork/QNetworkRequest>
#include<QtNetwork/QNetworkReply>
#include<QUrl>
#include<QUrlQuery>
#include<QFile>
 
 
void sendRequest(){
 
    QEventLoop eventLoop;
    QNetworkAccessManager mgr;
 
    QObject::connect(&mgr,SIGNAL(finished(QNetworkReply*)),&eventLoop,SLOT(quit()));
 
    QNetworkRequest req(QUrl(QString("http://i39.tinypic.com/1yn3wx.jpg")));
    QNetworkReply * reply = mgr.get(req);
 
    eventLoop.exec();
 
    if(reply->error()==QNetworkReply::NoError){
        //連接成功
 
        qDebug()<<"Download...";
 
        QFile * file = new QFile("C:/Test/downloaded.jpg");
        if(file->open(QFile::Append)) {
            file->write(reply->readAll());
            file->flush();
            file->close();
        }
        delete file;
 
        qDebug() << "ok";
 
    }else{
        //連接失敗
        qDebug() << "Failure" <<reply -> errorString();
    }
    delete reply;
}
 
intmain(int argc, char * argv[]) {
    QCoreApplication a(argc,argv);
    sendRequest();
    return a.exec();
}
 
創作者介紹
創作者 黃彥霖 的頭像
黃彥霖

彥霖 實驗筆記

黃彥霖 發表在 痞客邦 留言(0) 人氣()