Results 1 to 7 of 7

Thread: SSL JSON in Qt 4.5.3 + MinGW 3.81

  1. #1
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default SSL JSON in Qt 4.5.3 + MinGW 3.81

    Hello everyone,

    I'm trying to make a quick program to retrieve buttcoin price from btc-e in json format, but I'm not actually receiving anything in the QNetworkReply. I think it's due to SSL (Heartbleed) updates that I don't have, but I'm not quite sure. Here's my code:

    Qt Code:
    1. #include <QtGui/QMainWindow>
    2. #include <QUrl>
    3. #include <QNetworkAccessManager>
    4. #include <QNetworkRequest>
    5. #include <QNetworkReply>
    6. #include <QSslConfiguration>
    7. #include <QNetworkProxy>
    8.  
    9. //in constructor
    10. QNetworkAccessManager *nwam = new QNetworkAccessManager();
    11. connect(nwam,SIGNAL(finished(QNetworkReply*)),this,SLOT(rfinish(QNetworkReply*)));
    12.  
    13. void mainWindow::pbSendClicked(){
    14. QUrl url("https://btc-e.com/api/3/ticker/btc_usd");
    15. QNetworkRequest request;
    16. request.setUrl(url);
    17.  
    18. QSslConfiguration config = QSslConfiguration::defaultConfiguration();
    19. config.setProtocol(QSsl::TlsV1);
    20. request.setSslConfiguration(config);
    21.  
    22. nwam->get(request);
    23. }
    24.  
    25. void hcode::rfinish(QNetworkReply *r){
    26. QString herp=QString::fromUtf8(r->readAll());
    27. //debug
    28. cout << "reply: " << herp.toStdString().c_str() << endl;
    29. }
    To copy to clipboard, switch view to plain text mode 

    The herp string is empty when I send the query. I have libeay32.dll and ssleay32.dll in the same folder as the exe. What's missing?


    Thanks in advance.


    Mr_Cloud

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: SSL JSON in Qt 4.5.3 + MinGW 3.81

    What have you tried so far?

    A non https site?
    Not forcing a specific protocol?
    Connected to the reply's sslErrors() signal?
    Checked for any runtime log/warning output?

    Cheers,
    _

  3. #3
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: SSL JSON in Qt 4.5.3 + MinGW 3.81

    Hi Anda,

    >non-https
    Yes, it works

    >not forcing specific protocol
    If I don't force a protocol, reply is blank; if I force TLS, QList<QSslError> has two entries, both being "No error" for https://btc-e.com

    >connected to sslErrors()
    Thanks for the tip; see previous

    >check for any runtime warning output
    If I launch from Qt Creator, I don't have any errors; program exits with return code 0.

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

    Default Re: SSL JSON in Qt 4.5.3 + MinGW 3.81

    So sslErrors gets emitted, but the list only contains "no error" entries?
    Sounds strange. Both the enum and the string value?

    Does the network reply error give you any hint?
    Also check for the case of not forcing a protocol.

    Cheers,
    _

  5. #5
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: SSL JSON in Qt 4.5.3 + MinGW 3.81

    Thanks for your replies, anda_skoa. I assume that by enum you mean the number of errors? In which case it'd agree, it's 2. Forcing SSLv3 or SSLv2 has identical effect to not forcing protocol: Blank reply. Forcing TLS gives the strange SSL errors. Can confirm again that http:// returns index.html of a page just fine, including this one's!

    Here's the code I'm using:


    mw.h
    Qt Code:
    1. #ifndef MW_H
    2. #define MW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QNetworkAccessManager>
    6. #include <QNetworkRequest>
    7. #include <QNetworkReply>
    8. #include <QSslError>
    9. #include <QSslConfiguration>
    10. #include <QUrl>
    11. #include <fstream>
    12.  
    13. namespace Ui
    14. {
    15. class mw;
    16. }
    17.  
    18. class mw : public QMainWindow
    19. {
    20. Q_OBJECT
    21.  
    22. public:
    23. mw(QWidget *parent = 0);
    24. ~mw();
    25.  
    26. public slots:
    27. void rfinish(QNetworkReply *);
    28. void refresh();
    29. void errors(QNetworkReply *,QList<QSslError>);
    30.  
    31. private:
    32. Ui::mw *ui;
    33. QNetworkAccessManager *nwam;
    34. };
    35.  
    36. #endif // MW_H
    To copy to clipboard, switch view to plain text mode 

    mw.cpp
    Qt Code:
    1. #include "mw.h"
    2. #include "ui_mw.h"
    3.  
    4. using namespace std;
    5.  
    6. mw::mw(QWidget *parent)
    7. : QMainWindow(parent), ui(new Ui::mw)
    8. {
    9. ui->setupUi(this);
    10. nwam=new QNetworkAccessManager();
    11. connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(refresh()));
    12. connect(nwam,SIGNAL(finished(QNetworkReply*)),this,SLOT(rfinish(QNetworkReply*)));
    13. connect(nwam,SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),this,SLOT(errors(QNetworkReply*,QList<QSslError>)));
    14. }
    15.  
    16. void mw::refresh(){
    17. QUrl url("https://www.btc-e.com/api/3/ticker/btc_usd");
    18. QNetworkRequest request;
    19. request.setUrl(url);
    20.  
    21. QSslConfiguration config = QSslConfiguration::defaultConfiguration();
    22. // config.setProtocol(QSsl::TlsV1);
    23. request.setSslConfiguration(config);
    24.  
    25. nwam->get(request);
    26. ui->statusBar->showMessage("Updating...",0);
    27. }
    28.  
    29. void mw::rfinish(QNetworkReply *r){
    30. QString reply=QString::fromUtf8(r->readAll());
    31. ofstream file;
    32. file.open("debug.txt");
    33. file << reply.toStdString().c_str() << endl;
    34. file.close();
    35. ui->statusBar->showMessage("reply received",3000);
    36. }
    37.  
    38. void mw::errors(QNetworkReply *r,QList<QSslError> errList){
    39. ofstream file;
    40. file.open("debugErr.txt");
    41. file << errList.size() << endl;
    42. for (int i=0;i<errList.size();i++) file << errList[i].errorString().toStdString().c_str() << endl;
    43. file.close();
    44. ui->statusBar->showMessage("SSL errors",3000);
    45. }
    46.  
    47. mw::~mw()
    48. {
    49. delete ui;
    50. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp (default
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mw.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. mw w;
    8. w.show();
    9. return a.exec();
    10. }
    11.  
    12. .pro file has QT += network added
    13. .ui file has a push button called pushButton.
    To copy to clipboard, switch view to plain text mode 

    Any ideas?

    Thanks in advance.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: SSL JSON in Qt 4.5.3 + MinGW 3.81

    Quote Originally Posted by Mr_Cloud View Post
    Thanks for your replies, anda_skoa. I assume that by enum you mean the number of errors?
    No, I meant the value returned by the error() method, the one that returns an error code, one of a set of enum values.

    Maybe also check r->error() and r->errorString() in the slot connected to finish().

    Another thing you can try, just to see if it has any affect, call ignoreSslErrors() on the reply object right after it gets returns from nwam->get().

    Cheers,
    _

  7. #7
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: SSL JSON in Qt 4.5.3 + MinGW 3.81

    Ah yes. Ok enum was 0 for both "No error" entries when forcing TLS.

    I outputted the error() and errorString() in rfinish() [edit: when not forcing TLS or SSL] and we're onto something! Enum 6, SSL handshake failed


    Added after 24 minutes:


    Update: Your solution worked. Putting r->ignoreSslErrors(); in the mw::errors() function did it. Thanks a lot mate!


    Regards,
    Mr_Cloud
    Last edited by Mr_Cloud; 26th June 2015 at 16:33.

Similar Threads

  1. json
    By nabeel in forum Newbie
    Replies: 3
    Last Post: 29th August 2013, 09:24
  2. json parsing
    By sabbu in forum Newbie
    Replies: 1
    Last Post: 17th June 2011, 14:04
  3. Qt and JSON
    By Chiggins in forum Qt Programming
    Replies: 2
    Last Post: 28th June 2010, 14:12
  4. json: eval
    By mickey in forum General Programming
    Replies: 3
    Last Post: 17th January 2008, 20:50

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.