2014-03-27 99 views
0

我拼命嘗試設置QSslSocket連接,我嘗試從黑莓示例開始,但始終在控制檯上獲取無效的URL錯誤,但沒有任何詳細信息... 以下是我嘗試運行的代碼:QSslSocket返回無效的網址

if (!m_socket) { 

    bool res; 
    Q_UNUSED(res); 
    m_socket = new QSslSocket(); 

    // Connect to signals to receive notifications 
    // about state changes 

    res = QObject::connect(m_socket, 
       SIGNAL(sslErrors(QList<QSslError>)), 
       this, 
       SLOT(onSslErrors(QList<QSslError>))); 

    Q_ASSERT(res); 

    res = QObject::connect(m_socket, 
      SIGNAL(stateChanged(QAbstractSocket::SocketState)), 
      this, 
      SLOT(onSocketSateChange(QAbstractSocket::SocketState))); 
    Q_ASSERT(res); 

    res = QObject::connect(m_socket, 
        SIGNAL(error (QAbstractSocket::SocketError)), 
        this, 
        SLOT(onError(QAbstractSocket::SocketError))); 
      Q_ASSERT(res); 

    res = QObject::connect(m_socket, SIGNAL(encrypted()), 
      this, 
      SLOT(onSocketEncrypted())); 
    Q_ASSERT(res); 

    res = QObject::connect(m_socket, SIGNAL(readyRead()), 
      this, 
      SLOT(onSocketReadyRead())); 
    Q_ASSERT(res); 


} 

// Make the SSL connection to the host on the specified port 
m_socket->connectToHostEncrypted("www.blackberry.com", 443); 

/* if (!m_socket->waitForEncrypted()) { 
    qDebug() << m_socket->errorString(); 
    //return 1; 
} 
*/ 
} 

如果有人可以幫助或告訴我一個SSL樣的工作?

回答

0

這是在客戶端的樣本SSL連接。我連接到「www.blackberry.com」,443這段代碼:

SSLClient::SSLClient(QObject *parent) 
{ 

    if (!QSslSocket::supportsSsl()) 
     QMessageBox::information(0, "Secure Socket Client", 
       "This system does not support OpenSSL."); 

    client_socket.setProtocol(QSsl::SslV3); 

    connect(&client_socket, SIGNAL(error(QAbstractSocket::SocketError)), 
      this, SLOT(TCPError(QAbstractSocket::SocketError))); 

    connect(&client_socket, SIGNAL(sslErrors(QList<QSslError>)), 
      this, SLOT(sslError(QList<QSslError>))); 

    connect(&client_socket, SIGNAL(readyRead()), 
      this, SLOT(tcpReady())); 

    connect(&client_socket, SIGNAL(encrypted()), 
      this, SLOT(enable_client())); 

    client_socket.abort(); 
    client_socket.connectToHostEncrypted("www.blackberry.com", 443); 


} 


void SSLClient::tcpReady() 
{ 
    QByteArray array = client_socket.read(client_socket.bytesAvailable()); 
    QString str; 


    QTextCodec *codec = QTextCodec::codecForName("UTF-16"); 
    QTextDecoder *decoderWithoutBom = codec->makeDecoder(QTextCodec::IgnoreHeader); 
    str = decoderWithoutBom->toUnicode(array); 

} 

void SSLClient::sslError(QList<QSslError> errors) 
{ 

    client_socket.ignoreSslErrors(); 

} 

void SSLClient::TCPError(QAbstractSocket::SocketError error) 
{ 

    QMessageBox::warning(this, tr("Error"),client_socket.errorString()); 

    client_socket.disconnectFromHost(); 
} 
+0

感謝Nejat,其實我的代碼工程,我succeded來管理我的CA和所有的東西的工作,但我有兩次出現的URL無效在我的控制檯中,我認爲qsssocket嘗試密鑰庫所有的CA,並且因此第2次失敗...我應該嘗試設置一個默認的可能... – user3469381