PDA

View Full Version : QNetworkAccessManager not reaching to google.com



mvbhavsar
28th May 2013, 09:52
Hi,

I am using QNetworkAccessManager to reach to http://google.com. The idea is that there will web services over the internet and I need to call methods from those services. So start with I am trying to reach google.com. I my organization, we are on proxy and this proxy is configured in one script and that script is executed while connecting to internet. Now when I use QNetworkAccessManager's GET method I am not getting any output and connection is getting timed out. I tried put QNetworkProxy also but it is not working.
The question is how can I get output of google.com using QNetworkAccessManager when proxy settings is in the form of script.

(Pl. find attached screenshot of attached script.)


Regards

Manish

ChrisW67
28th May 2013, 22:02
All that should be required is a call to QNetworkProxyFactory::setUseSystemConfiguration() after which Qt asks Windows (http://qt.gitorious.org/qt/qtbase/blobs/stable/src/network/kernel/qnetworkproxy_win.cpp#line59) what proxy to use. If a proxy is required to exit your network and Qt does not have that then an attempt to connect outside should fail immediately not be "getting timed out" (assuming you have a sane network). Since there's nothing useful in your screen shot and we cannot see your code I think that is the limit of diagnosis.

Why don't you post a small, compilable example program that fails in the manner described.

mvbhavsar
29th May 2013, 07:16
Thanks for reply.
Putting code below.



#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->textEdit->append("Updating response now.");
QUrl a("http://google.co.in");
manager = new QNetworkAccessManager(this);
// QNetworkProxyQuery npq(a);
// QList<QNetworkProxy> listOfProxies = QNetworkProxyFactory::systemProxyForQuery(npq);
// QNetworkProxy pr = listOfProxies.at(0);
// qDebug() << "Count: " << pr.hostName();

// if (listOfProxies.count() !=0){
// if (listOfProxies.at(0).type() != QNetworkProxy::NoProxy) {
// manager->setProxy(listOfProxies.at(0));
// qDebug() << "listOfProxies.at(0).hostName().toStdString()";
//// qDebug() << "Using Proxy " << listOfProxies.at(0).hostName().toStdString();
// }
// }
connect(manager,SIGNAL(finished(QNetworkReply*)),t his,SLOT(doneSlot(QNetworkReply*)));
// QNetworkProxy proxy(QNetworkProxy::HttpCachingProxy,"http://Proxy.TechM/wpad.dat",80);
// manager->setProxy(proxy);
QNetworkReply *reply = manager->get(QNetworkRequest(a));
reply->waitForReadyRead(-1);
qDebug() << reply->readAll().data();
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::doneSlot(QNetworkReply *reply)
{
ui->textEdit->append("Reading reply now");
QString response = reply->readAll().data();
ui->textEdit->append(response);
}



Basically the problem is that this code is getting run in an environment where internet connection is through proxy connection and that proxy connection is done through some script as attached. There is no other proxy configuration is mentioned. I am able get http://google.com through my browser.
Also above code is reaching to internal network if URL is given with internal IP and path to file.

ChrisW67
29th May 2013, 09:56
You don't tell QNetworkAccessManager to use the system proxy settings and it will not happen magically. However, that is not the major problem with your code. Qt network is processed asynchronously and you are blocking the program waiting for an event that cannot occur when you call waitForReadyRead(-1).

This example should work just fine.


#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkProxyFactory>
#include <QDebug>

class Fetcher: public QObject
{
Q_OBJECT
public:
Fetcher(QObject *p = 0): QObject(p), reply(0) {}

void fetch(const QUrl &url) {
reply = nam.get(QNetworkRequest(url));
connect(reply, SIGNAL(finished()), SLOT(slotFinished()));
connect(reply, SIGNAL(readyRead()), SLOT(slotReadyRead()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(slotError(QNetworkReply::NetworkError)));
}

public slots:
void slotError(QNetworkReply::NetworkError code) {
qDebug() << Q_FUNC_INFO << "Error" << code;
}

void slotReadyRead() {
qDebug() << Q_FUNC_INFO << "Bytes avail" << reply->bytesAvailable();;
}

void slotFinished() {
QByteArray ba = reply->readAll();
qDebug() << ba;
reply->deleteLater();
qApp->exit();
}

private:
QNetworkAccessManager nam;
QNetworkReply *reply;
};

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QNetworkProxyFactory::setUseSystemConfiguration(tr ue);
Fetcher f;
f.fetch(QUrl("http://www.google.com"));
return app.exec();
}
#include "main.moc"

mvbhavsar
31st May 2013, 17:06
Thanks.
But still it is not working and core issue is not addressed. How to pass script as proxy setting to network so that google.com can be reached.

ChrisW67
31st May 2013, 23:50
How to pass script as proxy setting to network
Line 45 in my example. Did you bother to either read it or try it?

so that google.com can be reached.
Your code will not talk to Google unless you fix the incorrect design... and this has nothing to do with proxies.

Qt network is processed asynchronously and you are blocking the program waiting for an event that cannot occur when you call waitForReadyRead(-1).

mvbhavsar
5th June 2013, 08:15
Thanks ChrisW67.

Yes, I have made changes suggested by you but still issue is not resolved and I am getting error as


void MainWindow::slotError(QNetworkReply::NetworkError) Error 99


Listing my modified code as below.



#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->textEdit->append("Updating response now.");
QNetworkProxyFactory::setUseSystemConfiguration(tr ue);
QUrl a("http://google.co.in",QUrl::StrictMode);
reply = manager.get(QNetworkRequest(a));
connect(reply, SIGNAL(finished()), SLOT(slotFinished()));
connect(reply, SIGNAL(readyRead()), SLOT(slotReadyRead()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(slotError(QNetworkReply::NetworkError)));
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::doneSlot(QNetworkReply *reply)
{
ui->textEdit->append("Reading reply now");
QString response = reply->readAll().data();
ui->textEdit->append(response);
}

void MainWindow::slotFinished()
{
QByteArray ba = reply->readAll();
qDebug() << ba;
ui->textEdit->append(ba);
reply->deleteLater();
}

void MainWindow::slotReadyRead()
{
qDebug() << Q_FUNC_INFO << "Bytes avail" << reply->bytesAvailable();
}

void MainWindow::slotError(QNetworkReply::NetworkError code)
{
qDebug() << Q_FUNC_INFO << "Error" << code;
}

wysota
5th June 2013, 08:19
Your proxy factory gets destroyed when the constructor of MainWindow ends.

ChrisW67
6th June 2013, 01:24
The static function call allocates an internal, application-wide proxy factory that should not go out of scope.

Error 99:


QNetworkReply::UnknownNetworkError 99 an unknown network-related error was detected

This is not an error that is returned if a host name cannot be resolved, a connection is explicitly refused by proxy or host, times out or returns access denied etc. This is more like the application is totally prohibited from talking to a network. A "helpful" Windows anti-virus/security/firewall program perhaps.

Only the OP has the visibility to diagnose what is misconfigured with their network.