Skip to content

Instantly share code, notes, and snippets.

@lamprosg
Created January 25, 2013 13:41
Show Gist options
  • Select an option

  • Save lamprosg/4634546 to your computer and use it in GitHub Desktop.

Select an option

Save lamprosg/4634546 to your computer and use it in GitHub Desktop.
Qt - Network (http request)
QT += network
#include "tester.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Tester test;
test.GetUrl(); //Just start it
return a.exec();
}
#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();
}
}
//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);
};
@yscontrol
Copy link
Copy Markdown

tester line 14 "explicit Tester(QObjet parent = 0);" typo , replace with QObject and add #include

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment