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

Thread: Qhttp

  1. #1
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Qhttp

    I have the QT4 example network.http working. I mean I press download and it does it.
    But I want to call the download directly from the code without going through this button.

    So I edited the example to remove the download button and call directly as follows :
    Qt Code:
    1. quitButton = new QPushButton(tr("Quit"));
    2. // downloadButton = new QPushButton(tr("Download"));
    3. quitButton->setDefault(true);
    4.  
    5. http = new QHttp(this);
    6.  
    7. connect(http, SIGNAL(requestFinished(int, bool)),
    8. this, SLOT(httpRequestFinished(int, bool)));
    9. connect(http, SIGNAL(dataReadProgress(int, int)),
    10. this, SLOT(updateDataReadProgress(int, int)));
    11. connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
    12. this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
    13. // connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
    14. connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
    15.  
    16. QHBoxLayout *buttonLayout = new QHBoxLayout;
    17. buttonLayout->addStretch(1);
    18. // buttonLayout->addWidget(downloadButton);
    19. buttonLayout->addWidget(quitButton);
    20.  
    21. QVBoxLayout *mainLayout = new QVBoxLayout;
    22. mainLayout->addLayout(buttonLayout);
    23. setLayout(mainLayout);
    24.  
    25. setWindowTitle(tr("HTTP"));
    26.  
    27. QString url = "http://api.smsbox.fr/index.php?login=ahcoeur&pass=aaaaaa&action=credit";
    28. downloadFile(url);
    29. }
    30.  
    31. void HttpWindow::downloadFile(QString mUrl)
    To copy to clipboard, switch view to plain text mode 


    All works well, the url is called, the download is done and the remaining Quit button screen is briefly displayed, then it crashes.

    It looks like it is the return to the main event loop that aborts but why ?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Qhttp

    Qt Code:
    1. void HttpWindow::showEvent(QShowEvent* e)
    2. {
    3. QDialog::showEvent(e);
    4. QString url = "http://api.smsbox.fr/index.php?login=ahcoeur&pass=aaaaaa&action=credit";
    5. downloadFile(url);
    6. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    incapacitant (15th May 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Qhttp

    My main program is now reduced to :

    Qt Code:
    1. HttpWindow::HttpWindow(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4.  
    5. http = new QHttp(this);
    6.  
    7. connect(http, SIGNAL(requestFinished(int, bool)),
    8. this, SLOT(httpRequestFinished(int, bool)));
    9. connect(http, SIGNAL(dataReadProgress(int, int)),
    10. this, SLOT(updateDataReadProgress(int, int)));
    11. connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
    12. this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
    13.  
    14. showEvent(e);
    15. }
    16.  
    17. void HttpWindow::showEvent(QShowEvent* e)
    18. {
    19. QDialog::showEvent(e);
    20. QString url = "http://api.smsbox.fr/index.php?login=ahcoeur&pass=aaaaaa&action=credit";
    21. downloadFile(url);
    22. }
    To copy to clipboard, switch view to plain text mode 

    The result is a bit similar, but I may misuse your code. For instance I have a compile warning :
    Qt Code:
    1. 19 C:\root\dev\Qt\test\http\http\httpwindow.cpp [Warning] 'e' might be used uninitialized in this function
    To copy to clipboard, switch view to plain text mode 

    The application is executed, the url called, the file downloaded but again when it comes back to what I assume is the main loop it aborts.

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Qhttp

    Don't call the showEvent() yourself. It gets called when the HttpWindow dialog is shown.

    The event loop is not yet running by the time when the constructor of the HttpWindow dialog is called. That's why I moved the piece of code which starts the downloading automatically, to the show event.
    J-P Nurmi

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

    incapacitant (15th May 2006)

  7. #5
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qhttp

    so now I have :
    Qt Code:
    1. HttpWindow::HttpWindow(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4.  
    5. http = new QHttp(this);
    6.  
    7. connect(http, SIGNAL(requestFinished(int, bool)),
    8. this, SLOT(httpRequestFinished(int, bool)));
    9. connect(http, SIGNAL(dataReadProgress(int, int)),
    10. this, SLOT(updateDataReadProgress(int, int)));
    11. connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
    12. this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
    13.  
    14. }
    15.  
    16. void HttpWindow::showEvent(QShowEvent* e)
    17. {
    18. QDialog::showEvent(e);
    19. QString url = "http://api.smsbox.fr/index.php?login=ahcoeur&pass=aaaaaa&action=credit";
    20. downloadFile(url);
    21. }
    To copy to clipboard, switch view to plain text mode 

    The result is still not perfect. The new thing is that a big empty window is displayed, that I could do without. And when the downoload is completed it still crashes.

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

    Default Re: Qhttp

    So if you don't need a window, why do you create one?

    Qt Code:
    1. QApplication app(argc, argv);
    2. QHttp http;
    3. QFile file("writetothisfile");
    4. file.open(QIODevice::WriteOnly);
    5. connect(&http, SIGNAL(done(bool)), &app, SLOT(quit()));
    6. http.get("http://api.smsbox.fr/index.php?login=ahcoeur&pass=aaaaaa&action=credit", &file);
    7. return app.exec();
    To copy to clipboard, switch view to plain text mode 

    Of course you can connect something else than quit() to this signal
    Last edited by wysota; 15th May 2006 at 16:00.

  9. The following 2 users say thank you to wysota for this useful post:

    incapacitant (17th May 2006), patcito (31st May 2006)

  10. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Qhttp

    I somewhy have a feeling that you simply want to show the amount of credits in a way or another...

    Here's for example a line edit which does the job:
    Qt Code:
    1. #include <QApplication>
    2. #include <QLineEdit>
    3. #include <QHttp>
    4. #include <QUrl>
    5. #include <QBuffer>
    6.  
    7. class Credits: public QLineEdit
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. Credits(QWidget* parent = 0): QLineEdit(parent)
    13. {
    14. setReadOnly(true);
    15.  
    16. // target url
    17. QUrl url("http://api.smsbox.fr/index.php?login=ahcoeur&pass=aaaaaa&action=credit");
    18.  
    19. http = new QHttp(this);
    20. connect(http, SIGNAL(done(bool)), this, SLOT(showCredits(bool)));
    21. // remember to set the host
    22. http->setHost(url.host());
    23.  
    24. // the resulting data will be stored in the buffer
    25. buffer = new QBuffer(this);
    26. http->get(url.toString(), buffer);
    27. }
    28.  
    29. private slots:
    30. void showCredits(bool error)
    31. {
    32. if (error)
    33. setText(http->errorString());
    34. else
    35. setText(QString(buffer->data()));
    36. }
    37.  
    38. private:
    39. QHttp* http;
    40. QBuffer* buffer;
    41. };
    42.  
    43. int main(int argc, char *argv[])
    44. {
    45. QApplication app(argc, argv);
    46. Credits c;
    47. c.show();
    48. app.connect(&app, SIGNAL(lastWindowClose()), &app, SLOT(quit()));
    49. return app.exec();
    50. }
    51.  
    52. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  11. The following user says thank you to jpn for this useful post:

    incapacitant (17th May 2006)

  12. #8
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Qhttp

    wysota:

    I used your sample to write this main.cpp :
    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3. #include <QtCore>
    4. #include <QtNetwork>
    5. #include <QDialog>
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication app(argc, argv);
    10. QHttp http;
    11. QFile file("writetothisfile");
    12. file.open(QIODevice::WriteOnly);
    13. connect(&http, SIGNAL(done(bool)), &app, SLOT(quit()));
    14. http.get("http://api.smsbox.fr/index.php?login=ahcoeur&pass=aaaaaa&action=credit", &file);
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    and I get a compile error :
    Qt Code:
    1. 13 C:\root\dev\Qt\test\http\http-wysota\main.cpp `connect' undeclared (first use this function)
    To copy to clipboard, switch view to plain text mode 

    The background is that within an application that has windows I try to call urls (I try this one because it is the easiest) but I want nothing to be displayed during the call and I want to call the url without a button.

    Meanwhile I will look at jpn's proposal...

  13. #9
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Qhttp

    wysota:
    I replaced :
    Qt Code:
    1. connect(&http, SIGNAL(done(bool)), &app, SLOT(quit()));
    To copy to clipboard, switch view to plain text mode 

    with

    Qt Code:
    1. app.connect(&http, SIGNAL(done(bool)), &app, SLOT(quit()));
    To copy to clipboard, switch view to plain text mode 
    and it compiles.

    Then it runs but I doubt that the url is called as usually I am warned by Norton Internet Security that the code is accessing the outside. Anyway the file is created but empty.

  14. #10
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Qhttp

    jpn:
    Your code works wonderfully. I will try to adapt it to my needs.
    For information what is the reason of this line :
    Qt Code:
    1. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  15. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Qhttp

    I was just lazy and didn't want to add separate header and implementation files for the Credits class. By including "main.moc", qmake knows to run moc on the main.cpp file. And moc'ing is required because the class uses signals and slots.
    J-P Nurmi

  16. The following user says thank you to jpn for this useful post:

    incapacitant (17th May 2006)

  17. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qhttp

    Quote Originally Posted by incapacitant
    Then it runs but I doubt that the url is called as usually I am warned by Norton Internet Security that the code is accessing the outside. Anyway the file is created but empty.
    Maybe it doesn't know how to extract all data from the url. Use QHttp::setHost to set the server address and port and then use the rest of the url as an argument to get().

    My point was to show you that you don't need any windows to use QHttp.

  18. #13
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Qhttp

    jpn:
    People say you should first learn C++, then QT. I did not. I will register to a university this fall to learn C++. I say that because I can't really understand your code structure because I don't know enough of C++.


    Anyway I could extract what I need to make it work. I still have a problem though.

    First from my main code I call :
    Qt Code:
    1. qS = testUrl();
    To copy to clipboard, switch view to plain text mode 
    It fills in qS that I can put on the first window that I display.

    testUrl() is :
    Qt Code:
    1. QString ApplicationWindow::testUrl()
    2. {
    3. // target url
    4. QUrl url("http://api.smsbox.fr/index.php?login=ahcoeur&pass=aaaaaa&action=credit");
    5.  
    6. http = new QHttp(this);
    7. connect(http, SIGNAL(done(bool)), this, SLOT(defineData(bool)));
    8. // remember to set the host
    9. http->setHost(url.host());
    10.  
    11. // the resulting data will be stored in the buffer
    12. buffer = new QBuffer(this);
    13. http->get(url.toString(), buffer);
    14. QMessageBox::warning( this, "Http", QString(buffer->data()) );
    15. return QString(buffer->data());
    16. }
    To copy to clipboard, switch view to plain text mode 

    defineData is just a return statement.

    As listed above it works fine, first by displaying this QMessageBox that I put there for tracing what was return. By the way allthough the right value is returned the MessageBox does not display the text !!

    The weird thing is that is I comment out the QMessageBox call then it does not work.

    What I see when it works:
    the message box with title Http but no value (?) and a OK button, I click
    my first window with this credit value correctly retrieved.

    What I see when it does not work (ie I comment out the QMessageBox)
    my first window with an empty line and no credit value.

    From day one when I started with Qhttp I noticed this kind of behaviour where the code is not executed sequentially.

  19. #14
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Qhttp

    Quote Originally Posted by incapacitant
    As listed above it works fine, first by displaying this QMessageBox that I put there for tracing what was return. By the way allthough the right value is returned the MessageBox does not display the text !!
    You cannot do it like this. QHttp::get() is asynchronous and does not cause the data to be stored in the buffer right away. QHttp offers a bunch of signals, which are emitted when something significant has happened. Basically there can't be a function which returns the data result of an http request. You will have the data by the time a slot connected to for example QHttp::done(bool) signal gets called. As you can see from my example, the resulting credits text is set in the slot connected to the QHttp::done(bool), not for example in the constructor.

    Quote Originally Posted by incapacitant
    The weird thing is that is I comment out the QMessageBox call then it does not work.
    It "works" with the message box only because the http request may be (but not necessarily) finished meanwhile the message box is shown.

    Quote Originally Posted by incapacitant
    From day one when I started with Qhttp I noticed this kind of behaviour where the code is not executed sequentially.
    QHttp::get():
    The function does not block and returns immediately. The request is scheduled, and its execution is performed asynchronously.
    To state it one more time, you will get notified by a signal when it's done...
    Last edited by jpn; 17th May 2006 at 15:44.
    J-P Nurmi

  20. The following user says thank you to jpn for this useful post:

    incapacitant (17th May 2006)

  21. #15
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Qhttp

    jpn

    Thank you for all your good advice. Since http completes when the slot is activated, I moved the rest of my main code to the http slot and it works fine.

    It looks like cascading down, moving the the main program down from slot to slot.

  22. #16
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Qhttp

    New issue :

    This works :
    Qt Code:
    1. QString mUrl;
    2. mUrl = "http://api.smsbox.fr/index.php?login=ahcoeur&pass=aaaaaa&action=credit";
    3. QUrl url(mUrl);
    To copy to clipboard, switch view to plain text mode 

    This does not work (ie the slot when activated returns error)
    Qt Code:
    1. QString mUrl;
    2. mUrl = QString("/index.php?login=%1&pass=%2&action=%3")
    3. .arg( Login, Password, Action );
    4. QUrl url(mUrl);
    To copy to clipboard, switch view to plain text mode 

    This also does not work (ie url is called but server says invalid password)
    Qt Code:
    1. QString Password = "aaaaaa";
    2. QString mUrl;
    3. mUrl = "http://api.smsbox.fr/index.php?";
    4. mUrl.append("login=ahcoeur&pass=");
    5. mUrl.append(Password);
    6. mUrl.append("&action=credit");
    7. QUrl url(mUrl);
    To copy to clipboard, switch view to plain text mode 

    My url contains variables. I used a constant for the test and now I want to insert my parameters. I tried various combinations but nothing works so far.

  23. #17
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Qhttp

    Does this work?
    Qt Code:
    1. QUrl url("http://api.smsbox.fr/index.php");
    2. url.addQueryItem("login", Login);
    3. url.addQueryItem("pass", Password);
    4. url.addQueryItem("action", Action);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  24. The following user says thank you to jpn for this useful post:

    incapacitant (18th May 2006)

  25. #18
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Qhttp

    jpn:

    No, I get the url through but the server replies invalid password or login.
    So the parameters are not inserted as they should.

    The only case when when it works is when I use QUrl url("blah...blah...blah");
    Only a string of characters between quotes.

  26. #19
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Qhttp

    Are you 100% sure about the user/passwd/act arguments' correctness? Did you try to output the resulting url?

    Qt Code:
    1. #include <QtDebug>
    2. ...
    3. QUrl url(..);
    4. url.addQueryItem(...);
    5. qDebug() << url;
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  27. The following user says thank you to jpn for this useful post:

    incapacitant (19th May 2006)

  28. #20
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Qhttp

    I tried qDebug() but I don't know where the output goes... I ran it from a DOS window and nothing is displayed.

    So I tried this :
    Qt Code:
    1. QUrl url("http://api.smsbox.fr/index.php?login=ahcoeur&pass=aaaaaa");
    2. // url.addQueryItem("login", Login);
    3. // url.addQueryItem("pass", Password);
    4. url.addQueryItem("action", Action);
    To copy to clipboard, switch view to plain text mode 


    And this works "Action" seems to be appended correctly.
    Then I tried :
    Qt Code:
    1. QUrl url("http://api.smsbox.fr/index.php?login=ahcoeur")
    2. // url.addQueryItem("login", Login);
    3. url.addQueryItem("pass", Password);
    4. url.addQueryItem("action", Action);
    To copy to clipboard, switch view to plain text mode 

    The url goes through and the server replies invalid password or login. Strange.

    I would like to display the url with QMessageBox but first I need to find how to convert a QUrl to QString

Similar Threads

  1. new QHttp() Error
    By fengtian.we in forum Qt Programming
    Replies: 7
    Last Post: 6th October 2010, 17:56
  2. From QHttp to QHttp over SSL
    By Nyphel in forum Newbie
    Replies: 1
    Last Post: 3rd July 2007, 10:41
  3. QHttp with localhost.
    By William Wilson in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2007, 20:26
  4. QHttp download file problem.
    By fengtian.we in forum Qt Programming
    Replies: 12
    Last Post: 12th June 2007, 09:39
  5. how to use QHttp inside QThread in Qt3
    By alusuel in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2006, 11:19

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.