Results 1 to 9 of 9

Thread: QNetworkAccessManager not reaching to google.com

  1. #1
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QNetworkAccessManager not reaching to google.com

    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
    Attached Images Attached Images

  2. The following user says thank you to mvbhavsar for this useful post:


  3. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QNetworkAccessManager not reaching to google.com

    All that should be required is a call to QNetworkProxyFactory::setUseSystemConfiguration() after which Qt asks Windows 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.

  4. The following user says thank you to ChrisW67 for this useful post:


  5. #3
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager not reaching to google.com

    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.

  6. The following user says thank you to mvbhavsar for this useful post:


  7. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QNetworkAccessManager not reaching to google.com

    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.
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QNetworkAccessManager>
    3. #include <QNetworkRequest>
    4. #include <QNetworkReply>
    5. #include <QNetworkProxyFactory>
    6. #include <QDebug>
    7.  
    8. class Fetcher: public QObject
    9. {
    10. Q_OBJECT
    11. public:
    12. Fetcher(QObject *p = 0): QObject(p), reply(0) {}
    13.  
    14. void fetch(const QUrl &url) {
    15. reply = nam.get(QNetworkRequest(url));
    16. connect(reply, SIGNAL(finished()), SLOT(slotFinished()));
    17. connect(reply, SIGNAL(readyRead()), SLOT(slotReadyRead()));
    18. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(slotError(QNetworkReply::NetworkError)));
    19. }
    20.  
    21. public slots:
    22. void slotError(QNetworkReply::NetworkError code) {
    23. qDebug() << Q_FUNC_INFO << "Error" << code;
    24. }
    25.  
    26. void slotReadyRead() {
    27. qDebug() << Q_FUNC_INFO << "Bytes avail" << reply->bytesAvailable();;
    28. }
    29.  
    30. void slotFinished() {
    31. QByteArray ba = reply->readAll();
    32. qDebug() << ba;
    33. reply->deleteLater();
    34. qApp->exit();
    35. }
    36.  
    37. private:
    38. QNetworkAccessManager nam;
    39. QNetworkReply *reply;
    40. };
    41.  
    42. int main(int argc, char *argv[])
    43. {
    44. QCoreApplication app(argc, argv);
    45. QNetworkProxyFactory::setUseSystemConfiguration(true);
    46. Fetcher f;
    47. f.fetch(QUrl("http://www.google.com"));
    48. return app.exec();
    49. }
    50. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  8. #5
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager not reaching to google.com

    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.

  9. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QNetworkAccessManager not reaching to google.com

    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.
    Quote Originally Posted by Me
    Qt network is processed asynchronously and you are blocking the program waiting for an event that cannot occur when you call waitForReadyRead(-1).

  10. #7
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager not reaching to google.com

    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.

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. ui->textEdit->append("Updating response now.");
    10. QNetworkProxyFactory::setUseSystemConfiguration(true);
    11. QUrl a("http://google.co.in",QUrl::StrictMode);
    12. reply = manager.get(QNetworkRequest(a));
    13. connect(reply, SIGNAL(finished()), SLOT(slotFinished()));
    14. connect(reply, SIGNAL(readyRead()), SLOT(slotReadyRead()));
    15. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(slotError(QNetworkReply::NetworkError)));
    16. }
    17.  
    18. MainWindow::~MainWindow()
    19. {
    20. delete ui;
    21. }
    22.  
    23. void MainWindow::doneSlot(QNetworkReply *reply)
    24. {
    25. ui->textEdit->append("Reading reply now");
    26. QString response = reply->readAll().data();
    27. ui->textEdit->append(response);
    28. }
    29.  
    30. void MainWindow::slotFinished()
    31. {
    32. QByteArray ba = reply->readAll();
    33. qDebug() << ba;
    34. ui->textEdit->append(ba);
    35. reply->deleteLater();
    36. }
    37.  
    38. void MainWindow::slotReadyRead()
    39. {
    40. qDebug() << Q_FUNC_INFO << "Bytes avail" << reply->bytesAvailable();
    41. }
    42.  
    43. void MainWindow::slotError(QNetworkReply::NetworkError code)
    44. {
    45. qDebug() << Q_FUNC_INFO << "Error" << code;
    46. }
    To copy to clipboard, switch view to plain text mode 

  11. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkAccessManager not reaching to google.com

    Your proxy factory gets destroyed when the constructor of MainWindow ends.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QNetworkAccessManager not reaching to google.com

    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.

Similar Threads

  1. Google Analytics for QT
    By ibingame in forum Qt Programming
    Replies: 3
    Last Post: 8th August 2012, 11:24
  2. Does anyone know google earth?
    By miaoliang in forum General Discussion
    Replies: 1
    Last Post: 30th November 2006, 12:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.