Results 1 to 10 of 10

Thread: HTTP request

  1. #1
    Join Date
    Mar 2009
    Posts
    29
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default HTTP request

    I have tried to make http request with GET method, but I couldn't obtain the result of it.
    My code:
    Qt Code:
    1. QHttp http;
    2. http.setHost("appmsg.gadu-gadu.pl");
    3. QHttpRequestHeader dataRequest("GET", QUrl::toPercentEncoding("/appsvc/appmsg4.asp?fmnumber=1247818&version=7,0,0,20&lastmsg=1"));
    4. dataRequest.setValue("Accept-Language", "pl");
    5. dataRequest.setValue("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98)");
    6. dataRequest.setValue("Pragma", "no-cache");
    7. dataRequest.setValue("Host", "appmsg.gadu-gadu.pl");
    8. http.request(dataRequest);
    9. QString dataResult = http.readAll();
    10. qDebug() << dataResult;
    To copy to clipboard, switch view to plain text mode 
    What's wrong in this code? Why can't I get the result of request?

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

    Default Re: HTTP request

    QHttp works in an asynchronous manner. You have to wait until the request result is ready.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Default Re: HTTP request

    Yes, if http.request worked in the way you have used it, you main program would hang until the result was received or an error occured, which is definitely not what you want.

    Use signals and slots.

  4. #4
    Join Date
    Mar 2009
    Posts
    29
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: HTTP request

    This is my improve code:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::MainWindow)
    3. {
    4. ui->setupUi(this);
    5. http = new QHttp("appmsg.gadu-gadu.pl");
    6. QHttpRequestHeader dataRequest("GET", QUrl::toPercentEncoding("/appsvc/appmsg4.asp?fmnumber=1247818&version=7,0,0,20&lastmsg=1"));
    7. dataRequest.setValue("Accept-Language", "pl");
    8. dataRequest.setValue("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98)");
    9. dataRequest.setValue("Pragma", "no-cache");
    10. dataRequest.setValue("Host", "appmsg.gadu-gadu.pl");
    11. http->request(dataRequest);
    12. QObject::connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(function(int, bool)));
    13. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void MainWindow::function(int id, bool error){
    2. qDebug() << "Test";
    3.  
    4. if(error){
    5. qDebug() << http->errorString();
    6. return;
    7. }
    8.  
    9. QString dataResult = http->readAll();
    10. qDebug() << dataResult;
    11. }
    To copy to clipboard, switch view to plain text mode 

    Output return:
    Qt Code:
    1. Starting C:/Documents and Settings/Trok/Pulpit/Qt Progs/Test_gg/debug/Test_gg.exe...
    2. NOD32 protected [MSAFD Tcpip [TCP/IP]]NOD32 protected [MSAFD Tcpip [UDP/IP]]NOD32 protected [MSAFD Tcpip [RAW/IP]]NOD32 protected [RSVP UDP Service Provider]NOD32 protected [RSVP TCP Service Provider]Test
    3. ""
    To copy to clipboard, switch view to plain text mode 
    What's wrong else?
    Last edited by Trok; 2nd January 2010 at 13:15.

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

    Default Re: HTTP request

    Really, connect should go before the request, but it seems like your firewall (NOD32?) might be interfering with your application

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

    Trok (2nd January 2010)

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

    Default Re: HTTP request

    Connect can go after the request. Qt assures the signal will not be delivered until the application re-enters the event loop. Anyway it seems the firewall is blocking the connection as suggested.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    Trok (2nd January 2010)

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

    Default Re: HTTP request

    Is that a general rule, wysota? I was thinking if maybe a signal was emitted in the request method (not the complete, but maybe an error signal) that Qt wouldn't actually emit or queue the signal as there was no listeners at the time it was emitted.

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

    Trok (2nd January 2010)

  11. #8
    Join Date
    Mar 2009
    Posts
    29
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: HTTP request

    I closed NOD32 and all the firewalls but I have still the same communique:
    Starting C:/Documents and Settings/Trok/Pulpit/Qt Progs/Test_gg/debug/Test_gg.exe...
    NOD32 protected [MSAFD Tcpip [TCP/IP]]NOD32 protected [MSAFD Tcpip [UDP/IP]]NOD32 protected [MSAFD Tcpip [RAW/IP]]NOD32 protected [RSVP UDP Service Provider]NOD32 protected [RSVP TCP Service Provider]Test
    ""
    I suppose that the firewall don't block this operation, because I can send POST request by SOCKET correctly.
    Last edited by wysota; 5th January 2010 at 15:08.

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

    Default Re: HTTP request

    Quote Originally Posted by fatjuicymole View Post
    Is that a general rule, wysota?
    With networking classes - yes.

    I was thinking if maybe a signal was emitted in the request method (not the complete, but maybe an error signal) that Qt wouldn't actually emit or queue the signal as there was no listeners at the time it was emitted.
    All "request" type of methods don't do anything. They just schedule execution.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. The following user says thank you to wysota for this useful post:

    Trok (2nd January 2010)

  14. #10
    Join Date
    Oct 2009
    Posts
    37
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: HTTP request

    Small side note: Use QNetworkAccessManager. QHttp is deprecated.
    Disclaimer: Although I work on Qt for Nokia, anything I post here is personal

  15. The following user says thank you to mgoetz for this useful post:

    waynew (21st March 2010)

Similar Threads

  1. HTTP GET authentication does not proceed
    By SailingDreams in forum Qt Programming
    Replies: 2
    Last Post: 1st June 2009, 01:27
  2. problem with Synchronous Http request
    By rchaitanya in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 30th January 2009, 11:03
  3. How do I send data with http post request?
    By Morea in forum Qt Programming
    Replies: 13
    Last Post: 21st January 2009, 22:51
  4. http request
    By yagabey in forum Qt Programming
    Replies: 3
    Last Post: 28th December 2008, 19:19
  5. uploading files to HTTP!!
    By Raajesh in forum Qt Programming
    Replies: 2
    Last Post: 24th June 2008, 22:00

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.