Port QHttp class from Qt 4 to Qt 5

4.3k views Asked by At

I have an application created in Qt 4 that uses the QHttp class. The new Qt 5 has almost the same functionality in the QNetworkAccessManager class, but I need a couple of tweaks:

  1. My piece of code is:

    http=new QHttp ( this );
    if ( config->brokerurl.indexOf ( "https://" ) ==0 )
        neworkAccess->
        http->setHost ( lurl.host(),QHttp::ConnectionModeHttps,
                        lurl.port ( 443 ) );
    else
        http->setHost ( lurl.host(),QHttp::ConnectionModeHttp,
                        lurl.port ( 80 ) );
    

    I have found the QNetworkRequest class that uses the URL address to post a message, but I'm using http and https that work in different ports, so I need to set also this ConnectionMode(http or https).

    Is there a way to also set a ConnectionMode in QNetworkRequest?

  2. I have this piece of code:

    http->post ( lurl.path(),req.toUtf8(),&httpSessionAnswer );
    

    This in Qt 4 has the signature:

    int post ( const QString & path, const QByteArray & data, QIODevice * to = 0 )<br><br>
    

    In Qt 5 we have:

    QNetworkReply * post ( const QNetworkRequest & request, QIODevice * data )
    
    QNetworkReply * post ( const QNetworkRequest & request, const QByteArray & data )
    

    What is the new Qt 5 equivalent for posting a message (a request+data) and also getting an answer(QIODevice * data)?

1

There are 1 answers

0
Greenflow On

You can set the port in QUrl(). Something like (extremely simplified):

QNetworkAcessManager http;
QUrl url;
url.setHost("xxx.xxx.xxx.xxx");
url.setPort(xxxx);
QNetworkRequest req(url);
http.post(req,....);

For sslErrors:

You find the signal in QNetworkReply. You can do:

connect(&http,SIGNAL(finished(QNetworkReply *)),
this,SLOT(finishedSlt(QNetworkReply *)));

In your slot:

finishedSlt(QNetworkReply *reply){
    connect(reply,SIGNAL(sslErrors(const QList<QSslError> &)),
    SLOT(sslErrorsSlt(QList<QSslError> &)));
 }

Disclaimer: This code is only an example and not meant to be copied/pasted into a production system.