Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED][QHttp] qhttp->get("/index.html?text=AAA",file) Doesn't work correctly
Forum Updated to NodeBB v4.3 + New Features

[SOLVED][QHttp] qhttp->get("/index.html?text=AAA",file) Doesn't work correctly

Scheduled Pinned Locked Moved General and Desktop
15 Posts 6 Posters 10.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jaszczomb
    wrote on last edited by
    #1

    Hello everyone
    I want to write simple program that would collect links from one website I've found. I decided to use QHttp as the simplest way to make it work. From what I've already wrote I've got program that downloads index.html file, but I need to send GET data to collect various links, depending on program input. Here's my code:
    @
    file = new QFile("local.html", this);
    http = new QHttp(this);
    file->open(QIODevice::ReadWrite);
    http->setHost("website.info");
    http->get("/index.html?text=some+text", file);
    QTextStream data(file);
    text = data.readAll();
    file->close();
    QDebug()<<"File size = "<<plik->size()<<"\n";
    QDebug()<<text;
    @
    Program compiles without any errors and using QDebug i can see that file has some size (so it's created and page was downloaded), but when i look at the content of 'text' variable i can see it is /index.html itself but not formated and without data which /index.html?text=some+text should have.
    Is there any special way i should send GET request?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on last edited by
      #2

      "QHttp":http://doc.qt.nokia.com/4.7/qhttp.html
      [quote]This class is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.[/quote]
      Use QNetworkAccessManager instead! However, your problem might be that you don't use "QUrl::toPercentEncoding":http://doc.qt.nokia.com/4.7/qurl.html#toPercentEncoding .

      1 Reply Last reply
      0
      • L Offline
        L Offline
        LinusA
        wrote on last edited by
        #3

        Since the docs say QHttp is deprecated, and without testing:
        Can't you use the QNetworkAccessManager?
        Like so:
        @
        QUrl myURL(QString("http://website.info/index.html"));
        myURL.addQueryItem(QString("text"), QString("some+text"));
        QNetworkRequest myRequest(myURL);

        QNetworkAccessManager myManager(this);
        QNetworkReply * pMyReply = myManager.get(myRequest);

        // now use pMyReply to get the data, which is a subclass of IODevice!
        // don't know if the device is already open, if not, use ->open()
        // then read
        QByteArray byteData = pMyReply->readAll();
        // and convert to string!
        // be careful, take care of encoding here:
        QString stringData(byteData);

        // and clean up
        delete pMyReply;
        @

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jaszczomb
          wrote on last edited by
          #4

          Thank you for quick answer. I'm already trying to use this suggestions in my code, I'll write when I'll finish.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jaszczomb
            wrote on last edited by
            #5

            I've got a problem in Your code LinusA. I tried to initialize QNetworkRequest myRequest(myURL); I've got "Variable 'QNetworkRequest myReques' has initializer but incompatible type". What could I do wrong? To be sure if everything is as you said I've copied Your code.

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              Possible cause of the problem may be that ::get() call is asynchronous, so you should wait for requestFinished() signal to be emitted before reading. Also, I assume that "text" variable is a QString? And, in first QDebug() you should probably use "file->size()" instead of "plik->size()".

              And indeed, QHttp is deprecated, try QNAM, it's fun! :D

              (Z(:^

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jaszczomb
                wrote on last edited by
                #7

                That's not where problem is because /index.html without GET is being downloaded correctly. Only the one with GET (/index.html?text=some+text) causes problems.

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  LinusA
                  wrote on last edited by
                  #8

                  [quote author="jaszczomb" date="1312299549"]I've got a problem in Your code LinusA. I tried to initialize QNetworkRequest myRequest(myURL); I've got "Variable 'QNetworkRequest myReques' has initializer but incompatible type". What could I do wrong? To be sure if everything is as you said I've copied Your code.[/quote]
                  Hm, since "Variable 'QNetworkRequest myReques' has initializer but incompatible type" looks like a typo (myReques vs myRequest), could you please post your code and the exact error message with line number, please?

                  As I said, I didn't even compile this code, just gave a rough idea how to use QNetworkAccessManager. It's all in the docs.

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jaszczomb
                    wrote on last edited by
                    #9

                    @ ui->setupUi(this);
                    connect(ui->przycisk,SIGNAL(clicked()),this,SLOT(pobieranie()));
                    QUrl url(QString("http://website.info/index.html"));
                    url.addQueryItem(QString("text"), QString("some+text"));
                    QNetworkRequest request(url); //12 line in my cpp file
                    QNetworkAccessManager manager(this);
                    QNetworkReply * pReply = manager.get(request);
                    QByteArray byteData = pReply->readAll();
                    QString stringData(byteData);@
                    Message: "12: error:variable ‘QNetworkRequest request’ has initializer but incomplete type"
                    That's all in constructor of an application. Until it's not working I won't write anything more because next step is to find links from the document with .indexOf (that's why I'm converting site to QString).
                    I'll search the Docs to find an answer but any of your suggestions is very helpful.

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      dangelog
                      wrote on last edited by
                      #10

                      You need an #include.

                      Software Engineer
                      KDAB (UK) Ltd., a KDAB Group company

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        jaszczomb
                        wrote on last edited by
                        #11

                        @#include <QMainWindow>
                        #include <QUrl>
                        #include <QFile>
                        #include <QTextStream>
                        #include <QDebug>
                        #include <QNetworkAccessManager>@
                        Here is my Include section. Should I include something more?

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          jaszczomb
                          wrote on last edited by
                          #12

                          Peppe you were right, it's because I had to place one more include.
                          Now i have one more problem with QByteArray byteData = pReply->readAll() but I hope I can make it on my own since now. Thank you everyone :]

                          1 Reply Last reply
                          0
                          • L Offline
                            L Offline
                            LinusA
                            wrote on last edited by
                            #13

                            [quote author="jaszczomb" date="1312309394"]
                            Now i have one more problem with QByteArray byteData = pReply->readAll() but I hope I can make it on my own since now. [/quote]
                            I could imagine you might have to call
                            @
                            if (!pReply->open(QIODevice::ReadOnly)) {
                            // Error: something went wrong,
                            }
                            // you can also check pReply->isOpen() now
                            pReply->readAll();
                            @
                            See docs of QIODevice.

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              mlong
                              wrote on last edited by
                              #14

                              Be sure and take note of sierdzio's post above. If you call readAll() immediately after the get(), then there more than likely won't be anything to read yet.

                              You probably want to look at either "QNetworkRequest::finished()":http://doc.qt.nokia.com/4.7/qnetworkreply.html#finished or "QNetworkAccessManager::finished()":http://doc.qt.nokia.com/4.7/qnetworkaccessmanager.html#finished

                              Software Engineer
                              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                              1 Reply Last reply
                              0
                              • sierdzioS Offline
                                sierdzioS Offline
                                sierdzio
                                Moderators
                                wrote on last edited by
                                #15

                                Just a quick additional note on my last post - if you don't want to make your class asynchronous, too, you can wait for the signal right in your constructor, with something like this:

                                @forever
                                {
                                if (/* Check for reply here /)
                                {
                                /
                                Do reading here */
                                }
                                else
                                {
                                // This ensures that your application will respond to events while waiting for the reply
                                qApp->processEvents();
                                }
                                }
                                @
                                Although doing it in the constructor might be dangerous. Also, it will wait for an answer indefinitely... be careful :)

                                (Z(:^

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved