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



  • 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?



  • "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 .



  • 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;
    @



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



  • 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.


  • Moderators

    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



  • 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.



  • [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.



  • @ 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.



  • You need an #include.



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



  • 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 :]



  • [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.


  • Moderators

    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


  • Moderators

    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 :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.