PDA

View Full Version : QNetworkAccessManager Http Basic Authentication?



jloundy
28th December 2010, 19:51
Hi, I am trying to send HTTP commands to some hardware(the server) which requries authentication before I can communicate. I left visual studio and converted my code to QT because I was sure it would be much easier to accomplish, but I am having problem just getting started.

1) Should I just use QTcpSockets and try to authenticate them with my username and password, or stick with QNetworkAccessManager
2) How do I even use QNetworkAccessManager to send out a string to a server? And how do I set up my port Number and hostname when using QNetworkAccessManager?

Any help is greatly appeciated, I would post some code but I mean im confused on just how to start using QNetworkAccessManager

-Thanks

wysota
28th December 2010, 20:03
2) How do I even use QNetworkAccessManager to send out a string to a server?
Most likely using QNetworkAccessManager::get() or QNetworkAccessManager::post().


And how do I set up my port Number and hostname when using QNetworkAccessManager?
Place them as part of the URL you pass to get() or post().

javimoya
28th December 2010, 22:12
example:
QNetworkRequest * nr = new QNetworkRequest(QUrl("https://www.whatever.com/"));
nr->setRawHeader("Authorization", "Basic " + QByteArray(QString("%1:%2").arg("username").arg("password").toAscii()).toBase64());

jloundy
28th December 2010, 23:54
Thank you guys for the help. Basically the hardware I am working with is controlled by http commands sent through a web browser. I am trying to bypass the webbrowser and send them straight to the hardware. Im getting a 401 unauthorized error but i connected the signal and slot to handle the authentication process. Do you guys have any ideas?

javimoya
29th December 2010, 01:13
sorry... i don't understand you....
maybe you are not encoding correctly the request??

anyway.... you can use a http debugger (like Fiddler -it's free-) and mimic the same requests... (maybe you must set up an user-agent... or something like that)

wysota
29th December 2010, 01:19
Im getting a 401 unauthorized error but i connected the signal and slot to handle the authentication process.
Show us contents of the slot connected to the authenticationRequired() signal.