简体   繁体   中英

QSslSocket return Invalid url

I am desperately trying to set QSslSocket connection, I try to start from blackberry sample but always get an Invalid URL error on my console without any details... Here is the code I try to run:

    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;
}
 */
 }

If anyone can help or show me an ssl sample working...

This is a sample SSL connection on client side. I connected to "www.blackberry.com", 443 by this code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM