Qt5.7 implementation of the whole process of HTTPS authentication (hands-on version)

Source: Internet
Author: User

# # # NetworkRequestManager.h

#include <QSsl>
#include <QSslKey>
#include <QSslSocket>
#include <QSslConfiguration>
#include <QSslCertificate>
#include <QSslError>
#include <QUrl>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QtNetwork>

Class Networkrequestmanager:public Qobject
{
Q_object
Public
Explicit Networkrequestmanager (Qobject *parent = 0);
void sendmsg (const qstring&, const qstring&, const qstring&);
void Sendmsgwithouttoken (const qstring&, const qstring&);

Private
Qsslconfiguration m_sslconf;
Qnetworkrequest m_request;
Qnetworkaccessmanager *m_manager;

Signals:
void postrequestfinished (const qjsonobject&);

Private Slots:
void requestfinished (qnetworkreply*);
void Sslerrorsh (qnetworkreply*, qlist<qsslerror>);
};

#endif//Networkrequestmanager_h

** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **

** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **

1. Pc-client.crt

2. Pc-client.key

3. Pc-server.pem

These 3 key files are generated by the server, in this machine with the P12 file generation, do not know what reason can not be certified successful, perhaps because it will not use OpenSSL, the full solution of the great God.

About the HTTPS authentication process, self-Baidu bar. The Qnetwork class is also good for encapsulation, both asynchronously and with the parsing of the JSON string.

** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **

** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **

# # # NetworkRequestManager.cpp

Networkrequestmanager::networkrequestmanager (Qobject *parent): Qobject (parent)
{
Https CA Certificate:
Create a client certificate
QFile t_file (":/https/license/pc-client.crt");
T_file.open (qiodevice::readonly);
Const Qsslcertificate Certificate (&t_file, Qssl::P em);
T_file.close ();

Create a client private key
T_file.setfilename (":/https/license/pc-client.key");
T_file.open (qiodevice::readonly);
Const Qsslkey Prvatekey (&t_file, Qssl::rsa);
T_file.close ();

SSL authentication mode, TLS protocol version
M_sslconf.setpeerverifymode (Qsslsocket::verifypeer);
M_sslconf.setprotocol (Qssl::tlsv1_2orlater);
M_sslconf.setlocalcertificate (certificate);
M_sslconf.setprivatekey (Prvatekey);

Create a server-side certificate
Qlist<qsslcertificate> cacerlist;
T_file.setfilename (":/https/license/pc-server.pem");
T_file.open (qiodevice::readonly);
Const Qsslcertificate Cacertificate (&t_file, Qssl::P em);
To add a service certificate to the CA list
Cacerlist.append (Cacertificate);
M_sslconf.setcacertificates (cacerlist);
T_file.close ();

This->m_manager = new Qnetworkaccessmanager;
M_request.setsslconfiguration (m_sslconf);
Qobject::connect (M_manager, SIGNAL (finished (qnetworkreply*)), this, SLOT (requestfinished (qnetworkreply*)));
Qobject::connect (M_manager, SIGNAL (Sslerrors (qnetworkreply*,qlist<qsslerror>)), this, SLOT (Sslerrorsh ( (qnetworkreply*,qlist<qsslerror>)));
}

void Networkrequestmanager::sslerrorsh (qnetworkreply *reply, qlist<qsslerror> error)
{
Qdebug () << "* * Sslerrorsh ..." <<error;
}

void networkrequestmanager::requestfinished (qnetworkreply *reply)
{
int statusCode = Reply->attribute (Qnetworkrequest::httpstatuscodeattribute). ToInt ();
Qvariant Statuscodev =
Reply->attribute (Qnetworkrequest::httpstatuscodeattribute);

Qjsondocument Jdoc = Qjsondocument::fromjson (Reply->readall (). Constdata ());
Qjsonobject obj = Jdoc.object ();

Qdebug () << "* * requestfinished" <<reply->isfinished () <<statusCode;

if (!obj.isempty ()) emit this->postrequestfinished (obj);
Reply->deletelater ();
}

Qt5.7 implementation of the whole process of HTTPS authentication (hands-on version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.