Qt5网络请求使用及WebRequest函数

Qt5模拟curl进行HTTP的head请求, curl -I <url> :

 1 #include <QtCore>
 2 #include <QNetworkReply>
 3 #include <QNetworkAccessManager>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QCoreApplication app(argc, argv);
 8 
 9     QUrl url("http://sw.bos.baidu.com/sw-search-sp/software/d4e97ccd4bd9f/jdk-8u144-windows-i586_8.0.1440.1.exe");
10     auto request = QNetworkRequest(url);
11     //request.setRawHeader("Host", "www.baidu.com");
12     request.setRawHeader("User-Agent", "curl/7.55.1");
13     //request.setRawHeader("Accept", "*/*");
14     //request.setRawHeader("Connection", "keep-alive");
15     request.setRawHeader("Accept-Encoding", "*");
16     //qDebug() << request.rawHeaderList();
17 
18     auto manager = new QNetworkAccessManager;
19     manager->head(request);
20 
21     QEventLoop loop;
22     QObject::connect(manager, &QNetworkAccessManager::finished, &loop,
23         [&](QNetworkReply* reply) {
24         qDebug() << reply->rawHeaderList();
25         qDebug() << reply->header(QNetworkRequest::ContentLengthHeader).toInt();
26         for (auto ele : reply->rawHeaderPairs()) qDebug() << ele.first << ":" << ele.second;
27         reply->deleteLater();
28         loop.exit();
29     });
30     loop.exec();
31     return 0;
32 }

输出结果:

("Server", "Date", "Content-Type", "Content-Length", "Connection", "ETag", "Last-Modified", "Expires", "Age", "Accept-Ranges", "Content-MD5", "x-bce-debug-id", "x-bce-request-id", "Ohc-Response-Time")
200214072
"Server" : "JSP3/2.0.14"
"Date" : "Fri, 13 Oct 2017 07:01:18 GMT"
"Content-Type" : "application/octet-stream"
"Content-Length" : "200214072"
"Connection" : "close"
"ETag" : "\"8f189a37b323d81c0f0e013cf3070c93\""
"Last-Modified" : "Thu, 27 Jul 2017 07:09:28 GMT"
"Expires" : "Fri, 13 Oct 2017 07:40:11 GMT"
"Age" : "256440"
"Accept-Ranges" : "bytes"
"Content-MD5" : "jxiaN7Mj2BwPDgE88wcMkw=="
"x-bce-debug-id" : "MTAuMTk0LjIzNC40ODpUaHUsIDI3IEp1bCAyMDE3IDE1OjE5OjU2IENTVDoxMTk2OTEwMzI0"
"x-bce-request-id" : "9e484498-6dbd-4a98-9bf7-39cb5f47ff7c"
"Ohc-Response-Time" : "1 0 0 0 0 0"
Press any key to continue . . .

 封装成 WebRequest 函数:

 1 QString WebRequest(QString url, int& status_code, QString method,
 2         QByteArray postData,QList<QNetworkReply::RawHeaderPair>* reply_pairs, int time_out)
 3 {
 4     QNetworkAccessManager networkManager;
 5     QNetworkRequest request;
 6 
 7     request.setUrl(QUrl(url));
 8     request.setHeader(QNetworkRequest::UserAgentHeader,
 9         UserAgent);
10     request.setRawHeader("Accept","*/*");
11     request.setRawHeader("Accept-Encoding", "*");
12     
13     QNetworkReply* reply;
14     if(method == "get")
15     {
16         reply = networkManager.get(request);
17     }
18     else if(method == "post")
19     {
20         request.setHeader(QNetworkRequest::ContentTypeHeader,
21             "application/x-www-form-urlencoded");
22         reply = networkManager.post(request, postData);
23     }
24     else
25     {
26         qDebug() << "method not support.";
27         return QString("");
28     }
29 
30     QTimer timer;
31     timer.setSingleShot(true);
32 
33     QEventLoop loop;
34     QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
35     QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
36     if (time_out <= 0)
37     {
38         time_out = 3000;
39     }
40     timer.start(time_out);   // 3 secs. timeout
41     loop.exec();
42 
43     if (timer.isActive()) {
44         timer.stop();
45         if (reply->error() > 0) {
46             qDebug() << "error";// handle error
47         }
48         else {
49             status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
50             if (status_code >= 200 && status_code < 300)
51             {
52                 // Success
53                 if (reply_pairs)
54                 {
55                     *reply_pairs = reply->rawHeaderPairs();
56                 }
57                 return QString(reply->readAll());
58             }
59         }
60     }
61     else {
62         // timeout
63         QObject::disconnect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
64         reply->abort();
65         return QString("");
66     }
67 }

 

posted @ 2017-10-13 15:02  星云的彼岸  阅读(3613)  评论(0编辑  收藏  举报