PDA

View Full Version : HTTP request



Trok
1st January 2010, 18:37
I have tried to make http request with GET method, but I couldn't obtain the result of it.
My code:


QHttp http;
http.setHost("appmsg.gadu-gadu.pl");
QHttpRequestHeader dataRequest("GET", QUrl::toPercentEncoding("/appsvc/appmsg4.asp?fmnumber=1247818&version=7,0,0,20&lastmsg=1"));
dataRequest.setValue("Accept-Language", "pl");
dataRequest.setValue("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98)");
dataRequest.setValue("Pragma", "no-cache");
dataRequest.setValue("Host", "appmsg.gadu-gadu.pl");
http.request(dataRequest);
QString dataResult = http.readAll();
qDebug() << dataResult;

What's wrong in this code? Why can't I get the result of request?

wysota
1st January 2010, 18:43
QHttp works in an asynchronous manner. You have to wait until the request result is ready.

squidge
1st January 2010, 19:48
Yes, if http.request worked in the way you have used it, you main program would hang until the result was received or an error occured, which is definitely not what you want.

Use signals and slots.

Trok
2nd January 2010, 14:09
This is my improve code:

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
http = new QHttp("appmsg.gadu-gadu.pl");
QHttpRequestHeader dataRequest("GET", QUrl::toPercentEncoding("/appsvc/appmsg4.asp?fmnumber=1247818&version=7,0,0,20&lastmsg=1"));
dataRequest.setValue("Accept-Language", "pl");
dataRequest.setValue("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98)");
dataRequest.setValue("Pragma", "no-cache");
dataRequest.setValue("Host", "appmsg.gadu-gadu.pl");
http->request(dataRequest);
QObject::connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(function(int, bool)));
}

void MainWindow::function(int id, bool error){
qDebug() << "Test";

if(error){
qDebug() << http->errorString();
return;
}

QString dataResult = http->readAll();
qDebug() << dataResult;
}

Output return:


Starting C:/Documents and Settings/Trok/Pulpit/Qt Progs/Test_gg/debug/Test_gg.exe...
NOD32 protected [MSAFD Tcpip [TCP/IP]]NOD32 protected [MSAFD Tcpip [UDP/IP]]NOD32 protected [MSAFD Tcpip [RAW/IP]]NOD32 protected [RSVP UDP Service Provider]NOD32 protected [RSVP TCP Service Provider]Test
""

What's wrong else?

squidge
2nd January 2010, 14:37
Really, connect should go before the request, but it seems like your firewall (NOD32?) might be interfering with your application

wysota
2nd January 2010, 16:33
Connect can go after the request. Qt assures the signal will not be delivered until the application re-enters the event loop. Anyway it seems the firewall is blocking the connection as suggested.

squidge
2nd January 2010, 17:44
Is that a general rule, wysota? I was thinking if maybe a signal was emitted in the request method (not the complete, but maybe an error signal) that Qt wouldn't actually emit or queue the signal as there was no listeners at the time it was emitted.

Trok
2nd January 2010, 18:31
I closed NOD32 and all the firewalls but I have still the same communique:


Starting C:/Documents and Settings/Trok/Pulpit/Qt Progs/Test_gg/debug/Test_gg.exe...
NOD32 protected [MSAFD Tcpip [TCP/IP]]NOD32 protected [MSAFD Tcpip [UDP/IP]]NOD32 protected [MSAFD Tcpip [RAW/IP]]NOD32 protected [RSVP UDP Service Provider]NOD32 protected [RSVP TCP Service Provider]Test
""

I suppose that the firewall don't block this operation, because I can send POST request by SOCKET correctly.:confused:

wysota
2nd January 2010, 19:15
Is that a general rule, wysota?
With networking classes - yes.


I was thinking if maybe a signal was emitted in the request method (not the complete, but maybe an error signal) that Qt wouldn't actually emit or queue the signal as there was no listeners at the time it was emitted.

All "request" type of methods don't do anything. They just schedule execution.

mgoetz
5th January 2010, 15:49
Small side note: Use QNetworkAccessManager. QHttp is deprecated.