HTTPS request returns damaged HTML body
Hi there,
I recently wanted to write a tool exploring the data which is requestable in facebook's Graph API. However, I created a tool performing a minimal request of the user's id and name.
The code is as following:
Code:
void MainWindow::doRequest()
{
// Read username, access token
socket = new QSslSocket(this);
QString data
= "GET https://graph.facebook.com/" + id
+ "?fields=id,name,about&method=GET&format=json&access_token="
+ QUrl::toPercentEncoding( access_token
) + " HTTP/1.1\r\n"
+ "Host: graph.facebook.com\r\n"
+ "Connection: Keep-alive\r\n"
+ "Accept-Encoding: gzip,deflate\r\n"
+ "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20100101 Firefox/21.0\r\n"
+ "Accept: */*\r\n"
+ "\r\n"
;
connect(socket, SIGNAL(readyRead()), this, SLOT(resp()));
socket->connectToHostEncrypted("graph.facebook.com", 443);
if (!socket->waitForEncrypted())
{
ui->edResponse->setText(socket->errorString());
}
socket->write(data.toAscii());
}
void MainWindow::resp()
{
responseData += tmp;
ui->edResponse->setText( ui->edResponse->toPlainText() + "\r\n" + responseData);
}
With that code i get a response as following:
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Cache-Control: private, no-cache, no-store, must-revalidate
Content-Type: application/json; charset=UTF-8
ETag: "581d7bde213d9c05777d0b6355eb626070e238fe"
Expires: Sat, 01 Jan 2000 00:00:00 GMT
Pragma: no-cache
X-FB-Rev: 831988
X-FB-Debug: GGFSORJxReaKTcVhWmvBe6iBZoQTLyjEFm++YDBAGwM=
Date: Fri, 31 May 2013 18:11:07 GMT
Connection: keep-alive
Content-Length: 58
{
"id": "100002634417763",
"name": "Lukas Juhrich"
}
So far, so well.
But when I add some property fields, e.g. “sports,likes,interests†(added right after the “about†in the request string), the response gets damaged, although the very same request made by facebook's graph-explorer tool returned a correct result:
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Cache-Control: private, no-cache, no-store, must-revalidate
Content-Type: application/json; charset=UTF-8
ETag: "d8e5cc3516f63acfeebc1a4db5b4a56b92809b7c"
Expires: Sat, 01 Jan 2000 00:00:00 GMT
Pragma: no-cache
X-FB-Rev: 831988
Content-Encoding: gzip
X-FB-Debug: mm/FphJrwWeme7wz/uyv2FiadQxR6TdJXA//z7xgTnw=
Date: Fri, 31 May 2013 18:14:51 GMT
Connection: keep-alive
Content-Length: 10612
‹
¬Ãtôµéªszð¾c]¾ÙûéêøÃ{•^
Ìîʓ+ï±µ’7;Q•ô ŒKk}É8AUHHHQ$õùú÷oµªT• luÂTA`ÂÃ@"þ›È8‘©Ò ¤T‡x³eÂŽpêà :ûq‘S÷ {ÆÃÚ¹¤n+ÃÙ7ªÀ Š&•ïT»5´`*¡/•‰+˜µ[h$ÔúŽ£q9öël¥ŽÂŒ „ÂúÂMAvKäN´’ˣߠS¹ÔyQö<¡¼Ã1—•à ç—láªZµßÂ⾻` z$`$QôâöÅ)wn!Ôdˆ§‹K W`§¯“Y¾·®t{?½ŸŒÒ ¨gÃ*Ÿ\¼Škþ„i_-V=DX±˜W¬«V:¬š,#ò¦™ ί.ë+“h£BUÖ}¿„â2-ŹÎã{•ëéêþZ¨à –è îK3¡¾ÞX¿LAªÛî’x‰ >ºæjv9Èâ
¾<©Šµïî0Yª£Ge‘¶P4 ’ØQãâ}´¬¦
wCÂC4ƒLÆÂïˆ`ºs´ïW“4¥Ú*TÂ#ç¶Â0¾nôQÂœÊ1Ó8nm嬗%Ê#Ã…)%ß"Â…nB7à Þ\=¬óxQ
£Stûg%«ªæ ¤a
£)¨ya²ÆóQ“£´vÇê #`+EyñÃTs!Eç!Ã*H—¦¿² ?<AÓ¤bÂM¹¢“š»Tµæ h~R¥ŠEµ©%ýŠ×»4nßà ´>Ã’Z÷NÂ~}«$ \®º#kÆó1#WjܙV¾Óà øÃ1ô‹þz’†ù*ªÿV¾ûˆ6"¢m†\à (ÂŽsßÀ4Éՙ9Ã*¿©U ¾ÑÅ:Ön2,:ää¦fEüb ·ÂœÂ¯Â¼Ã–nÙ¡!N ºö)ˆ'ž šTÿö¡• ñ
*(SP©³¥8ZSöÄfKf:°£qDà 82FžÀ5ø$SØI|0ò°Ӥ —.sdiƲ'Z!ê^!®Ø¯€ ÀÖ<ÚmÀ¯ì‹T«$-氅jÀ¯8©ËâQ ÞHS_ÃÕo;‰–=¦Øy:Ù¹ £d;É*4 ×õ°VOmÂv°¿z§ÃrZÀà $Â¥z˜^’Úæ¦<(¢dJˆ¨;( Ãœ_Róª<’”T7b)5o)¿Ì)T ª)ñW3öîHö*ÞÃ
·Ã*ÿ Q P ÂϛfÃJ”jR½hn£j‚Ä\ ¦ÖU”¡ð¢8n ÂMç0î)Ã(‘ÂÓÂە# azȢF êØ*8û&"Ã¥w7}R1–Pý¦Óà ^asèåóÂÃ*ÔB¨ÂÃʬÈ4}AÊKÃ*à ” Â’ÃNðPbÕ=¦â˜‘wm˜oà ºczHGÃ’~ú3Ã’2Yš©nb6J€3V66à ‘ey¥†,ç•ÓèöÙ.à µ<˜··H©]•Ö®ñxByžÊF6$óAÃŒJMà ŽpW¹ìÒêÂ4û¡+ý†yG y膎õª2'É;Ñò[H¡¡&õ*êÂHy
–ÞôòÃ@?Ã*äs:±®‰¾ šB,*)£«ÂÀ·t%žtPR=U*Ë tÆOãwu6j<ò«·=ùŽ¡üE “ÃY×
Do you have any kind of idea / tips etc. for me? thank you! :)
Re: HTTPS request returns damaged HTML body
One tip I can definitely give you is to not expect all data to come in one chunk. You can see that content-length header's value is 10612 which is definitely more than what you posted. Another tip I might give you is that if a header says "content-encoding: gzip" then it means content is compressed and you need to uncompress it.
Re: HTTPS request returns damaged HTML body
Also might make sense to check out QNetworkAccessManager to handle all the HTTP level (and below) protocol for you.
Cheers,
_
Re: HTTPS request returns damaged HTML body
NICE! It works!
Thank you very much!
both tips were very useful:
1. After I changed Accpt–encoding to none, I got some readable text, but not everything.
2. But when I changed the code using a QNetworkAcessmanager, it returned everything, and the code is a lot simpler.
Code:
void MainWindow::doRequest()
{
// INITIALIZATION
// Read username, access token
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(resp(QNetworkReply*)));
QNetworkRequest request(
"https://graph.facebook.com/"
+ id
+ "?fields=id,name,about,sports,likes,interests&method=GET&format=json&access_token="
+ QUrl::toPercentEncoding( access_token
) );
request.setRawHeader("Accept-Encoding", "none");
request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20100101 Firefox/21.0");
manager->get(request);
}
void MainWindow::resp(QNetworkReply *reply)
{
ui->edResponse->setText( ui->edResponse->toPlainText() + "\r\n" + responseData);
}
I wish you a nice day :)
Re: HTTPS request returns damaged HTML body
Just in case: one thing people who are new to using QNetworkAccessManager can miss is that it can be used for as many requests in parallel as one would want.
I.e. in your code you create the object every time doRequest() is called, but you would only have to do it once.
Each request is paired with a reply object and QNAM is able to process them individually, even multiple requests at the same time.
Cheers,
_
Re: HTTPS request returns damaged HTML body
Thnk you for these nice tips, I always try to improve my skills — so every little tip helps me a lot and saves me from hours of debugging ;)
Re: HTTPS request returns damaged HTML body
Well, then I have another one :)
You will need to delete the reply object, otherwise you are leaking it.
One way to do that is to schedule its deletion in the slot connected to the finished signals
Code:
void MainWindow::resp(QNetworkReply *reply)
{
ui->edResponse->setText( ui->edResponse->toPlainText() + "\r\n" + responseData);
reply->deleteLater(); // schedule deletion when event loop continues event processing
}
Cheers,
_