Results 1 to 3 of 3

Thread: Qnetworkaccessmanager post request get error ProtocolUnknownError

  1. #1
    Join Date
    Mar 2017
    Posts
    2
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Qnetworkaccessmanager post request get error ProtocolUnknownError

    i can't find what wrong in my code. Im try test use this data at https://www.hurl.it and it return json.
    But when run Qt, i got error

    Qt Code:
    1. 1: "4/ufy-e-ud4_3XxlHYN7VNp0nZB9G26EzLX_CrEt0F3QA"
    2. ***** request url= QUrl("www.googleapis.com/oauth2/v4/token")
    3. ***** request content type header= 0
    4. ***** call POST *****
    5. qt.network.ssl: QSslSocket: cannot resolve SSLv2_client_method
    6. qt.network.ssl: QSslSocket: cannot resolve SSLv2_server_method
    7. 5
    8. ***** finishedSlot! *****
    9. ***** reply http error = 301
    10. ***** response ***** ""
    To copy to clipboard, switch view to plain text mode 

    PLZ ANYONE CAN HELP ?

    Qt Code:
    1. #include "gettoken.h"
    2. #include <QNetworkRequest>
    3. #include <QFile>
    4. #include <QDir>
    5. #include <QTextCodec>
    6. #include <QUrlQuery>
    7. #include <QUrl>
    8. #include <QSslSocket>
    9.  
    10. getToken::getToken(QObject *parent) : QObject(parent)
    11. {
    12. appID = "1037983989117-p3cllh8ou68c32v9irrh7i9fcdk8egs7.apps.googleusercontent.com";
    13. secretString = "uQQZK2Jb9XOprgYzYjrrWj3s";
    14. qReply = Q_NULLPTR;
    15. accessUrl = "www.googleapis.com/oauth2/v4/token";
    16. qnam = new QNetworkAccessManager(this);
    17. }
    18.  
    19. void getToken::requestToken(QString code)
    20. {
    21.  
    22. qDebug()<<"1: "<<code; //very sure this is right code
    23. QUrl setUrl(accessUrl);
    24. QNetworkRequest qRequest(setUrl);
    25. qRequest.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
    26. connect(qnam,SIGNAL(finished(QNetworkReply *)), this,SLOT(writeFile(QNetworkReply *)));
    27.  
    28.  
    29. QByteArray qByte;
    30. qByte.append("code="+code+"&");//very sure this is right code
    31. qByte.append("client_id=1037983989117-p3cllh8ou68c32v9irrh7i9fcdk8egs7.apps.googleusercontent.com&");
    32. qByte.append("client_secret=uQQZK2Jb9XOprgYzYjrrWj3s&");
    33. qByte.append("redirect_uri=http://localhost:8080/cb&");
    34. qByte.append("grant_type=authorization_code");
    35.  
    36. qDebug() << "***** request url=" << qRequest.url();
    37. qDebug() << "***** request content type header=" << qRequest.ContentTypeHeader;
    38. qDebug() << "***** call POST *****";
    39. qnam->post(qRequest, qByte);
    40. // qnam.post(qRequest,qByte);
    41.  
    42. qDebug()<<"5";
    43.  
    44. // qDebug()<<"almost done";
    45. }
    46.  
    47. void getToken::writeFile(QNetworkReply *mReply)
    48. {
    49. qDebug() << "***** finishedSlot! *****";
    50. // Reading attributes of the reply
    51. // e.g. the HTTP status code
    52. QVariant statusCodeV =
    53. mReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    54. // Or the target URL if it was a redirect:
    55. QVariant redirectionTargetUrl =
    56. mReply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    57. // see CS001432 on how to handle this
    58.  
    59. // no error received?
    60. if (mReply->error() == QNetworkReply::NoError)
    61. {
    62. // read data from QNetworkReply here
    63. // Example 2: Reading bytes form the reply
    64. QByteArray bytes = mReply->readAll(); // bytes
    65. QString stringResponse(bytes); // string
    66.  
    67. qDebug() << "***** response *****" << stringResponse;
    68. }
    69. // Some http error received
    70. else
    71. {
    72. qDebug() << "***** reply http error =" <<(int) mReply->error();
    73.  
    74. QByteArray bytes = mReply->readAll(); // bytes
    75. QString stringResponse(bytes); // string
    76. qDebug() << "***** response *****" << stringResponse;
    77. }
    78.  
    79. // We receive ownership of the reply object
    80. // and therefore need to handle deletion.
    81. delete mReply;
    82. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Qnetworkaccessmanager post request get error ProtocolUnknownError

    Looks like you need to prefix the URL with https://?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

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

    Ryu_shai (9th March 2017)

  4. #3
    Join Date
    Mar 2017
    Posts
    2
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Qnetworkaccessmanager post request get error ProtocolUnknownError

    oh well, thanks you somuch, that extraclly what i miss. thank you so much guy

Similar Threads

  1. QT5 http post request
    By marty.marty in forum Qt Programming
    Replies: 0
    Last Post: 14th January 2015, 12:44
  2. Replies: 1
    Last Post: 6th February 2013, 13:18
  3. Request ID of QNetworkaccessmanager get and post request
    By dineshkumar in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2011, 22:56
  4. POST request to a web service
    By QPlace in forum Qt Programming
    Replies: 3
    Last Post: 6th November 2008, 09:05
  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.