Results 1 to 7 of 7

Thread: HTTPS request returns damaged HTML body

  1. #1
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Question 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:

    Qt Code:
    1. void MainWindow::doRequest()
    2. {
    3. // Read username, access token
    4. QString id = QString(ui->edUserID->text());
    5. QString access_token = QString(ui->edAcessToken->text());
    6.  
    7. socket = new QSslSocket(this);
    8. QString data = "GET https://graph.facebook.com/"
    9. + id
    10. + "?fields=id,name,about&method=GET&format=json&access_token="
    11. + QUrl::toPercentEncoding( access_token )
    12. + " HTTP/1.1\r\n"
    13. + "Host: graph.facebook.com\r\n"
    14. + "Connection: Keep-alive\r\n"
    15. + "Accept-Encoding: gzip,deflate\r\n"
    16. + "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20100101 Firefox/21.0\r\n"
    17. + "Accept: */*\r\n"
    18. + "\r\n"
    19. ;
    20.  
    21. connect(socket, SIGNAL(readyRead()), this, SLOT(resp()));
    22.  
    23.  
    24. socket->connectToHostEncrypted("graph.facebook.com", 443);
    25. if (!socket->waitForEncrypted())
    26. {
    27. ui->edResponse->setText(socket->errorString());
    28. }
    29.  
    30. socket->write(data.toAscii());
    31.  
    32. }
    33.  
    34. void MainWindow::resp()
    35. {
    36. QString responseData;
    37. QString tmp = QString(socket->readAll().data());
    38. responseData += tmp;
    39.  
    40. ui->edResponse->setText( ui->edResponse->toPlainText() + "\r\n" + responseData);
    41. }
    To copy to clipboard, switch view to plain text mode 

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    ljuhrich (1st June 2013)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    ljuhrich (1st June 2013)

  6. #4
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up 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.

    Qt Code:
    1. void MainWindow::doRequest()
    2. {
    3. // INITIALIZATION
    4.  
    5. // Read username, access token
    6. QString id = QString(ui->edUserID->text());
    7. QString access_token = QString(ui->edAcessToken->text());
    8.  
    9.  
    10. manager = new QNetworkAccessManager(this);
    11. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(resp(QNetworkReply*)));
    12.  
    13. QNetworkRequest request(
    14. "https://graph.facebook.com/"
    15. + id
    16. + "?fields=id,name,about,sports,likes,interests&method=GET&format=json&access_token="
    17. + QUrl::toPercentEncoding( access_token )
    18. );
    19. request.setRawHeader("Accept-Encoding", "none");
    20. request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20100101 Firefox/21.0");
    21.  
    22. manager->get(request);
    23. }
    24.  
    25. void MainWindow::resp(QNetworkReply *reply)
    26. {
    27. QString responseData = QString::fromAscii(reply->readAll());
    28. ui->edResponse->setText( ui->edResponse->toPlainText() + "\r\n" + responseData);
    29. }
    To copy to clipboard, switch view to plain text mode 

    I wish you a nice day

  7. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    ljuhrich (1st June 2013)

  9. #6
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

  10. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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

    Qt Code:
    1. void MainWindow::resp(QNetworkReply *reply)
    2. {
    3. QString responseData = QString::fromAscii(reply->readAll());
    4. ui->edResponse->setText( ui->edResponse->toPlainText() + "\r\n" + responseData);
    5.  
    6. reply->deleteLater(); // schedule deletion when event loop continues event processing
    7. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  11. The following user says thank you to anda_skoa for this useful post:

    ljuhrich (5th June 2013)

Similar Threads

  1. Replies: 1
    Last Post: 5th December 2012, 10:19
  2. Replies: 0
    Last Post: 22nd October 2012, 13:39
  3. Replies: 0
    Last Post: 3rd May 2011, 10:15
  4. any body using qextserialport?
    By yagabey in forum Newbie
    Replies: 4
    Last Post: 25th December 2007, 23:08
  5. Https POST Request
    By munna in forum Qt Programming
    Replies: 10
    Last Post: 11th November 2006, 15:24

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.