PDA

View Full Version : Creating a HTTP connection



Momergil
25th December 2012, 16:54
Hello!

I want to create a http connection to a webserver and start downloading data in a QThread, but I'm having some problems.

First, my code is returning usually nothing from my searches:



HTTPClass::HTTPClass(QObject *parent) :
QObject(parent)
{
namConnection = new QNetworkAccessManager(this);

connect(namConnection, SIGNAL(finished(QNetworkReply*)),
this, SLOT(slotReplyFinished(QNetworkReply*)));
connect(namConnection, SIGNAL(authenticationRequired(QNetworkReply*,QAuth enticator*)),
this, SLOT(slotAuthenticationRequired(QNetworkReply*,QAu thenticator*)));
connect(namConnection, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,Q Authenticator*)),
this, SLOT(slotProxyAuthenticationRequired(QNetworkProxy ,QAuthenticator*)));
connect(namConnection, SIGNAL(networkSessionConnected()),
this, SLOT(slotNetworkSessionConnected()));

QNetworkRequest request;
request.setUrl(QUrl("http://en.wikipedia.org/wiki/Main_Page"));

reply = namConnection->get(request);
connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(slotError(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
this, SLOT(slotSslErrors(QList<QSslError>)));
}

void HTTPClass::setConnectionData()
{

}

void HTTPClass::slotReplyFinished(QNetworkReply *reply)
{
qDebug() << reply->readAll();

reply->deleteLater();
}

void HTTPClass::slotAuthenticationRequired(QNetworkRepl y *reply, QAuthenticator *aut)
{
qDebug() << "Ha";
//aut->setUser("Username");
//aut->setPassword("Password");
}

void HTTPClass::slotProxyAuthenticationRequired(QNetwor kProxy proxy, QAuthenticator *aut)
{

}

void HTTPClass::slotNetworkSessionConnected()
{
qDebug() << "Connected";
}

void HTTPClass::slotReadyRead()
{
reply->readAll();
}

void HTTPClass::slotError(QNetworkReply::NetworkError error)
{
qDebug() << QString::number(error);
}

void HTTPClass::slotSslErrors(QList<QSslError> errors)
{
qDebug() << errors[0].errorString();
}


Everything I get is 3 blanck answers. ("" "" "") What am I doing wrong? I know how to download a file from the web, but not how to read data from a url.

--
My second question: actually I don't know how the information is in the link I actually want to connect with. It must be some kind of server, but not if its an sql server with MySQL or FTP something else. How could I know how the information is stored in the link I want to connect with? (which by the way: sharkbuss.banifinvest.com.br , gate 80, parameter: /BI3S2/get.aspx)
--
My third question: in order to connect myself with this link, I now I'll have to give a password and username. Where exactly in my code should I put this part?


Thanks,

Momergil

anda_skoa
25th December 2012, 18:50
Everything I get is 3 blanck answers. ("" "" "") What am I doing wrong? I know how to download a file from the web, but not how to read data from a url.


QNetworkReply is a QIODevice subclass, so you use the QIODevice API to read data from the reply object.

You can either do that as it comes in (your slotReadyRead() does that) or read at the end of the transmission (your slotReplyFinished() does that).

For example if you wanted to download into a file, you would also open a file and write the data you get from the readAll() calls into it.
You could also append it to a QByteArray member, write into a QBuffer object or process it right away.



My second question: actually I don't know how the information is in the link I actually want to connect with. It must be some kind of server, but not if its an sql server with MySQL or FTP something else. How could I know how the information is stored in the link I want to connect with? (which by the way: sharkbuss.banifinvest.com.br , gate 80, parameter: /BI3S2/get.aspx)


I am not sure what you mean but the network reply will also have the HTTP headers sent by the server, e.g. the content MIME type.



My third question: in order to connect myself with this link, I now I'll have to give a password and username. Where exactly in my code should I put this part?


That depends on how the service expects authentication data. It could be encoded into the URL, passed as part of headers in QNetworkRequest or provided on-demand in slotAuthenticationRequired().

Cheers,
_

Momergil
26th December 2012, 02:30
I am not sure what you mean but the network reply will also have the HTTP headers sent by the server, e.g. the content MIME type.

I mean that while I know the link, password, etc.. I have no idea just yet about how the information I want is stored on that server - it's no mine, it wasn't me who created it. So different from a SQL server where I would call a function to know all tables avaliable, and all headers of thoose tables, I just don't know how data is stored on that http server as well as I don't know how to know that :)

But you mentioned about headers; what exactly is that?

anda_skoa
26th December 2012, 21:46
I mean that while I know the link, password, etc.. I have no idea just yet about how the information I want is stored on that server - it's no mine, it wasn't me who created it.

One usually never knows that, it is an implementation detail of the server.

Like a web browser not caring at all how a web server creates its content. The browser sends a GET request for a given URL and the web server returns either an error code or success and the content.
It is of no concern to the web browser if the content had been stored in a file, read from a database or created on the fly.

If the server you are talking to is a web service, they'll have a description of their API. If it is just a normal web server, it will respond with the document your GET calls request.


But you mentioned about headers; what exactly is that?

HTTP headers. Meta data used in communication between an HTTP client and an HTTP server and vice versa.
Things like HTTP version, supported compression types, content MIME type, etc.

Qt has support for sending those in QNetworkRequest and for retrieving them in QNetworkReply

Cheers,
_

Momergil
4th January 2013, 13:47
Ok, thanks!

Since I'm not sure anymore if I will need to do this HTTP connection, I will stop my questions here.

Thanks,

Happy new year,

Momergil