Results 1 to 5 of 5

Thread: Creating a HTTP connection

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Creating a HTTP connection

    Hello!

    I want to create a http connection to a webserver and start downloading data in a QThread, but I'm having some problems.

    First, my code is returning usually nothing from my searches:

    Qt Code:
    1. HTTPClass::HTTPClass(QObject *parent) :
    2. QObject(parent)
    3. {
    4. namConnection = new QNetworkAccessManager(this);
    5.  
    6. connect(namConnection, SIGNAL(finished(QNetworkReply*)),
    7. this, SLOT(slotReplyFinished(QNetworkReply*)));
    8. connect(namConnection, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
    9. this, SLOT(slotAuthenticationRequired(QNetworkReply*,QAuthenticator*)));
    10. connect(namConnection, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
    11. this, SLOT(slotProxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
    12. connect(namConnection, SIGNAL(networkSessionConnected()),
    13. this, SLOT(slotNetworkSessionConnected()));
    14.  
    15. QNetworkRequest request;
    16. request.setUrl(QUrl("http://en.wikipedia.org/wiki/Main_Page"));
    17.  
    18. reply = namConnection->get(request);
    19. connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
    20. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
    21. this, SLOT(slotError(QNetworkReply::NetworkError)));
    22. connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
    23. this, SLOT(slotSslErrors(QList<QSslError>)));
    24. }
    25.  
    26. void HTTPClass::setConnectionData()
    27. {
    28.  
    29. }
    30.  
    31. void HTTPClass::slotReplyFinished(QNetworkReply *reply)
    32. {
    33. qDebug() << reply->readAll();
    34.  
    35. reply->deleteLater();
    36. }
    37.  
    38. void HTTPClass::slotAuthenticationRequired(QNetworkReply *reply, QAuthenticator *aut)
    39. {
    40. qDebug() << "Ha";
    41. //aut->setUser("Username");
    42. //aut->setPassword("Password");
    43. }
    44.  
    45. void HTTPClass::slotProxyAuthenticationRequired(QNetworkProxy proxy, QAuthenticator *aut)
    46. {
    47.  
    48. }
    49.  
    50. void HTTPClass::slotNetworkSessionConnected()
    51. {
    52. qDebug() << "Connected";
    53. }
    54.  
    55. void HTTPClass::slotReadyRead()
    56. {
    57. reply->readAll();
    58. }
    59.  
    60. void HTTPClass::slotError(QNetworkReply::NetworkError error)
    61. {
    62. qDebug() << QString::number(error);
    63. }
    64.  
    65. void HTTPClass::slotSslErrors(QList<QSslError> errors)
    66. {
    67. qDebug() << errors[0].errorString();
    68. }
    To copy to clipboard, switch view to plain text mode 

    Everything I get is 3 blanck answers. ("" "" "") What am I doing wrong? I know how to download a file from the web, but not how to read data from a url.

    --
    My second question: actually I don't know how the information is in the link I actually want to connect with. It must be some kind of server, but not if its an sql server with MySQL or FTP something else. How could I know how the information is stored in the link I want to connect with? (which by the way: sharkbuss.banifinvest.com.br , gate 80, parameter: /BI3S2/get.aspx)
    --
    My third question: in order to connect myself with this link, I now I'll have to give a password and username. Where exactly in my code should I put this part?


    Thanks,

    Momergil

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a HTTP connection

    Quote Originally Posted by Momergil View Post
    Everything I get is 3 blanck answers. ("" "" "") What am I doing wrong? I know how to download a file from the web, but not how to read data from a url.
    QNetworkReply is a QIODevice subclass, so you use the QIODevice API to read data from the reply object.

    You can either do that as it comes in (your slotReadyRead() does that) or read at the end of the transmission (your slotReplyFinished() does that).

    For example if you wanted to download into a file, you would also open a file and write the data you get from the readAll() calls into it.
    You could also append it to a QByteArray member, write into a QBuffer object or process it right away.

    Quote Originally Posted by Momergil View Post
    My second question: actually I don't know how the information is in the link I actually want to connect with. It must be some kind of server, but not if its an sql server with MySQL or FTP something else. How could I know how the information is stored in the link I want to connect with? (which by the way: sharkbuss.banifinvest.com.br , gate 80, parameter: /BI3S2/get.aspx)
    I am not sure what you mean but the network reply will also have the HTTP headers sent by the server, e.g. the content MIME type.

    Quote Originally Posted by Momergil View Post
    My third question: in order to connect myself with this link, I now I'll have to give a password and username. Where exactly in my code should I put this part?
    That depends on how the service expects authentication data. It could be encoded into the URL, passed as part of headers in QNetworkRequest or provided on-demand in slotAuthenticationRequired().

    Cheers,
    _

  3. #3
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a HTTP connection

    I am not sure what you mean but the network reply will also have the HTTP headers sent by the server, e.g. the content MIME type.
    I mean that while I know the link, password, etc.. I have no idea just yet about how the information I want is stored on that server - it's no mine, it wasn't me who created it. So different from a SQL server where I would call a function to know all tables avaliable, and all headers of thoose tables, I just don't know how data is stored on that http server as well as I don't know how to know that

    But you mentioned about headers; what exactly is that?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a HTTP connection

    Quote Originally Posted by Momergil View Post
    I mean that while I know the link, password, etc.. I have no idea just yet about how the information I want is stored on that server - it's no mine, it wasn't me who created it.
    One usually never knows that, it is an implementation detail of the server.

    Like a web browser not caring at all how a web server creates its content. The browser sends a GET request for a given URL and the web server returns either an error code or success and the content.
    It is of no concern to the web browser if the content had been stored in a file, read from a database or created on the fly.

    If the server you are talking to is a web service, they'll have a description of their API. If it is just a normal web server, it will respond with the document your GET calls request.

    Quote Originally Posted by Momergil View Post
    But you mentioned about headers; what exactly is that?
    HTTP headers. Meta data used in communication between an HTTP client and an HTTP server and vice versa.
    Things like HTTP version, supported compression types, content MIME type, etc.

    Qt has support for sending those in QNetworkRequest and for retrieving them in QNetworkReply

    Cheers,
    _

  5. #5
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a HTTP connection

    Ok, thanks!

    Since I'm not sure anymore if I will need to do this HTTP connection, I will stop my questions here.

    Thanks,

    Happy new year,

    Momergil

Similar Threads

  1. Replies: 0
    Last Post: 11th November 2011, 19:18
  2. Http connection in Qt
    By sabbu in forum Newbie
    Replies: 4
    Last Post: 27th May 2011, 11:54
  3. Replies: 1
    Last Post: 2nd April 2010, 06:42
  4. XML http?
    By Mohd.Imran in forum Newbie
    Replies: 2
    Last Post: 23rd June 2009, 10:11
  5. http connection
    By bhogasena in forum Qt Programming
    Replies: 12
    Last Post: 25th February 2009, 11:32

Tags for this Thread

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.