Qt-jambi-interest Archive, February 2007
How to fetch data from a HTTP url?
Message 1 in thread
I am trying to fetch data from an HTTP URL. So far I have gathered that
to do this, I should use QUrl and QHttp to set up the connection, and
dump the data to a QIODevice or QByteArray. But with my simple test, it
seems that nothing really happens, and watching netstat, I can see that
no connection is made to the http host..
A snipplet of my test-code follows, I welcome pointers to info (rtfm
style), corrections to my code as well as flaming because I have
misunderstood the concept in general..
/* SNIP */
QUrl url = new QUrl();
QHttp http = new QHttp();
QBuffer buf = new QBuffer();
http.setParent(this);
buf.setParent(this);
url.setUrl("http://defcon.no/files/test.txt");
http.setHost(url.host());
http.get(url.path(), buf);
System.out.println(buf.buffer().toString());
/* SNIP */
In the test-snipplet, I try to see if any data is transferred, by using
QBuffer.buffer().toString() which should return a String representation
of the QByteArray contained within the QBuffer, and giving that to
System.out.println(). All this gives me is an empty line..
Thanks...
--
[ signature omitted ]
Message 2 in thread
Jon Langseth wrote:
> http.get(url.path(), buf);
>
> System.out.println(buf.buffer().toString());
Hi Jon,
The call to get() is not synchronous, so you there will not be any
content in the buffer until the request has executed and terminated. See
http://doc.trolltech.com/qtjambi-1.0/com/trolltech/qt/network/QHttp.html#get(java.lang.String,%20com.trolltech.qt.core.QIODevice)
for details.
ps: The JavaDoc formatting there is interresting to say the least ;-)