0

I am working on a Qt application in which I would like to retrieve objects which are tagged @Secured in the Spring-MVC based web-application. I have written the code on the server side for REST authentication and if I use the 'curl' tool, then I am able to access secure services on the server. I would like to know if there is anyway I can replicate the way I am accessing secured services in a QT application. Any help would be nice. Thanks a lot.

curl code :

curl -i -X POST -d j_username=email@email.de -d j_password=password -c /home/username/cookies.txt http://localhost:8080/j_spring_security_check 

And for using the session-id to access secure services, I do :

curl -i --header "Accept:application/json" -X GET -b /home/cookies.txt http://localhost:8080/secure_service_method

How can I achieve the same thing in QT. Any help would be nice. Please note, I am not that of an expert in Qt, so please be patient. Thanks a lot.

We are Borg
  • 5,117
  • 17
  • 102
  • 225
  • CURL has a `C` API http://curl.haxx.se/libcurl/c/ (libcurl) – Galik Apr 15 '15 at 14:02
  • @Galik But I am looking for something Cpp, not in C. I don't know if that can be used that way, my apologies I don't know that much. Can you please give me some sample for getting methods from server using curl? – We are Borg Apr 15 '15 at 14:05
  • Judging from your curl command you probably just need to send a [POST request using Qt](http://doc.qt.io/qt-5/qnetworkaccessmanager.html#post). Have a look at http://stackoverflow.com/questions/2599423/how-can-i-post-data-to-a-url-using-qnetworkaccessmanager, use Wireshark to find out how your POST request has to look like exactly and build the same request using Qt. – m.s. Apr 15 '15 at 14:10
  • @m.s. : Thank you for those links, I will surely try them out, just one thing, when I get the reply back from curl, the cookie file looks like on this link, http://pastebin.com/a9dug4CB .The only interesting data is the session-id which I will need for subsequent requests, how can I sort that in QT to get only the session-id. Thanks a lot. :-) – We are Borg Apr 15 '15 at 14:24
  • Use a [QNetworkCookieJar](http://doc.qt.io/qt-5/qnetworkaccessmanager.html#setCookieJar). – m.s. Apr 15 '15 at 14:26
  • @m.s. : I believe I have everything I would require, thanks a lot. If you could just make an answer(some sample code wouldn't hurt.. ;-) ) , I would mark your answer. Thanks a lot. :-) – We are Borg Apr 15 '15 at 14:28

1 Answers1

2

Converting my comments into this answer:

Judging from your curl command you probably just need to send a POST request and handle the cookies using Qt . Qt provides everything necessary to accomplish this task, have a look at QNetworkAccessManager::post and QNetworkAccessManager::setCookieJar.

The basic steps you need to implement will look like this:

1.) Setup:

QNetworkAccessManager* manager = new QNetworkAccessManager(this);
manager->setCookieJar(new QNetworkCookieJar(manager));
connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));

2.) Request the cookie

QNetworkRequest request;
request.setUrl(QUrl("http://localhost:8080/j_spring_security_check"));
QByteArray postData;
postData.append("j_username=email@email.de&j_password=password");
manager->post(request,postData);

3.) Use the manager object which then has the cookie to request the protected files.

m.s.
  • 16,063
  • 7
  • 53
  • 88
  • Thanks a lot. I have done most of the stuff above, I just have to use the postData.append and use the manager part. Have a nice day ahead. – We are Borg Apr 15 '15 at 14:42
  • Hello my friend, I noticed a problem when I am sending data, the username is blank. Can you please tell me why that might be happening. Thanks a lot. – We are Borg Apr 15 '15 at 15:25
  • @WeareBorg: you might need to [urlencode](http://doc.qt.io/qt-5.4/qurl.html#toPercentEncoding) the username to `email%40email.de`; how does the request look like in Wireshark? – m.s. Apr 15 '15 at 16:10
  • I don't know how to use wireshark, I have already posted a new question, can you check that out. Thanks a lot. Question link : http://stackoverflow.com/questions/29668696/qt-qnetworkrequest-sends-blank-username-for-remote-authentication – We are Borg Apr 16 '15 at 07:59