QNetworkAccessManager建立SSH连接时出现SslHandshakeFailedError

在使用QNetworkAccessManager通过HTTPS访问cgi程序时遇到SslHandshakeFailedError。通过对比IE访问的wireshark抓包,发现在设置QSslConfiguration的peerVerifyMode为QSslSocket::VerifyNone后,问题得到解决。默认模式为QSslSocket::AutoVerifyPeer,此模式根据socket的QSocket::SslMode选择合适的验证模式。

程序需要通过HTTPS访问一个cgi程序,出现错误:QNetworkReply::SslHandshakeFailedError

wireshark抓取通过IE访问该页面和执行程序访问该页面的包,没有看到明显差异。但使用IE访问时,会提示certificationerror,需要选择继续访问才能访问该页面。

经过一番搜索和尝试,发现将QSslConfiguration中的peerverify mode设置为QSslSocket::VerfifyNone后问题解决。缺省mode为AutoVerifyPeer。QtAssistant上的描述是:

enum QSslSocket::PeerVerifyMode

Describes the peer verification modes for QSslSocket.The default mode is AutoVerifyPeer, which selects an appropriatemode depending on the socket's QSocket::SslMode.

ConstantValueDescription
QSslSocket::VerifyNone0QSslSocket willnot request a certificate from the peer. You can set this mode ifyou are not interested in the identity of the other side of theconnection. The connection will still be encrypted, and your socketwill still send its local certificate to the peer if it'srequested.
QSslSocket::QueryPeer1QSslSocket willrequest a certificate from the peer, but does not require thiscertificate to be valid. This is useful when you want to displaypeer certificate details to the user without affecting the actualSSL handshake. This mode is the default for servers.
QSslSocket::VerifyPeer2QSslSocket willrequest a certificate from the peer during the SSL handshake phase,and requires that this certificate is valid. Onfailure, QSslSocket willemit theQSslSocket::sslErrors()signal. This mode is the default for clients.
QSslSocket::AutoVerifyPeer3QSslSocket willautomatically use QueryPeer for server sockets and VerifyPeer forclient sockets.

This enum was introduced or modified in Qt 4.4.

See also QSslSocket::peerVerifyMode().

       

       manager= new QNetworkAccessManager;
       QNetworkRequest request;

       QString url = tr("https://username:password@%1%2").arg(curEnb.enbIp).arg(cgiPathMap["ENABLESSH"]);
       request.setUrl(QUrl(url));

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

       if(enableDebug)
       {
           logTab->append(tr("QNetworkRequest::Attribute"));
           parseNetRequestAttribute(&request);
       }

       reply = manager->get(request);
       if(enableDebug)
       {
           logTab->append(tr("GET%1").arg(reply->url().toString()));
       }

       connect(manager, SIGNAL(finished(QNetworkReply*)), this,SLOT(onNetManagerFinished(QNetworkReply*)));
       connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this,SLOT(onNetReplyError(QNetworkReply::NetworkError)));
       connect(reply, SIGNAL(sslErrors(QList&)), this,SLOT(onNetReplySshErrors(QList&)));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值