[Qt-interest] weird problem with QNetworkAccessManager and posting-requests

Mandeep Sandhu mandeepsandhu.chd at gmail.com
Mon Apr 25 06:44:02 CEST 2011


> The code I use looks almost like this:
>
> netManager = new QNetworkAccessManager(this);
> connect(netManager, SIGNAL(finished(QNetworkReply*)), this,
> SLOT(replyFinished(QNetworkReply*)));
> QNetworkRequest request(QUrl("http://www.scherbenwelten.de/webpage.php"));
> request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT
> 6.1; de; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
> request.setRawHeader("Accept-Charset", "ISO-8859-1");
> request.setRawHeader("Accept", "text/html");
> request.setRawHeader("Accept-Language", "de-de");
> request.setRawHeader("Referer","http://www.scherbenwelten.de/webpagel.php");
> request.setAttribute(QNetworkRequest::CookieSaveControlAttribute,
> QVariant(true));
> request.setHeader(QNetworkRequest::ContentTypeHeader,
> QVariant("application/x-www-form-urlencoded"));
> QByteArray *user = SWCommunicator::wwwEncodeQString(username);
> QByteArray post("SCX=1680&"); // whatever?
> post.append("SCY=1050&"); // whatever?
> post.append("WSCX=1664&"); // whatever?
> post.append("WSCY=875&"); // whatever?
> post.append("FORM_USERNAME="+*user+"&");
> post.append("LOGON=Login&");
> post.append("NOREFRESH=1");
> request.setHeader(QNetworkRequest::ContentLengthHeader,
> QVariant(QString::number(post.length())));

I'm shooting in the dark here....but do you need to set the content
length explicitly?

I'm using the post method to send off a request using QT 4.6.2 on and
embedded system (and 4.7.1 on Desktop) and it works w/o setting the
content length. I guess QT would already be doing that for you.

Also, how about using a QUrl to make your post query string and then
converting it into a QByteArray (using QUrl::toEncoded())...that way
you're assured that the query string is created and encoded properly.

What QT version are you working with? Also, could this be your
webserver specific issue? Have your tried another webserver?

-mandeep



More information about the Qt-interest-old mailing list