sups
25th March 2011, 09:35
Hello,
I have some problem in using qt setProxy() function.
I have to download file from a http server which I have installed on another system .
I have installed apache2 server and squid proxy server installed on that system.
what address should i provide for setproxy() function and what port.
How can I know that my code is working or not.
Here is my code.
//*******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(tr ue);
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();
}
when I provide setproxy("10.1.11.137",8888);url = "10.1.11.137:80"
it gives the output :Http Request failed. I am not getting problem yaar plz plz help me.
I have some problem in using qt setProxy() function.
I have to download file from a http server which I have installed on another system .
I have installed apache2 server and squid proxy server installed on that system.
what address should i provide for setproxy() function and what port.
How can I know that my code is working or not.
Here is my code.
//*******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(tr ue);
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();
}
when I provide setproxy("10.1.11.137",8888);url = "10.1.11.137:80"
it gives the output :Http Request failed. I am not getting problem yaar plz plz help me.