PDA

View Full Version : No output in HTTP request .. Help Required



divanshu
28th June 2011, 07:31
Hi,

I am trying to make a HTTP request. But when I am trying to bytearray output is null.

public class test {
private int httpGetId;
private boolean httpRequestAborted;
private QHttp qHttp;
public test(){
String url = "http://miniorb.in";

QUrl qURL = new QUrl(url);
System.out.println(qURL.encodedHost());
QHttpRequestHeader header = new QHttpRequestHeader("GET", "/index.html");
header.setValue("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98)");
header.setValue("Pragma", "no-cache");
header.setValue("Host","miniorb.in" );

qHttp = new QHttp();
qHttp.setHost("minirob.in");
qHttp.request(header);

httpGetId = qHttp.get(qURL.path());
qHttp.requestFinished.connect(this, "httpRequestFinished(int,boolean)");
qHttp.responseHeaderReceived.connect(this,"readResponseHeader(QHttpResponseHeader)");

QByteArray xyz = qHttp.readAll();
System.out.println("xyz : " + xyz.size());
System.out.println("print 1: " + qHttp.request(header));
String str = "Divanshu On work";
System.out.println("inside the finsihed()" + str);
}
private void httpRequestFinished(int requestId, boolean error)
{
System.out.println("httpRequestFinished");
if (requestId != httpGetId){
System.out.println("Error");
return;
}

}
private void readResponseHeader(QHttpResponseHeader responseHeader)
{
if (responseHeader.statusCode() != 200) {
httpRequestAborted = true;
qHttp.abort();
}
}
public static void main(String args[]){
test window = new test();
}
}

please help me .. where I am doing wrong.

Thanks in Advance !!

mvuori
28th June 2011, 13:33
What I think you are doing wrong is assuming that the code waits for the request to be processed. It, doesn't and your test() function should end in .request() call.

When the request has been processed, Qt calls your httpRequestFinished() callback function. That is where youd should check what you got.

See: http://doc.qt.nokia.com/latest/qhttp.html#request

squidge
28th June 2011, 16:11
Also, QHttp is obsolete and provided for old code only. It should not be used for new code. Use QNetwork* instead.