QT https post請求(QNetworkRequest要設置SSL證書,而SSL證書認證有三種)

 

由於https訪問須要用到SSL認證,而QT默認是不支持SSL認證,因此在使用以前必須先作一些準備工做:html

 

須要安裝OpenSSL庫:web

一、首先打開http://slproweb.com/products/Win32OpenSSL.html網頁;json

二、下載安裝包,我下載的是:Win32 OpenSSL v1.0.1c Light安裝包,隨着時間的推動,這個版本會不斷更新的;api

三、安裝(exe文件)到本地,而且在安裝過程當中選擇將庫安裝到OpenSSL的安裝目錄(/bin)下面。app

四、將libeay32.dll 和 ssleay32.dll 文件拷貝到Qt中的存放有QtNetwork4.dll和QtNetworkd4.dll的文件目錄下。個人是D:\Qt\4.8.2\binjsp

 

如今能夠編寫代碼:post

QNetworkRequest request;
QSslConfiguration config;htm

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

上面三句添回檢驗證書模式與協議,並設置給對應QNetworkRequest,很是重要,在這裏花費了我半天的時間才找到問題ip

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);

 

備註:

SSL證書認證,有如下三種:

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 -- http://www.javashuo.com/article/p-miovotor-ga.html

相關文章
相關標籤/搜索