CQtQt5.5.1ToolsQtCreatorbinqtcreator_process_stub.exe

#include<QCoreApplication>
#include<QDebug>
#include<QtNetwork/QNetworkAccessManager>
#include<QtNetwork/QNetworkRequest>
#include<QtNetwork/QNetworkReply>
#include<QUrl>
#include<QUrlQuery>
 
void sendRequest() {
 
    QEventLoop eventLoop;
    QNetworkAccessManager mgr;
 
    QObject::connect(&mgr,SIGNAL(finished(QNetworkReply*)),&eventLoop,SLOT(quit()));
 
    QNetworkRequest req(QUrl(QString("http://ip.jsontest.com/")));
    QNetworkReply * reply=mgr.get(req);
 
    eventLoop.exec();
 
    if(reply->error()==QNetworkReply::NoError){
        //連接成功
        qDebug()<<"Success"<<reply->readAll();
    } else {
        //連接失敗
        qDebug()<<"Failure"<<reply->errorString();
    }
    delete reply;
}
 
int main(int argc, char * argv[]) {
     QCoreApplication a(argc, argv);
     sendRequest();
     return a.exec();
}
 
arrow
arrow
    全站熱搜

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