PDA

View Full Version : QHttp never end



didcea
16th July 2009, 07:46
Hi,

I tried to performe a simple request with QHtpp object. Everything works well and my code pass correctly by the both slots: "done" and "requestFinished".
But when the QUrl adress is not valid or correct, QHttp doesn't pass througth any slot. So, How I can know if the request failed ? It's like the request never endding !
Can you help me to solve this problem, thank.




QHtpp *http;
QFile *page_html;
...
..
QUrl adresse("http://forum.qtfr.org");
page_html = new QFile(windowTitle()+"_page.html");
page_html->setPermissions(QFile::ReadUser | QFile::WriteUser | QFile::ReadOther | QFile::WriteOther) ;
http = new QHttp(adresse.host(),80);
if (http!=NULL) {
connect(http, SIGNAL(done(bool)), this, SLOT(http_done(bool)));
connect(http, SIGNAL(requestFinished(int ,bool )), this, SLOT(http_requestFinished(int, bool)));
http->get(adresse.toString(),page_html);
}

yogeshgokul
16th July 2009, 08:06
You can try:
QUrl::isValid ().

And you should better connect dataReadProgress signal of QHttp. to see the progress..
Try also connecting requestStarted signal.

didcea
16th July 2009, 08:29
Thank, but this command doesn't solve my problem. It just test if Url is valid no if the Url exists or not. It not able to indicate for example if the host is off. In these cases my problem is the same.

yogeshgokul
16th July 2009, 08:42
Then you have to set some time out value for http command.
I dont know how to do that with QHttp. But QTCPSocket is great way to do that.
And you can do all things with QTcpSocket, whatever you can do with QHttp. Because ultimately the socket is in charge of everything.

Example:


QTcpSocket sock;
sock.connectToHost(host,80);
if(!sock.waitForConnected()) // HERE YOU CAN SET THE TIME OUT VALUE
{
errorString = sock.errorString();
return false;
}

didcea
16th July 2009, 08:52
Ok, but if I replace Host by my adress (for example "http://forum.qtfr.org") that doesn't work !
And finally, It's a ways to bypass the problem. I would find the reason frozen my QHttp object. It's not a normal behaviour

yogeshgokul
16th July 2009, 09:02
Ok, but if I replace Host by my adress (for example "http://forum.qtfr.org") that doesn't work !

What happens, it connects or not?
See if you can ping this host with your machine's command prompt successfully, then you can connect it via QTcpSocket too.


And finally, It's a ways to bypass the problem.
No, this is fastest way to check that, any URL is working or not.
This code also checks, that your machine is properly connected to internet or not. May be you require the proxy settings, if you are connected via, proxy server.


I would find the reason frozen my QHttp object. It's not a normal behaviour
Qt, says QHttp works Asynchronously, means never blocks. So ideally it should not block anywhere. But its great if you find out the reason in your case.

didcea
16th July 2009, 09:39
Socket connection did not works when QHttp request works correctly !

Your're rigth, QThhp is asynchone, but I don't anderstand wiht the done slop(bool error) is not call with error flag set to 1, when the connection failed.

yogeshgokul
16th July 2009, 10:42
Socket connection did not works when QHttp request works correctly !
Can you explain this?



Your're rigth, QThhp is asynchone, but I don't anderstand wiht the done slop(bool error) is not call with error flag set to 1, when the connection failed.

You can find out the problem area by checking the state using
signal stateChanged.
This signal emits with current state. And I am sure, you will get enough information about pin pointing the probblematic area.

didcea
16th July 2009, 11:14
I mean, I test valid Url with QHttp which works fine, but this same Url ddin't work with socket.

I test stateChange slot and when the Url is not valid, the state change from "Connecting" to "Closing" but never become "Unconnected". When Url is valid state finishes in "Unconnected"

yogeshgokul
16th July 2009, 11:30
I mean, I test valid Url with QHttp which works fine, but this same Url ddin't work with socket.
I dont think so, because QHttp uses QTcpSocket internally.
Let me assure you, We can send every HTTP command using a TCPSocket. Having socket in hands gives your more power and control.



I test stateChange slot and when the Url is not valid, the state change from "Connecting" to "Closing" but never become "Unconnected". When Url is valid state finishes in "Unconnected"

This is strange. I donr know how we can control this. But you can work around like this.
1. Check for URL is valid using Socket.
2. Get the URL using HTTP or Socket, whatever easier for you.

didcea
16th July 2009, 12:19
Ok, thank, I will explore socket solution