Results 1 to 1 of 1

Thread: QHttp : Strange behavior with different host!

  1. #1
    Join Date
    Sep 2008
    Location
    Bangladesh
    Posts
    17
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default QHttp : Strange behavior with different host!

    I've written a class that downloads a single file. Surprisingly, I've noticed that the program successfully downloads one file but fails for another file that has a different host.

    I'm curious to know the reason. Please tell me if you can figure out.

    Here are my classes:

    myhttp.h file:

    Qt Code:
    1. #ifndef MYHTTP_H
    2. #define MYHTTP_H
    3.  
    4. #include <QObject>
    5. #include <QHttp>
    6. #include <QFile>
    7.  
    8. class MyHttp : public QObject
    9. {
    10. Q_OBJECT
    11.  
    12. private:
    13. QHttp http;
    14. QFile file;
    15.  
    16. public:
    17. MyHttp(const QString& strFilePath, QObject* parent = 0);
    18.  
    19. signals:
    20. void finished();
    21.  
    22. private slots:
    23. void myHttpDone(bool error);
    24. };
    25.  
    26. #endif
    To copy to clipboard, switch view to plain text mode 

    myhttp.cpp file:

    Qt Code:
    1. #include <QtNetwork>
    2.  
    3. #include <QFileInfo>
    4. #include <QUrl>
    5.  
    6. #include "MyHttp.h"
    7.  
    8. MyHttp::MyHttp(const QString& strFilePath, QObject* parent /* = 0 */) : QObject(parent)
    9. {
    10. connect(&http, SIGNAL(done(bool)), this, SLOT(myHttpDone(bool)));
    11.  
    12. QUrl url(strFilePath);
    13.  
    14. QFileInfo fileInfo(url.path());
    15. file.setFileName(fileInfo.fileName());
    16. file.open(QIODevice::WriteOnly);
    17.  
    18. http.setHost(url.host(), url.port(80));
    19. http.get(url.path(), &file);
    20. http.close();
    21. }
    22.  
    23. void MyHttp::myHttpDone(bool error)
    24. {
    25. file.close();
    26.  
    27. if (error == true)
    28. {
    29. file.remove();
    30. }
    31.  
    32. emit finished();
    33. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp file:

    Qt Code:
    1. #include <QCoreApplication>
    2.  
    3. #include "MyHttp.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication app(argc, argv);
    8.  
    9. // program doesn't work for this link
    10. // MyHttp file("http://thedailystar.net/images/tdsmainlogo.jpg");
    11.  
    12. // program works for this link
    13. MyHttp file("http://ahmadferdous.googlepages.com/Ferdous.JPG");
    14.  
    15. QObject::connect(&file, SIGNAL(finished()), &app, SLOT(quit()));
    16.  
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

Similar Threads

  1. Very strange (perhaps) QHttp behaviour.
    By Kumosan in forum Qt Programming
    Replies: 5
    Last Post: 11th June 2008, 07:03
  2. QHttp : Host vs IP adress
    By Nyphel in forum Newbie
    Replies: 5
    Last Post: 15th April 2007, 11:21

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.