Qt: Use tcp socket to get google map image? -
for reason need use blocking call perform image accessing google's server. however, qnetworkaccessmanager seems async, though there many work arounds, calling eventloop.exec(); many people online suggested me not so.
so trying use tcp socekt. want access image here:
http://mt1.google.com/vt/lyrs=y&x=0&y=0&z=0
and here code:
socket = new qtcpsocket(this); socket->connecttohost("mt1.google.com", 80, qiodevice::readwrite); if(socket->waitforconnected(5000)) { qdebug() << "connected!"; // send socket->write("/vt/lyrs=y&x=0&y=0&z=0"); socket->waitforbyteswritten(1000); socket->waitforreadyread(3000); qdebug() << "reading: " << socket->bytesavailable(); // data qdebug() << socket->readall(); // close connection socket->close(); } else { qdebug() << "not connected!"; }
but seems working @ all? should write through tcp socket image?
tcp provides transport mechanism. since trying communicate web server, should compose http messages.
replace line
socket->write("/vt/lyrs=y&x=0&y=0&z=0");
with
socket->write("get /vt/lyrs=y&x=0&y=0&z=0 http/1.1\r\nhost: mt1.google.com\r\nuser-agent: testagent\r\n\r\n");
and should following response :
http/1.1 200 ok date: sun, 14 jun 2015 14:24:40 gmt expires: sun, 14 jun 2015 14:24:40 gmt cache-control: private, max-age=3600 access-control-allow-origin: * content-type: image/jpeg x-content-type-options: nosniff server: paintfe content-length: 10790 x-xss-protection: 1; mode=block x-frame-options: sameorigin alternate-protocol: 80:quic,p=0 imagedata
parse response , extract imagedata
part.
edit : tcp delivers response divided chunks. approach, not able receive whole response since trying receive in 1 go.
you should examine content-length
header , wait until receiveing specified amount of bytes.
Comments
Post a Comment