Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, June 2007
problem with QHttp::request

Pages: Prev | 1 | 2 | Next

Message 1 in thread

Hi! I'm having the following problem using QHttp::request:


My debug message:


Started processing: 4
Could not read enough bytes from the device
Operation: 4 finished with success


The first message, refers to a requestStarted( int ), the second one, I 
believe comes from the QBuffer, which is where lies the problem. The 
last message comes from requestFinished( int, bool ).



The snip of the code is:

http->request(header, webpage, to);

webpage is initialized on the constructor of this class with:

webpage = new QBuffer(this);
webpage->open(QBuffer::ReadWrite);

and to with:

to = new QBuffer(this);
to->open(QBuffer::ReadWrite);
to->write("1");



What am I doing wrong? Why is the buffer closing before receiving all 
the data?



Thanks


--
 [ signature omitted ] 

Message 2 in thread

*bump*

André Lemos wrote:
> Hi! I'm having the following problem using QHttp::request:
>
>
> My debug message:
>
>
> Started processing: 4
> Could not read enough bytes from the device
> Operation: 4 finished with success
>
>
> The first message, refers to a requestStarted( int ), the second one, 
> I believe comes from the QBuffer, which is where lies the problem. The 
> last message comes from requestFinished( int, bool ).
>
>
>
> The snip of the code is:
>
> http->request(header, webpage, to);
>
> webpage is initialized on the constructor of this class with:
>
> webpage = new QBuffer(this);
> webpage->open(QBuffer::ReadWrite);
>
> and to with:
>
> to = new QBuffer(this);
> to->open(QBuffer::ReadWrite);
> to->write("1");
>
>
>
> What am I doing wrong? Why is the buffer closing before receiving all 
> the data?
>
>
>
> Thanks

--
 [ signature omitted ] 

Message 3 in thread

Hi,

>> The snip of the code is:
>> [...]
>> What am I doing wrong? Why is the buffer closing before receiving all 
>> the data?

Could you maybe provide a minimal but complete program that reproduces the 
problem?

--
 [ signature omitted ] 

Message 4 in thread

Dimitri wrote:
> Hi,
>
>>> The snip of the code is:
>>> [...]
>>> What am I doing wrong? Why is the buffer closing before receiving 
>>> all the data?
>
> Could you maybe provide a minimal but complete program that reproduces 
> the problem?

Oddly enough, as I was stripping away my current program to provide with 
a minimal example, I've come across another error, where http->error() 
yields 1.


So... When myRequestFinished() is called, I'm getting 1 (which is 
Unknown Error). And yes... I can open google.


#include <QtNetwork/QHttp>
#include <QtCore/QIODevice>
#include <QtCore/QBuffer>
#include <iostream>

#include "http.h"

using namespace std;

Http::Http( )
{

        http = new QHttp(this);

        webpage = new QBuffer(this);
        to = new QBuffer(this);

        webpage->open(QIODevice::ReadWrite);
        to->open(QIODevice::WriteOnly);

        connect(http, SIGNAL(requestFinished( int, bool )), this, 
SLOT(myRequestFinished(int, bool)));

        hitMe();

}


void Http::hitMe()
{

        http->get("www.google.com", webpage);


}


void Http::myRequestFinished(int id, bool result)
{
        if (result) {

                cout << "OH NOESSSSSSSSSSSSSSSSSSSSSSSS" << 
http->error() << endl;

        }

}

--
 [ signature omitted ] 

Message 5 in thread

Hi,

> Oddly enough, as I was stripping away my current program to provide with 
> a minimal example, I've come across another error, where http->error() 
> yields 1.

I cannot reproduce that. See below.

> So... When myRequestFinished() is called, I'm getting 1 (which is 
> Unknown Error). And yes... I can open google.

This is not a complete example since it lacks a http.h and main(). Anyway, I 
had a look at the code and noticed:

> [...]
>        http->get("www.google.com", webpage);
> [...]

The documentation of QHttp::get() reads:
	Sends a get request for path to the server set by setHost()
	or as specified in the constructor.
	path must be an absolute path like /index.html or an absolute
	URI like http://www.trolltech.com/index.html.

Change your code to:
        http->setHost("www.google.com");
        http->get("http://www.google.com/";, webpage);

--
 [ signature omitted ] 

Message 6 in thread

Dimitri wrote:
> Hi,
>
>> Oddly enough, as I was stripping away my current program to provide 
>> with a minimal example, I've come across another error, where 
>> http->error() yields 1.
>
> I cannot reproduce that. See below.
>
>> So... When myRequestFinished() is called, I'm getting 1 (which is 
>> Unknown Error). And yes... I can open google.
>
> This is not a complete example since it lacks a http.h and main(). 
> Anyway, I had a look at the code and noticed:
>
>> [...]
>>        http->get("www.google.com", webpage);
>> [...]
>
> The documentation of QHttp::get() reads:
>     Sends a get request for path to the server set by setHost()
>     or as specified in the constructor.
>     path must be an absolute path like /index.html or an absolute
>     URI like http://www.trolltech.com/index.html.
>
> Change your code to:
>        http->setHost("www.google.com");
>        http->get("http://www.google.com/";, webpage);


