Qt, qurl, qurlquery: Special character is encoded in the query string

I created a URL query like this:

QString normalize(QString text) 
{
text.replace("%", "%25");
text.replace("@", "%40");
text.replace("'", "%27" );
text.replace("&", "%26");
text.replace(""", "%22");
text.replace("'", "%27");
text.replace(",", "%2C");
text.replace(" ", "%20");

return text;
}
QString key = "usermail";
QString value = "[email protected]";
QUrlQuery qurlqr;
qurlqr.addQueryItem(normalize( key), normalize(value));

QString result = qurlqr.toString();

The expected result is:

< pre>usermail=aemail%40gmail.com.

But I received:

[email protected]

I don’t know why. Can you help me?

(I use Qt5 on Win7)

By default, QUrlQuery’s toString decodes percentage encoding. If you want the encoded version, please try:

qurlqr.toString(Q Url::FullyEncoded)

You also don’t need to manually encode strings by replacing characters; you can use QUrl::toEncoded() instead (I suggest you read the QUrlQuery documentation).

< /div>

I created a URL query like this:

QString normalize(QString text) 
{
text.replace("%", "%25");
text.replace("@", "%40");
text.replace("'", "%27") ;
text.replace("&", "%26");
text.replace(""", "%22");
text.replace("'", " %27");
text.replace(",", "%2C");
text.replace(" ", "%20");

return text ;
}
QString key = "usermail";
QString value = "[email protected]";
QUrlQuery qurlqr;
qurlqr.addQueryItem(normalize(key ), normalize(value));

QString result = qurlqr.toString();

The expected result is:

usermail=aemail%40gmail.com.

But I received:

[email protected]

I Don’t know why. Can you help me?

(I use Qt5 on Win7)

By default, QUrlQuery’s toString decodes percentage encoding. If you want the encoded version , Please try:

qurlqr.toString(QUrl :: FullyEncoded)

You also don’t need to manually encode strings by replacing characters; you can use QUrl instead: : toEncoded() (I suggest you read the QUrlQuery documentation).

Leave a Comment

Your email address will not be published.