QT5 HTTP protocol for Web page data

Source: Internet
Author: User

First, the HTTP protocol

The QT Network module provides APIs that enable applications based on the HTTP (Hyper Text Transport Protocol) protocol. In addition, QT can implement a variety of

Protocol applications, such as FTP, DNS, and SNMP, located at the OSI 7 layer application layer.

The qt4.x version provides the qhttp and QFTP classes to support HTTP and FTP protocols. Starting with the QT 5.0 release, the Qhttp and qftp classes were removed, with the qnetworkreply

, Qnetworkaccessmanager, and Qnetworkrequest classes implement HTTP and FTP protocol-based applications. In addition to the HTTP and FTP protocols, this class can be

High level (upper layer of OSI 7) network application.

The ①qnetworkrequest class, which is used through network requests. Also, you can request header information that contains header information and encryption (encryption). Qnetworkrequest can

To upload and download HTTP, ftp local files via URLs.

The ②qnetworkaccessmanager class is used to manage network requests and responses, to manage requests and response settings on the network, to use proxies and caches,

Handles signals that are triggered during network requests and responses.

The ③qnetworkreply class is used to process responses on the network, which is a subclass of Qiodevice that supports synchronous asynchronous methods.

Second, code example

Here is a code to get the weather of Baidu:

① Environment for vs2013 + qt5.7

② creating QT5 Gui qapplication Project

③ add Textbrowser to the UI to show the data obtained

    

④ Source Display

  

/*header File*/#ifndef Networkwidget_h#defineNetworkwidget_h#include<QtNetwork/QNetworkAccessManager>#include<QtNetwork/QNetworkRequest>#include<QtNetwork/QNetworkReply>#include<QtWidgets/QWidget>#include<QTextCodec>#include<QString>#include<QUrl>#include"ui_networkwidget.h"classNetworkwidget: Publicqwidget{Q_object Public: Networkwidget (Qwidget*parent =0); ~networkwidget ();Privateq_slots:voidSlot_replayfinished (qnetworkreply *reply);Private: Ui::networkwidgetclass Ui; Qnetworkaccessmanager*M_manager; //qnetworkreply *m_replay;};#endif //Networkwidget_h
/*implementation file*/#include"networkWidget.h"Networkwidget::networkwidget (Qwidget*parent): Qwidget (parent) {UI.SETUPUI ( This);  This-setwindowflags (Qt::framelesswindowhint); M_manager=NewQnetworkaccessmanager ( This);//New Qnetworkaccessmanager ObjectConnect (M_manager, SIGNAL (Finished (qnetworkreply *)), This, SLOT (slot_replayfinished (qnetworkreply*)));//correlated signals and slotsM_manager->Get(Qnetworkrequest (Qurl ("http://apistore.baidu.com/microservice/weather?cityid= Chengdu")));//Send Request}networkwidget::~Networkwidget () {}voidNetworkwidget::slot_replayfinished (qnetworkreply *reply) {Qtextcodec*codec = Qtextcodec::codecforname ("UTF8"); //use UTF8 encoding to display the ChineseQString str = Codec->tounicode (reply->ReadAll ()); Ui.textbrowser-SetText (str); Reply->deletelater ();//Finally, you want to release the replay object}
" networkWidget.h "  <QtWidgets/QApplication>int main (intChar *argv[]) {    Qapplication A (argc, argv);    Networkwidget W;    W.show ();     return a.exec ();}

Third, the running program crashes

Compile can pass, run the program crashes, crash in the sending request line tenth, originally thought to be a memory leak to cause heap damage, view Official document usage discover our code

There is no mistake at all, and finally, using QT Creator to create engineering tests to find out that our code is completely free of problems and to get the data successful. Finally determine that there must be no Qt5 dependencies in the VS configuration

Library. The following is an error:

    

Workaround: Right-click the project----configuration Properties------------additional dependencies, and find that dependencies can be contained in the Lib are all with D, and I now use the debug mode, so I

Join Qt5Core.lib, Qt5Gui.lib, last run not crash, get data successfully.

All Lib libraries in Qt are located under the QT installation path Lib file, such as: C:\Qt\Qt5.7.0\5.7\msvc2013_64\lib

Note: Libraries with D are for debug mode, without D for release mode

    

Iv. Display of results

    

  

  

  

QT5 HTTP protocol for Web page data

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.