That did fix the problem. But the real problem I am having is with 
QHttp::request().


void Http::hitMe()
{

        http->setHost("www.google.com");

        header.setValue("Host", "http://www.google.com:80";);
        header.setValue("Host", host+":80");

        http->request(header, webpage);


}



This gives me: "Could not read enough bytes from the device".

I want to request a crafted header and store the result on "webpage", 
that is a QBuffer.

Isn't this the right way of doing things?



Thanks

--
 [ signature omitted ] 

Message 7 in thread

André Lemos wrote:
> Dimitri wrote:
>> Hi,
>>
>>> Oddly enough, as I was stripping away my current program to provide 
>>> with a minimal example, I've come across another error, where 
>>> http->error() yields 1.
>>
>> I cannot reproduce that. See below.
>>
>>> So... When myRequestFinished() is called, I'm getting 1 (which is 
>>> Unknown Error). And yes... I can open google.
>>
>> This is not a complete example since it lacks a http.h and main(). 
>> Anyway, I had a look at the code and noticed:
>>
>>> [...]
>>>        http->get("www.google.com", webpage);
>>> [...]
>>
>> The documentation of QHttp::get() reads:
>>     Sends a get request for path to the server set by setHost()
>>     or as specified in the constructor.
>>     path must be an absolute path like /index.html or an absolute
>>     URI like http://www.trolltech.com/index.html.
>>
>> Change your code to:
>>        http->setHost("www.google.com");
>>        http->get("http://www.google.com/";, webpage);
>
>
> That did fix the problem. But the real problem I am having is with 
> QHttp::request().
>
>
> void Http::hitMe()
> {
>
>        http->setHost("www.google.com");
>
>        header.setValue("Host", "http://www.google.com:80";);

typo in here. Only this line.

>
>        http->request(header, webpage);
>
>
> }
>
>
>
> This gives me: "Could not read enough bytes from the device".
>
> I want to request a crafted header and store the result on "webpage", 
> that is a QBuffer.
>
> Isn't this the right way of doing things?
>
>
>
> Thanks 

--
 [ signature omitted ] 

Message 8 in thread

André Lemos wrote:
> Dimitri wrote:
>> Hi,
>>
>>> Oddly enough, as I was stripping away my current program to provide 
>>> with a minimal example, I've come across another error, where 
>>> http->error() yields 1.
>>
>> I cannot reproduce that. See below.
>>
>>> So... When myRequestFinished() is called, I'm getting 1 (which is 
>>> Unknown Error). And yes... I can open google.
>>
>> This is not a complete example since it lacks a http.h and main(). 
>> Anyway, I had a look at the code and noticed:
>>
>>> [...]
>>>        http->get("www.google.com", webpage);
>>> [...]
>>
>> The documentation of QHttp::get() reads:
>>     Sends a get request for path to the server set by setHost()
>>     or as specified in the constructor.
>>     path must be an absolute path like /index.html or an absolute
>>     URI like http://www.trolltech.com/index.html.
>>
>> Change your code to:
>>        http->setHost("www.google.com");
>>        http->get("http://www.google.com/";, webpage);
>
>
> That did fix the problem. But the real problem I am having is with 
> QHttp::request().
>
>
> void Http::hitMe()
> {
>
>        http->setHost("www.google.com");
>
>        header.setValue("Host", "http://www.google.com:80";);

typo in here. Only this line.

>
>        http->request(header, webpage);
>
>
> }
>
>
>
> This gives me: "Could not read enough bytes from the device".
>
> I want to request a crafted header and store the result on "webpage", 
> that is a QBuffer.
>
> Isn't this the right way of doing things?
>
>
>
> Thanks 

--
 [ signature omitted ] 

Message 9 in thread

Hi,

> [...]
> That did fix the problem. But the real problem I am having is with 
> QHttp::request().
> [...]

Maybe "header" is not properly initialized, or something like that.

Could you provide a compilable, minimal, but complete example? Try modifying 
the attached example which works fine for me.

--
 [ signature omitted ] 
class QHttp;
class QBuffer;

#include <QObject>

class Http : public QObject {
    Q_OBJECT
public:
    Http();
public slots:
    void done(int id, bool result);
private:
    QHttp *http;
    QBuffer *buffer;
};

#include <QHttp>
#include <QBuffer>
#include <QtDebug>

Http::Http() {
    http = new QHttp(this);
    buffer = new QBuffer(this);
    buffer->open(QIODevice::ReadWrite);
    connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(done(int, bool)));
    http->setHost("www.google.com");
    http->get("http://www.google.com/";, buffer);
}

void Http::done(int id, bool result) {
    if (result)
        qDebug() << "Error!  " << http->errorString();
    else
        qDebug() << "Success:" << http->errorString();
} 

#include <QApplication>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    Http http;
    return app.exec();
}

Message 10 in thread

Dimitri wrote:
> Hi,
>
>> [...]
>> That did fix the problem. But the real problem I am having is with 
>> QHttp::request().
>> [...]
>
> Maybe "header" is not properly initialized, or something like that.
>
> Could you provide a compilable, minimal, but complete example? Try 
> modifying the attached example which works fine for me.
>
> -- 
> Dimitri
>

