//*******http.cpp*******
#include "http.h"
//#include <iostream>
#include<dirent.h>
#include<QDir>
//using namespace std;
{
}
http::~http()
{
}
{
connect(&http, SIGNAL(done(bool)), this, SLOT(HttpDone(bool)));
}
bool HttpGet
::getFile(const QUrl &url
) {
QDir::setCurrent(str
+"/Downloads");
if (!url.isValid()) {
cerr << "Error: Invalid URL" << endl;
return false;
}
else
{
cout << "URL valid..!!" << endl;
}
if (url.scheme() != "http") {
cerr << "Error: URL must start with ’http:’" << endl;
return false;
}
if (url.path().isEmpty()) {
cerr << "Error: URL has no path" << endl;
return false;
}
if (localFileName.isEmpty())
localFileName = "httpget.out";
file.setFileName(localFileName);
cerr << "Error: Cannot open " << qPrintable(file.fileName())
<< " for writing: " << qPrintable(file.errorString())
<< endl;
return false;
}
// QNetworkAccessManager *http = new QNetworkAccessManager(this);
proxy.setHostName("10.0.0.1"); //what address should i provide?
// ip of another comp??
proxy.setPort(8888); //what port??
proxy.setUser("supriya");
proxy.setPassword("supriya");
http.setProxy(proxy);
// m_reply = http->get(request);
http.setHost(url.host(), url.port());
//cout<< "connected to host" << url.host() << endl << endl;
http.get(url.path(), &file);
http.close();
return true;
}
void HttpGet::HttpDone(bool error)
{
if (error) {
cout << "Error: " << qPrintable(http.errorString()) << endl;
} else {
cerr << "File downloaded as " << qPrintable(file.fileName())
<< endl;
}
file.close();
emit done();
}
//*******main.cpp*******
#include <QtGui/QApplication>
#include "http.h"
int main(int argc, char *argv[])
{
argc=2;
if (argc!= 2) {
cerr << "Usage: httpget url" << endl
<< "Example:" << endl
<< "httpget http://doc.trolltech.com/qq/index.html"
<< endl;
return 1;
}
QNetworkProxyFactory::setUseSystemConfiguration(true);
HttpGet getter;
if (!getter.
getFile(QUrl("http://10.1.11.137:80/"))) //what url? return 1;
QObject::connect(&getter,
SIGNAL(done
()),
&app,
SLOT(quit
()));
return app.exec();
}
//*******http.cpp*******
#include "http.h"
//#include <iostream>
#include<dirent.h>
#include<QDir>
//using namespace std;
http::http(QWidget *parent)
: QMainWindow(parent)
{
}
http::~http()
{
}
HttpGet::HttpGet(QObject *parent)
: QObject(parent)
{
connect(&http, SIGNAL(done(bool)), this, SLOT(HttpDone(bool)));
}
bool HttpGet::getFile(const QUrl &url)
{
QString str = QDir::currentPath();
QDir::setCurrent(str+"/Downloads");
if (!url.isValid()) {
cerr << "Error: Invalid URL" << endl;
return false;
}
else
{
cout << "URL valid..!!" << endl;
}
if (url.scheme() != "http") {
cerr << "Error: URL must start with ’http:’" << endl;
return false;
}
if (url.path().isEmpty()) {
cerr << "Error: URL has no path" << endl;
return false;
}
QString localFileName = QFileInfo(url.path()).fileName();
if (localFileName.isEmpty())
localFileName = "httpget.out";
file.setFileName(localFileName);
if (!file.open(QIODevice::WriteOnly)) {
cerr << "Error: Cannot open " << qPrintable(file.fileName())
<< " for writing: " << qPrintable(file.errorString())
<< endl;
return false;
}
// QNetworkAccessManager *http = new QNetworkAccessManager(this);
QNetworkProxy proxy;
proxy.setType(QNetworkProxy::Socks5Proxy);
proxy.setHostName("10.0.0.1"); //what address should i provide?
// ip of another comp??
proxy.setPort(8888); //what port??
proxy.setUser("supriya");
proxy.setPassword("supriya");
QNetworkProxy::setApplicationProxy(proxy);
http.setProxy(proxy);
// m_reply = http->get(request);
http.setHost(url.host(), url.port());
//cout<< "connected to host" << url.host() << endl << endl;
http.get(url.path(), &file);
http.close();
return true;
}
void HttpGet::HttpDone(bool error)
{
if (error) {
cout << "Error: " << qPrintable(http.errorString()) << endl;
} else {
cerr << "File downloaded as " << qPrintable(file.fileName())
<< endl;
}
file.close();
emit done();
}
//*******main.cpp*******
#include <QtGui/QApplication>
#include "http.h"
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QStringList args = app.arguments();
argc=2;
if (argc!= 2) {
cerr << "Usage: httpget url" << endl
<< "Example:" << endl
<< "httpget http://doc.trolltech.com/qq/index.html"
<< endl;
return 1;
}
QNetworkProxyFactory::setUseSystemConfiguration(true);
HttpGet getter;
if (!getter.getFile(QUrl("http://10.1.11.137:80/"))) //what url?
return 1;
QObject::connect(&getter, SIGNAL(done()), &app, SLOT(quit()));
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks