How can i download file from servrer right?



  • Hellow, i need to download file from server
    http://rizek.ucoz.ru/RizekQssEditor/a.txt
    I write:
    @ QFile file("a.txt");
    QFtp ftp;
    if (file.open(QIODevice::WriteOnly)){
    ftp.connectToHost("rizek.ucoz.ru");
    ftp.cd("RizekQssEditor");
    ftp.get("a.txt", &file);
    }@
    But when i download it, file in dir is empty, why is it?
    Advance many thanks for your help!



  • First of all that's a HTTP URL, not a FTP one. Why do you want to download it with FTP?
    Second: since it's a simple "get", you can use QNAM and not QFtp.
    Third: can you make a complete testcase? Are you waiting for the transfer to finish?


  • Moderators

    You should check the return values of get(...) for instance.
    Furthermore, you should consult the documentation of "QFtp::get":http://doc.qt.nokia.com/4.7/qftp.html#get it sais :
    "The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by commandStarted() and commandFinished()."
    So, you might exit the routine your snippet is from before the ftp is executed.



  • I try to use QHttp:
    @ QFile * file = new QFile("rizek.ucoz.ru");
    QHttp * http = new QHttp;
    if (file->open(QIODevice::WriteOnly)){
    http->setHost("foto.rambler.ru");
    http->get("/RizekQssEditor/a.txt", file);
    connect(http, SIGNAL(requestFinished(int,bool)), this, SLOT(replyFinished(int,bool)));@
    \Slot replyFinished
    @void Updater::replyFinished(int id,bool error)
    {
    label->setText(QVariant(id).toString() + " " + QVariant(error).toString());
    }@
    And text of label is "2 false" and there are empty file in the dir of program
    I am copied this example from the textbook, why it doesn not work?


  • Moderators

    You seem to have a hang of ignoring return values. May be you should check those as well. AFAIK the ids are incremented each time with a request at least for QFtp. So, presumably you are tracking the correct request, but you should make sure.
    Did you make sure that the file on your server has some contents?



  • QHttp is obsolete, use QNetworkAccessManager instead. The procedure is almost the same.

    bq. This class is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.



  • bq. Did you make sure that the file on your server has some contents?

    Yes, i am sure

    @May be you should check those as well.@

    How can i do it?


  • Moderators

    setHost and get return both an integer value. So you could assign the return values to int and check them. Like

    @ QFile * file = new QFile("rizek.ucoz.ru");
    QHttp * http = new QHttp;
    if (file->open(QIODevice::WriteOnly)){
    int ret0 = http->setHost("foto.rambler.ru");
    int ret1 = http->get("/RizekQssEditor/a.txt", file);
    connect(http, SIGNAL(requestFinished(int,bool)), this, SLOT(replyFinished(int,bool)));@

    You could use qDebug() for output or even write them to a file. But that is standard, so there should not be need to explain.

    However, did you notice
    [quote author="Lukas Geyer" date="1309465555"]QHttp is obsolete, use QNetworkAccessManager instead. The procedure is almost the same.

    bq. This class is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.[/quote]

    He is correct to switch to QNetworkAccessManager then.

    There seem to be already an example for what you are trying to do:
    "http example":http://doc.qt.nokia.com/4.7/network-http.html

    That is probably a good start for you. I did not try out, but typically the Qt are working and you can start to do your modifications from there.



  • You was right, i must be use QNetworkAccessManagder, i think that classes QHttp and QFtp dont work anymore



  • QHttp is there to keep backward compatibility(it does work) and should not be used for new software.

    Try using assistant more, it usually takes less time than waiting for an answer on devnet ;). And Qt has such grate documentation. BTW I think that the "This class is obsolete." part of QHttp documentation should be in red or any other eye catching color, because it's not uncommon to see people still trying to use it.


  • Moderators

    @kkrzewniak You are certainly right with your comment on QHttp and the same is true for QFtp.

    I like your idea of color coding when classes are no longer first choice. Even those who are careful manual readers ( I guess those are rare in development ;-) ) do not catch the deprecation of classes. Most of the time you are just looking which method you need. And that's it.



  • If you want to know what you were doing wrong with QHttp.. I had posted an example recently.. take a look at "this thread":http://developer.qt.nokia.com/forums/viewthread/6759

    Your slot is called multiple times but you should only react when the httpRequestId is matched.

    [ Edit: NOTE: QHttp is obsolete as pointed out by many others and shouldn't be used. ]



  • @jim_kaiser why are you telling people to use deprecated classes? You should stop such practices at once!! You are spreading confusion!!
    Fixed: thanks jim_kaiser



  • I simply pointed out what was wrong with his code. At the time I used the QHttp (the code posted).. there was no other option. I think it is pretty clear to everybody and spelled out many times that it is deprecated now. Honestly, I found that out myself just recently over here.

    Anyways, I apologize for the confusion and yes.. the OP in the thread I linked would also be better off using the QNetworkManager classes currently. Thanks for adding that info to the thread.

    [ Edit: Have edited my "post":http://developer.qt.nokia.com/forums/viewreply/39453/ and added the code using the QNetworkManager. Once again sorry about that.. honest mistake..]

    [ Edit2: @kkrzewniak: Off-topic but its "deprecated" not "depreciated".. we all get it.. but just saying.. :-) ]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.