it would be something like the attached files, that give me:

Success: "Unknown error"
Could not read enough bytes from the device
Success: "Unknown error"
//class QHttp;
//class QHttp;
class QBuffer;

#include <QObject>
#include <QtNetwork/QHttpRequestHeader>

#include <http.h>
/*
class Http : public QObject {
    Q_OBJECT
public:
    Http();
public slots:
    void done(int id, bool result);
private:
    QHttp *http;
    QBuffer *buffer;
};
*/
#include <QtNetwork/QHttp>
#include <QBuffer>
#include <QtDebug>

Http::Http() {
    http = new QHttp(this);
    buffer = new QBuffer(this);
    buffer->open(QIODevice::ReadWrite);
    connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(done(int, bool)));
    http->setHost("www.google.com");

    QHttpRequestHeader header("GET", "/");
    header.setValue("Host", "www.google.com:80");

    http->request(header, buffer);
}

void Http::done(int id, bool result) {
    if (result)
        qDebug() << "Error!  " << http->errorString();
    else
        qDebug() << "Success:" << http->errorString();
} 

#include <QApplication>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    Http http;
    return app.exec();
}
class QHttp;
class QHttp;
class QBuffer;

#include <QObject>

class Http : public QObject {
    Q_OBJECT
public:
    Http();

public slots:
    void done(int id, bool result);

private:
    QHttp *http;
    QBuffer *buffer;
};


Message 11 in thread

Hi,

>     http->request(header, buffer);

Try changing the above to:
	http->request(header);
seems to be fixing the problem.

--
 [ signature omitted ] 

Message 12 in thread

Dimitri wrote:
> Hi,
>
>>     http->request(header, buffer);
>
> Try changing the above to:
>     http->request(header);
> seems to be fixing the problem.
>
Well yeah....

But then where would the reply from that request be stored? That's my 
problem.


--
 [ signature omitted ] 

Message 13 in thread

Hi,

> Well yeah....
> 
> But then where would the reply from that request be stored? That's my 
> problem.

The documentation for QHttp::request()
	http://doc.trolltech.com/4.3/qhttp.html#request
reads:
	int QHttp::request ( const QHttpRequestHeader & header,
	                     QIODevice * data = 0,
	                     QIODevice * to = 0 )
	Sends a request to the server set by setHost() or as specified
	in the constructor. Uses the "header" as the HTTP request header.
	You are responsible for setting up a header that is appropriate
	for your request.
	The incoming data comes via the "data" IO device.
	[...]

The first argument to Qhttp::request() does not look like it stores the reply 
of the request.

--
 [ signature omitted ] 

Message 14 in thread

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Dimitri wrote:
<blockquote cite="mid:f4uf8b$lcc$1@xxxxxxxxxxxxxxxxxx"; type="cite">Hi,
  <br>
  <br>
  <blockquote type="cite">Well yeah....
    <br>
    <br>
But then where would the reply from that request be stored? That's my
problem.
    <br>
  </blockquote>
  <br>
The documentation for QHttp::request()
  <br>
&nbsp;&nbsp;&nbsp;&nbsp;<a class="moz-txt-link-freetext" href="http://doc.trolltech.com/4.3/qhttp.html#request";>http://doc.trolltech.com/4.3/qhttp.html#request</a>
  <br>
reads:
  <br>
&nbsp;&nbsp;&nbsp;&nbsp;int QHttp::request ( const QHttpRequestHeader &amp; header,
  <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; QIODevice * data = 0,
  <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; QIODevice * to = 0 )
  <br>
&nbsp;&nbsp;&nbsp;&nbsp;Sends a request to the server set by setHost() or as specified
  <br>
&nbsp;&nbsp;&nbsp;&nbsp;in the constructor. Uses the "header" as the HTTP request header.
  <br>
&nbsp;&nbsp;&nbsp;&nbsp;You are responsible for setting up a header that is appropriate
  <br>
&nbsp;&nbsp;&nbsp;&nbsp;for your request.
  <br>
&nbsp;&nbsp;&nbsp;&nbsp;The incoming data comes via the "data" IO device.
  <br>
&nbsp;&nbsp;&nbsp;&nbsp;[...]
  <br>
  <br>
The first argument to Qhttp::request() does not look like it stores the
reply of the request.
  <br>
</blockquote>
<br>
it sure doesn't! But the second one does.... Which is what I was trying
to do.<br>
<br>
<b>"The incoming data comes via the "data" IO device."<br>
<br>
</b>Anyways, I'm going to try Sam Duttons' sugestion.<br>
<br>
<br>
</body>
</html>

--
 [ signature omitted ] 

Message 15 in thread

Hi,

>> The first argument to Qhttp::request() does not look like it stores 
>> the reply of the request.
> 
> it sure doesn't! But the second one does.... Which is what I was trying 
> to do.

Sorry, I meant:
The second argument to Qhttp::request() does not look like it stores the reply 
of the request. What makes you think it does?

--
 [ signature omitted ] 

Pages: Prev | 1 | 2 | Next