Results 1 to 13 of 13

Thread: QHttp related

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Kranj, Slovenia
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5

    Default QHttp related

    I have set a function that goes through the database. from it it collect url's and download the files. at some url's i get a response "Host XYZ not found" and the function does not work anymore (the function is called from a loop that goes from the first record to the last) - the QHttp get() does not work any more. the function start workiing again if i start it manually from button on the GUI. What is the difference betwet calling a function from the loop and from the GUI?
    Regards;
    Luka

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QHttp related

    Quote Originally Posted by whoops.slo
    What is the difference betwet calling a function from the loop and from the GUI?
    None, if you have a single thread. Maybe there's something wrong with your application's logic? How many QHttp objects do you have?

  3. #3
    Join Date
    Jan 2006
    Location
    Kranj, Slovenia
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5

    Default Re: QHttp related

    Here is the code of the app.:
    Qt Code:
    1. void polje::skoci() {
    2.  
    3. int i = 0;
    4. bool bOk;
    5. QString str = ui.t_1->text();
    6. int st = str.toInt(&bOk) + 1;
    7.  
    8. //write new number into textfield t_1
    9. ui.t_1->setText(QString::number(st));
    10.  
    11. //define http path = in this example i'm using static URL, in the real case
    12. //it is collected from database
    13. //the URL below is the one making problems... <=host not found since it does not exist, but the loop end and I can not start it
    14. QUrl urlt("http://www.anabanana.brb/");
    15.  
    16. //the URL below has corect host address, but the file is missing - responseHeader code 404
    17. // QUrl urlt("http://www.bubi.si/luka.html");
    18. //the URL below is correct, just to test if the app. work
    19. // QUrl urlt("http://www.google.com/");
    20.  
    21. //define the path to the file into which we will write the contend of the page
    22. QDir pot(qApp->applicationDirPath());
    23. if(!pot.exists("html")) {
    24. pot.mkdir("html");
    25. }
    26. file = new QFile("html/blog_" + QString::number(st) + ".html");
    27. if (!file->open(QIODevice::WriteOnly)) {
    28. QMessageBox::critical(this, "Napaka", "Ne morem odpreti datoteke.");
    29. delete file;
    30. return;
    31. }
    32.  
    33. //open the connection and download the content of the page
    34. //into the file specified
    35. http->setHost(urlt.host());
    36. httpGetId = http->get(urlt.path(), file);
    37.  
    38. }
    39.  
    40. void polje::httpRequestFinished(int requestId, bool error) {
    41.  
    42. if (requestId != httpGetId)
    43. return;
    44.  
    45. //we close the specific file
    46. file->close();
    47.  
    48. if (error) {
    49. //insert the error string into textfield t_2
    50. //just to find out what the error is
    51. ui.t_2->insertPlainText("\n" + ui.t_1->text() + ": " + http->errorString());
    52. }
    53. else {
    54. bool bOk;
    55. QString str = ui.t_1->text();
    56. int st = str.toInt(&bOk) + 1;
    57. if (st <= 10) {
    58. skoci();
    59. }
    60. }
    61. }
    62.  
    63. void polje::readResponseHeader(const QHttpResponseHeader &responseHeader) {
    64.  
    65. ui.t_2->insertPlainText("\n" + ui.t_1->text() + ": " + QString::number(responseHeader.statusCode()));
    66.  
    67. }
    To copy to clipboard, switch view to plain text mode 

    I have defined QHttp and the rest of undefined variables in the header file in the private section:
    Qt Code:
    1. QHttp *http;
    2. QFile *file;
    3. int httpGetId;
    To copy to clipboard, switch view to plain text mode 
    Can anybody find the explanation and perhaps even the solution? Thank you!

    Regards;
    Luka

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QHttp related

    Well... I don't see any loop here. Anyway in case of an error you land here:
    Qt Code:
    1. if (error) {
    2. //insert the error string into textfield t_2
    3. //just to find out what the error is
    4. ui.t_2->insertPlainText("\n" + ui.t_1->text() + ": " + http->errorString());
    5. }
    To copy to clipboard, switch view to plain text mode 
    Could you explain what do you want to do then? What do you mean by "the loop end and I can not start it"?

    One more thing: IMO it would be better to change:
    Qt Code:
    1. if (st <= 10) {
    2. skoci();
    3. }
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. if (st <= 10) {
    2. QTimer::singleShot( 0, this, SLOT( skoci() ) );
    3. }
    To copy to clipboard, switch view to plain text mode 
    This way QHttp will have time to clean up itself after emitting requestFinished() signal.

  5. #5
    Join Date
    Jan 2006
    Location
    Kranj, Slovenia
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5

    Default Re: QHttp related

    thanks for noticing that. it was my mistake, when i rewrite the code to make it clearer and to show just where my problem is i forgot to insert a lass to skoci() function...
    Qt Code:
    1. if (error) {
    2. //insert the error string into textfield t_2
    3. //just to find out what the error is
    4. ui.t_2->insertPlainText("\n" + ui.t_1->text() + ": " + http->errorString());
    5. skoci();
    6. }
    7. else {
    8. bool bOk;
    9. QString str = ui.t_1->text();
    10. int st = str.toInt(&bOk) + 1;
    11. if (st <= 10) {
    12. skoci();
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    As far as I understand QHttp mechanism (am a beginner at it) when a get() is finished a requestFinished() signal is send. I have connected requestFinished() with httpRequestFinished() and responseHeaderReceived() with readResponseHeader()... Therefor when get() finish the httpRequestFinished() is called. At that point I call again the function skoci() and repeat this 10 times. It works with second and third URL (responseHeaderRecieved codes 404 and 200, but with the first one I do not get a responseHeaderReceived() since the host does not exist. In return I get an error, but when I try to call skoci() function from that error I call it but the get() is not called again...
    Timer::SingleShot() does not work in this contect - the second time the function skoci() is not called!
    There is obiously something wrong with my aproach to this problem, but I can not find where I made a mistake...

    Regards, Luka

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QHttp related

    Quote Originally Posted by whoops.slo
    It works with second and third URL (responseHeaderRecieved codes 404 and 200, but with the first one I do not get a responseHeaderReceived() since the host does not exist. In return I get an error, but when I try to call skoci() function from that error I call it but the get() is not called again...
    Do you change the host after the error has occured?

    Add this at the beginning of httpRequestFinished():
    Qt Code:
    1. qDebug() << httpGetId << requestId << error;
    To copy to clipboard, switch view to plain text mode 
    and this at the end of skoci():
    Qt Code:
    1. qDebug() << urlt.host() << httpGetId;
    To copy to clipboard, switch view to plain text mode 
    to see what's happening (don't forget about #include <QtDebug>).

    Quote Originally Posted by whoops.slo
    Timer::SingleShot() does not work in this contect - the second time the function skoci() is not called!
    Is that skoci() method a slot? Anyway you can forget about QTimer as QHttp is reentrant, so you can safely invoke setHost() and get() whenever you want.

  7. #7
    Join Date
    Jan 2006
    Location
    Kranj, Slovenia
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5

    Default Re: QHttp related

    skoci() is not a slot - at least it was not meant to be...

    I try what you suggest - qDebbug(). I am using Win XP, CodeBlock, MinGW and GDB and am new with debbugers. Therefor I can not understand what I should read from it (perhaps I have some setting set incorrectly). If you or somebody else have time and will to teach me how to set debbuger in this enviroment I would be most greatefull, since than I could at least look into what my app. is actually working

    Thanks anyway for your help, I hope you can help me further, but if it is a lost case than... I will understand

    Regards,
    Luka

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QHttp related

    Quote Originally Posted by whoops.slo
    skoci() is not a slot - at least it was not meant to be...
    That explains why the trick with QTimer didn't work.

    Quote Originally Posted by whoops.slo
    I am using Win XP, CodeBlock, MinGW and GDB and am new with debbugers. Therefor I can not understand what I should read from it
    Under windows you must add "CONFIG += console" to your .pro file to see the qDebug() output. Although debugger should be able to intercept these messages without it. Unfortunately I don't do much development under windows, so I can't help you with it.

  9. #9
    Join Date
    Jan 2006
    Location
    Kranj, Slovenia
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5

    Default Re: QHttp related

    I was not able to get anything from the debugger. Instead I put requested strigns to be send to the textbox. This is the response I got from using host, that do not exist:
    2
    2, 1
    2, 2
    1: Host www.anabanana.brb not found
    5
    And this is from the one that exist (The "loop" run 5 times):
    2
    2, 1
    1: 200
    2, 2
    4
    4, 3
    2: 200
    4, 4
    6
    6, 5
    3: 200
    6, 6
    8
    8, 7
    4: 200
    8, 8
    10
    10, 9
    5: 200
    10, 10
    I put the line below at the end of the skoci():
    Qt Code:
    1. ui.t_2->insertPlainText("\n" + QString::number(httpGetId));
    To copy to clipboard, switch view to plain text mode 
    and the line below at the beggining of httpRequestFinished():
    Qt Code:
    1. ui.t_2->insertPlainText("\n" + QString::number(httpGetId) + ", " + QString::number(requestId));
    To copy to clipboard, switch view to plain text mode 
    Can this help?
    Regards, Luka

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QHttp related

    What happens when you change skoci() like this?
    Qt Code:
    1. http->setHost( "localhost" ); // added
    2. http->setHost( urlt.host() );
    3. httpGetId = http->get( urlt.path(), file );
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Jan 2006
    Location
    Kranj, Slovenia
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5

    Default Re: QHttp related

    I get the same responce:
    3
    3, 1
    3, 2
    3, 3
    6
    I'll try something else... Before I call GET() I will check for ERROR(). If ERROR() is HostNotFound than GET() will not be called...
    Last edited by whoops.slo; 20th June 2006 at 07:08.

Similar Threads

  1. File Binary Upload QHttp find the bug/s
    By patrik08 in forum Qt Programming
    Replies: 13
    Last Post: 10th June 2008, 07:51
  2. QHttp has problems with some url's
    By whoops.slo in forum Qt Programming
    Replies: 2
    Last Post: 2nd June 2006, 23:16
  3. need help for QHttp
    By patcito in forum Qt Programming
    Replies: 9
    Last Post: 1st June 2006, 07:00
  4. using Qhttp with www(x) sites
    By importantman in forum Qt Programming
    Replies: 18
    Last Post: 4th April 2006, 00:12
  5. Help with QHttp needed.
    By bitChanger in forum Qt Programming
    Replies: 5
    Last Post: 25th January 2006, 21:09

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.