PDA

View Full Version : Http request encoding



Trok
29th September 2011, 22:44
Hi, i have difficulty with encoding http request by function toEncoded(), it encode only some characters.
It's my code:


request.setUrl(QUrl(sUrl));
QUrl postData;
postData.addQueryItem("ctl00$MainContent$txtHashes", lHashes);
postData.addQueryItem("ctl00$MainContent$txtCaptcha", sCaptcha);
postData.addQueryItem("__VIEWSTATE", this->sViewState);
postData.addQueryItem("__EVENTVALIDATION", this->sEventValidation);
postData.addQueryItem("ctl00$MainContent$btnDecrypt", " Decrypt Hashes ");
reply = manager->post(request, postData.toEncoded());

When return the postData.toEncoded(), it doeasn't change some characters, for example '/' or '$'. I don't know why this function doesn't work correctly.
In result i receive error source page:

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/error.aspx?aspxerrorpath=/sha1-decrypt.aspx">here</a>.</h2>
</body></html>
I wait for some idea to resolve my problem.

wysota
29th September 2011, 23:29
The function works correctly. You're just incorrectly assuming you should be using it for the purpose you are using it for. Maybe you wanted to use toPercentEncoding()?

ChrisW67
30th September 2011, 00:15
The error response from the upstream server is of precisely no use. Without an actual example of the data you claim is mis-encoded and how you are handling it we can just guess what is going wrong.

According to RFC 3986 (http://www.rfc-editor.org/rfc/rfc3986.txt), which QUrl implements, the following characters are permitted in a query string component:


query = *( pchar / "/" / "?" )
pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="

and then Section 2.4 kicks in:


Under normal circumstances, the only time when octets within a URI
are percent-encoded is during the process of producing the URI from
its component parts. This is when an implementation determines which
of the reserved characters are to be used as subcomponent delimiters
and which can be safely used as data.

For URL query strings only the '&' and '=' are used as a sub-component delimiters and therefore must be encoded if part of the data. So '$', '/', and most other characters from the "reserved" set are perfectly fine unencoded in this context.

If you wish to do some funky encoding of your own then feel free. You can use QUrl::addEncodedQueryItem() to put these into the QUrl.