Here the background of the story is like this. Here we mainly implement a function such as ip138 website. Here we first take a look at ip138 to implement the function described in the sample 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2041123222-0.jpg "title =" qq 308281820.3.jpg "/>
Here your IP address and the following information are the information we are interested in. You can add such information in your software to obtain your IP address, in addition, you can obtain information from other users, but this may be risky like rainbow QQ, I have also referred to some resources on the Internet, most of which are incorrect. A typical example is this example. This makes the downloaded file do not contain the information we need, but the information is clearly there, so far away!
The actual solution to the problem is very simple. It is to send a request to the webpage, collect the items returned from the webpage, and analyze the collected content. The code is directly provided here, then explain
QString getIpLocation () {// webpage address const QString URLSTR = QString: fromStdWString (IP138); QUrl url (URLSTR); networkqaccessmanager manager; QEventLoop loop; // send the request QNetworkReply * reply = manager. get (QNetworkRequest (url); // after the request ends and the download is complete, exit the subevent loop QObject: connect (reply, SIGNAL (finished (), & loop, SLOT (quit (); // enable the sub-event loop loop.exe c (); // extract information from the obtained webpage text code. QString code = reply-> readAll (); QString lcenter = "<center>"; QString rcenter = "</center>"; int lIndex = code. indexOf (lcenter); QString left = code. left (lIndex + lcenter. length (); code. replace (left, ""); int rIndex = code. indexOf (rcenter); QString result = code. left (rIndex); return result ;}
This function is for the page IP138, which I wrote in the macro.
#define IP138 L"http://iframe.ip138.com/ic.asp"
Open this page and you will see something like this.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2041125a4-1.jpg "title =" qq 30828183639.jpg "/>
Here is what the webpage gives us. We use the get method of the QNetworkAccessManager class. It is necessary to briefly describe this method here.
QNetworkReply * QNetworkAccessManager::get ( const QNetworkRequest & request )
Send a readyRead () signal to the target when receiving the request and return a QNetworkReply object. Simply put, the request sends a signal to the target, and retrieve the information returned by the web page, including the information header of the web page.
Here lines 7-13 carry out a message loop. At the end of this loop, reply sends the finish () signal, of course, this finish () signal () the signal is sent along with the webpage information sent by the server over there. It is not triggered by programmers. The channel quit () corresponding to this signal is also automatically executed by the system and the message loop is exited, here I think we can actively send signals. No matter what signal, it can be not the finish () signal, but it is connected to the quit () channel, here we can implement our own forced end of the message loop.
The QNetworkReply class contains some sent data and headers. For example, QNetworkRequest contains the URL, header, and response status and content, the 15 lines indicate that all the read data is read in the code. The following is the parsing of the code content. Here, because the information we need is between <center> and </center>, therefore, the subsequent processing is specific to the information we need, which is not described here.
This should be well understood and I want to talk about the subsequent operations. When using these classes, we should add the header file # include <QtNetwork>, but you will find that, after it is added, it cannot be compiled. here we need to manually change our pro file, add such QT + = network, and package the network library in, in this way, you can pass the compilation.
It is necessary to pay attention to this QNetworkRequest. Here we send a simple request to a Web page. We can send other requests to this web page to ask it to give us some feedback, for example, some lottery pages can be used as plug-ins and one-click lucky draws.
This article from the "selling cute programmers" blog, please be sure to keep this source http://7677869.blog.51cto.com/7667869/1284519