QT https post request (QNetworkRequest needs to set up an SSL certificate, and there are three types of SSL certificate authentication)

 

Because https access requires SSL authentication, and QT does not support SSL authentication by default, some preparations must be done before use:

 

The OpenSSL library needs to be installed:

1. First open http://slproweb.com/products/Win32OpenSSL.html webpage;

2. Download the installation package, what I downloaded is: Win32 OpenSSL v1.0.1c Light installation package, this version will be continuously updated as time progresses;

3. Install (exe file) to the local, and choose to install the library to the OpenSSL installation directory (/bin) during the installation process.

4. Copy the libeay32.dll and ssleay32.dll files to the file directory where QtNetwork4.dll and QtNetworkd4.dll are stored in Qt. Mine is D:\Qt\4.8.2\bin

 

Code can now be written:

QNetworkRequest request;
QSslConfiguration config;

 config.setPeerVerifyMode(QSslSocket::VerifyNone);
 config.setProtocol(QSsl::TlsV1);
 request.setSslConfiguration(config);

The above three sentences add back the verification certificate mode and protocol, and set it to the corresponding QNetworkRequest. It is very important. It took me half a day to find the problem here.

request.setUrl(QUrl("https://www.xxx.com/html.jsp"));
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
request.setHeader(QNetworkRequest::ContentLengthHeader, bytePost.length());
QNetworkReply *pReply = pManager->post(request, bytePost);

 

Remark:

There are three types of SSL certificate authentication:

1.
 QSslConfiguration config ;

 config.setPeerVerifyMode(QSslSocket::VerifyNone);
 config.setProtocol(QSsl::TlsV1);

 QNetworkRequest request(req);
 request.setSslConfiguration(config);
 

 2.
 QNetworkRequest request ;

 request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
 QNetworkReply* reply = QNetworkAccessManager::createRequest(op, request, outgoingData);
 reply->ignoreSslErrors();
 

 3.
 QSslConfiguration config ;

 QList<QSslCertificate> certs = QSslCertificate::fromPath("C:\\FiddlerRoot.crt");
 config.setCaCertificates(certs);

 QNetworkRequest request(req);
 request.setSslConfiguration(config);

 

FROM -- https://www.cnblogs.com/findumars/p/5574305.html

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324903306&siteId=291194637