Created
January 25, 2013 13:41
-
-
Save lamprosg/4634546 to your computer and use it in GitHub Desktop.
Qt - Network (http request)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| QT += network |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "tester.h" | |
| int main(int argc, char *argv[]) | |
| { | |
| QCoreApplication a(argc, argv); | |
| Tester test; | |
| test.GetUrl(); //Just start it | |
| return a.exec(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "tester.h" | |
| Tester::Tester(QObject *parent) : | |
| QObject(parent) | |
| { | |
| } | |
| void Tester::GetUrl() | |
| { | |
| /*****If we connect to a proxy*****************/ | |
| QNetworkProxy proxy; | |
| proxy.setType(QNetworkProxy::DefaultProxy); | |
| proxy.setHostName("127.0.0.1"); //Where the proxy is setup. (In this case localhost) | |
| proxy.setPort(1080); | |
| //proxy.setUser("username"); | |
| //proxy.setPassword("password"); | |
| QNetworkProxy::setApplicationProxy(proxy); //Every packet will go out through the proxy | |
| /**********************************************/ | |
| //Connecting | |
| QNetworkAccessManager *manager = new QNetworkAccessManager(this); | |
| connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply *Reply))); | |
| //Perform an HTTP GET | |
| manager->get(QNetworkRequest(QUrl("http://www.blabla.com/file.png"))); //When done, replyFinished(..) will be triggered | |
| } | |
| void Tester::replyFinished(QNetworkReply *Reply) | |
| { | |
| //Reading the reply from the http request | |
| if (Reply->isOpen()) | |
| { | |
| Reply->read(5000); //read the first 5000 bytes | |
| Reply->close(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Tester class, inherits QObject | |
| #include <QNetworkAccessManager> //Our includes | |
| #include <QNetworkReply> | |
| #include <QNetworkRequest> | |
| #include <QNetworkProxy> //If you need to go through a proxy server/service (like Tor) | |
| class Tester : public QObject | |
| { | |
| Q_OBJECT | |
| public: | |
| explicit Tester(QObjet parent = 0); | |
| signals: | |
| public slots: | |
| void GetUrl(); //get the file | |
| void replyFinished(QNetworkReply *Reply); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tester line 14 "explicit Tester(QObjet parent = 0);" typo , replace with QObject and add #include