Page 1 of 2 12 LastLast
Results 1 to 20 of 28

Thread: How to download a file using HTTP with URL in QT?

  1. #1
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default How to download a file using HTTP with URL in QT?

    Qt Code:
    1. QUrl url = QUrl::fromUserInput(strUrl);
    2. QFileInfo fileInfo(url.path());
    3. QString fileName=fileInfo.fileName();
    4. if (QFile::exists(fileName)) {
    5. QMessageBox::information(this, tr("HTTP"),
    6. tr("There already exists a file called %1 in "
    7. "the current directory.")
    8. .arg(fileName));
    9. }
    10.  
    11. file = new QFile(fileName);
    12. if (!file->open(QIODevice::WriteOnly)) {
    13. QMessageBox::information(this, tr("HTTP"),
    14. tr("Unable to save the file %1: %2.")
    15. .arg(fileName).arg(file->errorString()));
    16. delete file;
    17. file = 0;
    18. }
    19.  
    20. file.setFileName(fileName);
    21. file.open(QIODevice::WriteOnly);
    22. QHttp *http;
    23. http=new QHttp(this);
    24. setHost(url.host(),QHttp::ConnectionModeHttp);
    To copy to clipboard, switch view to plain text mode 


    Am getting while using the HTTP . It says undefined reference to the http object.
    I just want to download a file from the host.
    Last edited by wysota; 8th March 2011 at 08:26. Reason: missing [code] tags

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to download a file using HTTP with URL in QT?

    What include files have you specified?

  3. #3
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to download a file using HTTP with URL in QT?

    I have included <QNetworkAccessManager>, <QHttp>

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to download a file using HTTP with URL in QT?

    And you have no other errors during compilation ? Do you have Qt += network in .pro file ?

  5. #5
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to download a file using HTTP with URL in QT?

    No. I dont have errors. And also no Qt +=network in my .pro file.

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to download a file using HTTP with URL in QT?

    And also no Qt +=network in my .pro file
    So add it

  7. #7
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to download a file using HTTP with URL in QT?

    Well if u want to work with network u have to have the Qt += network

  8. #8
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to download a file using HTTP with URL in QT?

    Yes, I have added it. Upto this everything is fine. Next is how to download that file.


    Added after 1 4 minutes:


    Here is the code:: Pls help me..

    Qt Code:
    1. QUrl url = QUrl::fromUserInput(strUrl);
    2.  
    3. QFileInfo fileInfo(url.path());
    4.  
    5. QString fileName=fileInfo.fileName();
    6.  
    7. if (QFile::exists(fileName)) {
    8.  
    9. QMessageBox::information(this, tr("HTTP"),
    10.  
    11. tr("There already exists a file called %1 in "
    12.  
    13. "the current directory.")
    14. .arg(fileName));
    15.  
    16. }
    17.  
    18.  
    19.  
    20. file = new QFile(fileName);
    21.  
    22. if (!file->open(QIODevice::WriteOnly)) {
    23.  
    24. QMessageBox::information(this, tr("HTTP"),
    25.  
    26. tr("Unable to save the file %1: %2.")
    27.  
    28. .arg(fileName).arg(file->errorString()));
    29.  
    30. delete file;
    31.  
    32. file = 0;
    33. }
    34.  
    35.  
    36.  
    37. file.setFileName(fileName);
    38. file.open(QIODevice::WriteOnly);
    39.  
    40. QHttp http;
    41. QEventLoop loop;
    42. QFile file;
    43. QObject::connect(&http, SIGNAL(done(bool)), &loop, SLOT(quit()));
    44.  
    45. file.setFileName(filename);
    46. file.open(QIODevice::WriteOnly);
    47.  
    48. http.setHost(url.host(), url.port(80));
    49. http.get(url.toEncoded(QUrl::RemoveScheme | QUrl::RemoveAuthority),
    50. &file);
    51.  
    52. loop.exec();
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 10th March 2011 at 14:39. Reason: missing [code] tags

  9. #9
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to download a file using HTTP with URL in QT?

    You should consider using QAccessManager instead since QHttp is obsolete and qt strongly recommends not using it in new code

    Qt Code:
    1. QNetworkAccessManager* mNetworkManager = new QNetworkAccessManager(this);
    2. QObject::connect(mNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onNetworkReply(QNetworkReply*)));
    3.  
    4. QUrl url = "http://someurl.com";
    5. QNetworkReply* reply = mNetworkManager->get(QNetworkRequest(url));
    6.  
    7. void onNetworkReply(QNetworkReply* reply)
    8. {
    9. QString replyString;
    10. if(reply->error() == QNetworkReply::NoError)
    11. {
    12. int httpstatuscode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toUInt();
    13. switch(httpstatuscode)
    14. {
    15. case RESPONSE_OK:
    16. if (reply->isReadable())
    17. {
    18. //Assuming this is a human readable file replyString now contains the file
    19. replyString = QString::fromUtf8(reply->readAll().data());
    20. }
    21. break;
    22. case RESPONSE_ERROR:
    23. case RESPONSE_BAD_REQUEST:
    24. default:
    25. break;
    26. }
    27. }
    28.  
    29. reply->deleteLater();
    30. }
    To copy to clipboard, switch view to plain text mode 

  10. The following user says thank you to Berryblue031 for this useful post:

    Gokulnathvc (8th March 2011)

  11. #10
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to download a file using HTTP with URL in QT?

    It says incomplete type struct QNetworkRequest and lot more errors.. Could you pls help me..

  12. #11
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to download a file using HTTP with URL in QT?

    You need include

    Qt Code:
    1. #include <QtNetwork/QNetworkReply>
    2. #include <QtNetwork/QNetworkAccessManager>
    To copy to clipboard, switch view to plain text mode 

    in your cpp file

  13. #12
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to download a file using HTTP with URL in QT?

    It shows RESPONSE_ERROR was not declared in this scope..

  14. #13
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to download a file using HTTP with URL in QT?

    RESPONSE_OK, RESPONSE_ERROR, and RESPONSE_BAD_REQUEST are integers you need to define yourself depending on what status codes your webserver returns,

    http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

  15. #14
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to download a file using HTTP with URL in QT?

    Could you please explain the complete downloading of the file from the Host to the local machine?? please help me

  16. #15
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to download a file using HTTP with URL in QT?

    It's documented and even coded for you above by Berryblue031, which part don't you understand?

  17. #16
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to download a file using HTTP with URL in QT?

    No actually. its not getting saved in the QFile. How to save that and also QNetworkreply shows errors like.. invalid struct. declaration error.

  18. #17
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to download a file using HTTP with URL in QT?

    You need to be more descriptive.

  19. #18
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to download a file using HTTP with URL in QT?

    If you take a look at the documentation qnetworkreply->readAll().data() returns a qbytearray and QFile.write takes a qbytearray, so simply create your file like you did in your orignal code and read in the data from the qnetworkreply.

    If you check the documentation this example http://doc.trolltech.com/4.7-snapshot/network-http.html does everything you are trying to do.

  20. #19
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to download a file using HTTP with URL in QT?

    File is created but empty one.. How to read it properly from http and write it to a file.


    Added after 1 7 minutes:


    QHttp http;
    QFile file;
    file.setFileName("C:\\Qt\\OCZToolbox\\"+fileName+" .vic");
    //http.setSocket(&socket);
    //header.setValue("HOST",strhost);
    //QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
    //socket.connectToHost(url.host(),80);
    //http.setHost(url.host());
    //int httpGetId = http.get(url.path(), &file);

    connect (&http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
    //QUrl &url;
    http.setHost(url.host(),QHttp::ConnectionModeHttp) ;
    http.get(url.path(),&file);

    //http.request(header,0);

    file.open(QIODevice::WriteOnly);
    //file.write(http.readAll());
    file.close();
    http.close();

    Using this code Qfile is created but it is 0 kb. nothing is inside.. pls pls pls help me
    Last edited by Gokulnathvc; 14th March 2011 at 06:02.

  21. #20
    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: How to download a file using HTTP with URL in QT?

    I don't know why you are persisting with using QHttp when both the posts in this thread and the advice in the Qt documentation tell you to use QNetworkAccessManager. From the QHttp docs:
    This class is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.
    and
    This class provides a direct interface to HTTP that allows you to download and upload data with the HTTP protocol. However, for new applications, it is recommended to use QNetworkAccessManager and QNetworkReply, as those classes possess a simpler, yet more powerful API and a more modern protocol implementation.
    You do have a good reason... don't you? If you were using the recommended network classes then you would find there are examples doing exactly this sort of thing.

    Here is your code without your hack and slash commenting,. and with some comments of my own.
    Qt Code:
    1. QHttp http; // Deprecated class, please use QNetworkAccessManager
    2.  
    3. // Define a file
    4. QFile file;
    5. file.setFileName("C:\\Qt\\OCZToolbox\\"+fileName+" .vic");
    6. // file is not open for read or write
    7.  
    8. connect (&http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
    9. http.setHost(url.host(),QHttp::ConnectionModeHttp) ;
    10.  
    11. // Try to get a the URL response into the file that is not open for writing
    12. http.get(url.path(),&file);
    13.  
    14. // That was an asynchronous operation you just started, so we get here immediately
    15.  
    16. // Ignore the fact that you have asked QHttp to write the file and try to do it yourself
    17. // Open for writing and truncate any existing file
    18. file.open(QIODevice::WriteOnly);
    19. // write nothing to the file and then close
    20. file.close();
    21.  
    22. http.close();
    To copy to clipboard, switch view to plain text mode 
    I have no idea why you are surprised that the file is empty.
    Last edited by ChrisW67; 14th March 2011 at 06:48.

Similar Threads

  1. HTTP pause download in QTCreator
    By nhs_0702 in forum Qt Programming
    Replies: 12
    Last Post: 29th November 2010, 14:24
  2. Download file to QString
    By Chiggins in forum Qt Programming
    Replies: 2
    Last Post: 25th June 2010, 19:16
  3. I want to download a piece of file?
    By mismael85 in forum General Programming
    Replies: 1
    Last Post: 27th February 2010, 15:35
  4. Download File
    By BalaQT in forum Qt Programming
    Replies: 2
    Last Post: 13th February 2010, 09:53
  5. Download via HTTP in several threads?
    By bjilly in forum Qt Programming
    Replies: 6
    Last Post: 30th November 2009, 14:15

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
  •  
Qt is a trademark of The Qt Company.