title: QT HTTP Post issue when server requires cookies
body: <p>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.</p>
  
  <p>I am trying to do HTTP post in QT C++ &amp; I have already tried doing that in python <strong>(My question is not a python question, so Qt pros please help</strong>).. I know, I am somewhere wrong in handling cookies and all, so please help. Please provide probable solutions.</p>
  
  <p>In python, code is clean and simple. I have stripped error handling and all extra things to make it simple.</p>
  
  <pre><code>url = 'http://www.example.com/'
  data = 'username=abc&amp;password=passwd'
  cj = cookielib.CookieJar()
  opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  usock = opener.open(url, data)
  #&gt;&gt;&gt;&gt;&gt;&gt; NOW, I have the cookiejar &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
  
  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')]
  data_to_send = 'ABCDEFGH'
  url_send = "http://www.example.com/xyz.php"
  send = opener.open(url_send,data_to_send)
  </code></pre>
  
  <p>The QT Equivalent I made:-</p>
  
  <pre><code>void SmsSender::sendToMyCantos()
  {
      manager = new QNetworkAccessManager(this);
      manager-&gt;setCookieJar(new QNetworkCookieJar(manager));
      connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));
      request.setUrl(QUrl("http://www.mycantos.com"));
      postData.append("username=abc&amp;password=passwd");
      manager-&gt;post(request,postData);
      //&gt;&gt;&gt;&gt;&gt;&gt; So, I feel that I have CookieJar now to make POST &lt;&lt;&lt;&lt;&lt;&lt;&lt;
  
      request.setRawHeader("Referer","http://www.example.com/xyz.php");
      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");
  
      postData.clear();
      postData.append("ABCDEFGH");
      request.setUrl(QUrl("http://www.example.com/xyz.php"));
  
      manager-&gt;post(request,postData);
  }
  </code></pre>
  
  <p>Now the issue is that I am not able to do the same in QT. Problems I am facing:</p>
  
  <ol>
  <li>Handling cookies</li>
  <li>Handling redirects (HTTP 302)</li>
  <li>Retaining cookies to make future POST</li>
  </ol>
  
  <p>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 <a href="http://wiki.forum.nokia.com/index.php/CS001432_-_Handling_an_HTTP_redirect_with_QNetworkAccessManager" rel="noreferrer">very similar to the link I used to make it..</a></p>
  
  <pre><code>QUrl SmsSender::redirectUrl(const QUrl&amp; possibleRedirectUrl,
                                 const QUrl&amp; oldRedirectUrl) const {
          //Checking infinite resursions
          QUrl redirectUrl;
          if(!possibleRedirectUrl.isEmpty() &amp;&amp;
             possibleRedirectUrl != oldRedirectUrl) {
                  redirectUrl = possibleRedirectUrl;
          }
          return redirectUrl;
  }
  
  void SmsSender::replyFinished(QNetworkReply *reply)
  {
      QVariant possibleRedirectUrl =
               reply-&gt;attribute(QNetworkRequest::RedirectionTargetAttribute);
      QVariant data_size = reply-&gt;header(QNetworkRequest::ContentLengthHeader);
      qDebug()&lt;&lt;data_size.toFloat();
      qDebug()&lt;&lt;manager-&gt;cookieJar()-&gt;cookiesForUrl(QUrl("http://www.example.com"));
  
      /* We'll deduct if the redirection is valid in the redirectUrl function */
      _urlRedirectedTo = this-&gt;redirectUrl(possibleRedirectUrl.toUrl(),
                                           _urlRedirectedTo);
  
      /* If the URL is not empty, we're being redirected. */
      if(!_urlRedirectedTo.isEmpty()) {
              QString text = QString("SmsSender::replyFinished: Redirected to ")
                                    .append(_urlRedirectedTo.toString());
      qDebug(text.toAscii());
  
      // Do again in case we have more redirections
  
      this-&gt;_qnam-&gt;get(QNetworkRequest(_urlRedirectedTo));
      }
      else
      {
          QString text = QString("SmsSender::replyFinished: Arrived to ")
                                    .append(reply-&gt;url().toString());
          qDebug(text.toAscii());
          _urlRedirectedTo.clear();
      }
  
  }
  
  QNetworkAccessManager* SmsSender::createQNAM() {
          QNetworkAccessManager* qnam = new QNetworkAccessManager(this);
          /* We'll handle the finished reply in replyFinished */
          connect(qnam, SIGNAL(finished(QNetworkReply*)),
                  this, SLOT(replyFinished(QNetworkReply*)));
          return qnam;
  }
  </code></pre>