Question

I create 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 = "aemail@gmail.com";    
QUrlQuery qurlqr;    
qurlqr.addQueryItem(normalize(key), normalize(value));

QString result = qurlqr.toString();

The result that's be expecting is :

usermail=aemail%40gmail.com. 

But I received:

usermail=aemail@gmail.com

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

(I'm using Qt5 on Win7)

Was it helpful?

Solution

QUrlQuery's toString by default decodes the percent encoding. If you want the encoded version try:

qurlqr.toString(QUrl::FullyEncoded)

Also you don't need to manually encode the string by replacing characters; you could instead use QUrl::toEncoded() (I suggest you read the QUrlQuery documentation).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top