I have been trying this whole day with no success. Please help in solving the issue. On googling I found many users had this issue but nowhere I could find a solution.

I am trying to do HTTP post in QT C++ & I have already tried doing that in python (My question is not a python question, so Qt pros please help).. I know, I am somewhere wrong in handling cookies and all, so please help. Please provide probable solutions.

In python, code is clean and simple. I have stripped error handling and all extra things to make it simple.

Qt Code:
  1. url = 'http://www.example.com/'
  2. data = 'username=abc&password=passwd'
  3. cj = cookielib.CookieJar()
  4. opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  5. usock = opener.open(url, data)
  6. #>>>>>> NOW, I have the cookiejar <<<<<<<<<
  7.  
  8. opener.addheaders = [('Referer','http://www.example.com/xyz.php'),('User-Agent','Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.3) Gecko/20091020 Ubuntu/9.10 (karmic) Firefox/3.5.3 GTB7.0')]
  9. data_to_send = 'ABCDEFGH'
  10. url_send = "http://www.example.com/xyz.php"
  11. send = opener.open(url_send,data_to_send)
To copy to clipboard, switch view to plain text mode 

The Qt equivalent:-

Qt Code:
  1. void SmsSender::sendToMyCantos()
  2. {
  3. manager = new QNetworkAccessManager(this);
  4. manager->setCookieJar(new QNetworkCookieJar(manager));
  5. connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));
  6. request.setUrl(QUrl("http://www.mycantos.com"));
  7. postData.append("username=abc&password=passwd");
  8. manager->post(request,postData);
  9. //>>>>>> So, I feel that I have CookieJar now to make POST <<<<<<<
  10.  
  11. request.setRawHeader("Referer","http://www.example.com/xyz.php");
  12. request.setRawHeader("User-Agent","Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.3) Gecko/20091020 Ubuntu/9.10 (karmic) Firefox/3.5.3 GTB7.0");
  13.  
  14. postData.clear();
  15. postData.append("ABCDEFGH");
  16. request.setUrl(QUrl("http://www.example.com/xyz.php"));
  17.  
  18. manager->post(request,postData);
  19. }
To copy to clipboard, switch view to plain text mode 

Now the issue is that I am not able to do the same in QT. Problems I am facing:

  1. Handling cookies
  2. Handling redirects (HTTP 302)
  3. Retaining cookies to make future POST

All this is done automagically in python. Below, the code is not directly related, but I coded this to allow redirects in the POST.. The code is very similar to the link I used to make it..

Qt Code:
  1. QUrl SmsSender::redirectUrl(const QUrl& possibleRedirectUrl,
  2. const QUrl& oldRedirectUrl) const {
  3. //Checking infinite resursions
  4. QUrl redirectUrl;
  5. if(!possibleRedirectUrl.isEmpty() &&
  6. possibleRedirectUrl != oldRedirectUrl) {
  7. redirectUrl = possibleRedirectUrl;
  8. }
  9. return redirectUrl;
  10. }
  11.  
  12. void SmsSender::replyFinished(QNetworkReply *reply)
  13. {
  14. QVariant possibleRedirectUrl =
  15. reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
  16. QVariant data_size = reply->header(QNetworkRequest::ContentLengthHeader);
  17. qDebug()<<data_size.toFloat();
  18. qDebug()<<manager->cookieJar()->cookiesForUrl(QUrl("http://www.example.com"));
  19.  
  20. /* We'll deduct if the redirection is valid in the redirectUrl function */
  21. _urlRedirectedTo = this->redirectUrl(possibleRedirectUrl.toUrl(),
  22. _urlRedirectedTo);
  23.  
  24. /* If the URL is not empty, we're being redirected. */
  25. if(!_urlRedirectedTo.isEmpty()) {
  26. QString text = QString("SmsSender::replyFinished: Redirected to ")
  27. .append(_urlRedirectedTo.toString());
  28. qDebug(text.toAscii());
  29.  
  30. // Do again in case we have more redirections
  31.  
  32. this->_qnam->get(QNetworkRequest(_urlRedirectedTo));
  33. }
  34. else
  35. {
  36. QString text = QString("SmsSender::replyFinished: Arrived to ")
  37. .append(reply->url().toString());
  38. qDebug(text.toAscii());
  39. _urlRedirectedTo.clear();
  40. }
  41.  
  42. }
  43.  
  44. QNetworkAccessManager* SmsSender::createQNAM() {
  45. QNetworkAccessManager* qnam = new QNetworkAccessManager(this);
  46. /* We'll handle the finished reply in replyFinished */
  47. connect(qnam, SIGNAL(finished(QNetworkReply*)),
  48. this, SLOT(replyFinished(QNetworkReply*)));
  49. return qnam;
  50. }
To copy to clipboard, switch view to plain text mode