@squidge:
Thanks for the reply, I have modified my application, and now I use signals instead of blocking code:
socket.connectToHost("google.pl",80);
connect(&socket,SIGNAL(readyRead()),this,SLOT(read()));
connect(&socket,SIGNAL(connected()),this,SLOT(connectX()));
void MainWindow::connectX()
{
QUrl url
("http://www.google.pl/images/srpr/nav_logo73.png");
ui->statusBar->showMessage("CONNECTED");
socket.
write("GET "+ url.
toEncoded(QUrl::RemoveScheme |
QUrl::RemoveAuthority) +"\r\n");
}
void MainWindow::read()
{
ba+=socket.readAll();
}
{
ui
->statusBar
->showMessage
("Error! "+QString::number(socketError
));
}
socket.connectToHost("google.pl",80);
connect(&socket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(error(QAbstractSocket::SocketError)));
connect(&socket,SIGNAL(readyRead()),this,SLOT(read()));
connect(&socket,SIGNAL(connected()),this,SLOT(connectX()));
void MainWindow::connectX()
{
socket.setSocketOption(QAbstractSocket::KeepAliveOption,1);
QUrl url("http://www.google.pl/images/srpr/nav_logo73.png");
ui->statusBar->showMessage("CONNECTED");
socket.write("GET "+ url.toEncoded(QUrl::RemoveScheme | QUrl::RemoveAuthority) +"\r\n");
}
void MainWindow::read()
{
ba+=socket.readAll();
}
void MainWindow::error(QAbstractSocket::SocketError socketError)
{
ui->statusBar->showMessage("Error! "+QString::number(socketError));
}
To copy to clipboard, switch view to plain text mode
however weird thing is that with some links it works OK (like the google link mentioned in the first post), but with others I get "Bad request", also I can do:
socket.write("GET /images/srpr/nav_logo73.png\r\n");
socket.write("GET /images/srpr/nav_logo73.png\r\n");
To copy to clipboard, switch view to plain text mode
but not HEAD:
socket.write("HEAD /images/srpr/nav_logo73.png\r\n");
socket.write("HEAD /images/srpr/nav_logo73.png\r\n");
To copy to clipboard, switch view to plain text mode
which gives me "Bad request".
I am not a networking expert (obviously ;] ) but I think the problem is that while browsers and QNetworkAccesManager sends GET, HEAD and other HTTP requests that they are somehow identified (by wireshark for example) as HTTP, and my requests are identified as TCP (wireshark informs me that my requests are "TCP segment of a reassembled PDU", when I look inside I can see my request), please correct me if I am wrong.
How can I fix that?
Also, QNetworkAccessManager also emits the readyRead() signal whenever new data arrives.
But how can I read that data? I don't see any read function for QNetworkAccessManager that could be used here to read partial data, unless cache() can be used in that manner?
Thanks in advance.
EDIT:
OK now I see that I can use something like:
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkReply *reply;
reply
=manager
->get
(QNetworkRequest
(QUrl("http://www.google.pl/images/srpr/nav_logo73.png")));
connect(reply,SIGNAL(readyRead()),this,SLOT(read2()));
void MainWindow::read2()
{
ba+=reply->readAll();
temp.loadFromData(ba);
ui->display->setPixmap(temp);
}
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkReply *reply;
reply=manager->get(QNetworkRequest(QUrl("http://www.google.pl/images/srpr/nav_logo73.png")));
connect(reply,SIGNAL(readyRead()),this,SLOT(read2()));
void MainWindow::read2()
{
ba+=reply->readAll();
QPixmap temp;
temp.loadFromData(ba);
ui->display->setPixmap(temp);
}
To copy to clipboard, switch view to plain text mode
and it works however is something like the above safe to use?
Bookmarks