Results 1 to 2 of 2

Thread: curl command to ssl handshake and https posts

  1. #1
    Join Date
    Sep 2015
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Question curl command to ssl handshake and https posts

    hey there,

    I would like to transform a curl request to a qt method curl command looks like this:
    Qt Code:
    1. curl --cacert cacert.pem --request 'POST' 'https://api.twitter.com/1.1/statuses/update.json' --data 'status=ekmek' --header 'Authorization: OAuth oauth_consumer_key="ir0j0XHEZAjl5OIMLvRtGDpvz", oauth_nonce="d0d616f462d9d3dd5bb39b23b2ae76ea", oauth_signature="2IAkMds2Pxtp699MReoyupgfZl8%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1441658718", oauth_token="3427524377-klG6CAjAWyWOo9eR7g6WtRm8nGoo9CtQHSX3jpC", oauth_version="1.0"' --verbose
    To copy to clipboard, switch view to plain text mode 


    so I want to have a ssl handshake using my cacert.pem and send my request with a body and a custom header.

    thanks in advance

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: curl command to ssl handshake and https posts

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QNetworkAccessManager>
    3. #include <QNetworkRequest>
    4. #include <QNetworkReply>
    5. #include <QUrlQuery>
    6. #include <QDebug>
    7. #include <QSslConfiguration>
    8.  
    9. class Fetch: public QObject
    10. {
    11. Q_OBJECT
    12. public:
    13. Fetch(QObject *p = 0):
    14. QObject(p),
    15. mgr(new QNetworkAccessManager(this)),
    16. reply(0)
    17. {
    18. QNetworkRequest req(QUrl("https://api.twitter.com/1.1/statuses/update.json"));
    19.  
    20. // Standard header
    21. req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    22.  
    23. // The non-standard header
    24. req.setRawHeader(
    25. QByteArray("Authorization"),
    26. QByteArray( "OAuth "
    27. "oauth_consumer_key=\"ir0j0XHEZAjl5OIMLvRtGDpvz\", "
    28. "oauth_nonce=\"d0d616f462d9d3dd5bb39b23b2ae76ea\", "
    29. "oauth_signature=\"2IAkMds2Pxtp699MReoyupgfZl8%3D\", "
    30. "oauth_signature_method=\"HMAC-SHA1\", "
    31. "oauth_timestamp=\"1441658718\", "
    32. "oauth_token=\"3427524377-klG6CAjAWyWOo9eR7g6WtRm8nGoo9CtQHSX3jpC\", "
    33. "oauth_version=\"1.0\" "
    34. )
    35. );
    36.  
    37. // The POST payload
    38. QUrlQuery payload;
    39. payload.addQueryItem("status", "ekmek");
    40. QByteArray data = payload.query(QUrl::FullyEncoded).toLocal8Bit();
    41.  
    42. reply = mgr->post(req, data);
    43. connect(reply, SIGNAL(finished()), SLOT(finished()));
    44. }
    45. private slots:
    46. void error(QNetworkReply::NetworkError code) {
    47. qDebug() << Q_FUNC_INFO << code;
    48. }
    49.  
    50. void sslErrors(const QList<QSslError> & errors) {
    51. qDebug() << Q_FUNC_INFO << errors;
    52. }
    53.  
    54. void finished() {
    55. qDebug() << Q_FUNC_INFO << reply->readAll();
    56. reply->deleteLater();
    57. qApp->exit();
    58. }
    59.  
    60. private:
    61. QNetworkAccessManager *mgr;
    62. QNetworkReply *reply;
    63. };
    64.  
    65. int main(int argc, char *argv[])
    66. {
    67. QCoreApplication a(argc, argv);
    68.  
    69. // Something like this if you need it
    70. #if 0
    71. QSslConfiguration config = QSslConfiguration::defaultConfiguration();
    72. QList<QSslCertificate> certs = config.caCertificates();
    73. certs << QSslCertificate::fromPath("cacert.pem", QSsl::Pem);
    74. config.setCaCertificates(certs);
    75. QSslConfiguration::setDefaultConfiguration(config);
    76. #endif
    77.  
    78. Fetch f;
    79. return a.exec();
    80. }
    81.  
    82. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Connects to Twiteer but fails:
    Qt Code:
    1. void Fetch::finished() "{"errors":[{"code":32,"message":"Could not authenticate you."}]}"
    To copy to clipboard, switch view to plain text mode 
    so it clearly connected to Twitter without SSL errors.

Similar Threads

  1. How to code a (simple ?) cUrl command in Qt ?
    By nils_heidorn in forum Newbie
    Replies: 15
    Last Post: 30th August 2013, 11:36
  2. Replies: 2
    Last Post: 16th November 2012, 02:39
  3. QSslSocket based server never finishes handshake (Solved)
    By December in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2009, 14:15
  4. QWebView SSL Handshake failed
    By srikanth_trulyit in forum Qt Programming
    Replies: 1
    Last Post: 19th June 2009, 10:31

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.