Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt, QUrl, QUrlQuery: Encoding special character in a query string

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 = "[email protected]";    
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:

[email protected]

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

(I'm using Qt5 on Win7)

like image 323
aviit Avatar asked May 10 '13 02:05

aviit


1 Answers

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).

like image 86
Sam Avatar answered Oct 02 '22 23:10

Sam