PDA

View Full Version : QUrl with percent signs Issue



K_rol
22nd January 2014, 01:13
Hello everyone

Just started QML a few days ago and I am a bit confused.

I am trying to send a HTTP request to a webserver through QNetworkRequest.

My QUrl should have this: http://127.0.0.1/subdomain/PW?t=Someword&p=%01
But it seems that it changes it all the time to this: http://127.0.0.1/subdomain/PW?t=Someword&p=%2501

How would I get QUrl to not change the percent sign and keep it as it is?

I tried the below without success

QUrl url = QUrl::fromEncoded("http%3A//127.0.0.1/subdomain/PW%3Ft%3DSomeword%26p%3D%2501");

Any help would be appreciated
thanks

ChrisW67
22nd January 2014, 02:23
Works fine here with Qt 5.2:


QUrl url1("http://127.0.0.1/subdomain/PW?t=Someword&p=%01");
QUrl url2 = QUrl::fromEncoded("http://127.0.0.1/subdomain/PW?t=Someword&p=%01");
qDebug() << url1.toString(QUrl::FullyEncoded);
qDebug() << url2.toString(QUrl::FullyEncoded);

/* Output
"http://127.0.0.1/subdomain/PW?t=Someword&p=%01"
"http://127.0.0.1/subdomain/PW?t=Someword&p=%01"
*/


If the "01" following "%" is not the digits 0 and 1, for example the letters o and l, then the percent sign will be corrected because it must be followed by two hex digits. See the QUrl doc about ParseMode::Tolerant.

K_rol
22nd January 2014, 17:26
Thanks for you reply

Not sure of what I could be doing wrong... it always only shows "http://127.0.0.1/subdomain/PW?t=Someword&p=%2501"


QUrl url1("http://127.0.0.1/subdomain/PW?t=Someword&p=%01");
QString urlString1; urlString1 = url1.toEncoded();
std::cout << qPrintable(urlString1);


I noticed QUrl::FullyEncoded is only in QT5.

Could QT4 has a different take on the percent sign ? Seems unlikely to me :\

K_rol
21st February 2014, 05:32
Got it to work!
Just for future references to anyone else, someone pointed out to me the below codes:

QByteArray rawQuery("t=Someword&p=%01");
QUrl command("http://127.0.0.1/subdomain/letters");
command.setEncodedQuery(rawQuery);
std::cout << command.toEncoded().data();
Result "http://127.0.0.1/subdomain/letters?t=Someword&p=%01"


Explanation from qurl.html#setEncodedQuery



Sets the query string of the URL to query. The string is inserted as-is, and no further encoding is performed when calling toEncoded().
This function is useful if you need to pass a query string that does not fit into the key-value pattern, or that uses a different scheme for encoding special characters than what is suggested by QUrl.
Passing a value of QByteArray() to query (a null QByteArray) unsets the query completely. However, passing a value of QByteArray("") will set the query to an empty value, as if the original URL had a lone "?".