2012-11-13 39 views
0

我们的应用程序是一个32位应用程序。当它安装在Windows 7 64位时,通常安装在“C:\ Program Files(x86)”上,而不是“C:\ Program Files”。我们正在构建基于安装位置的Url,并将其作为Web服务的一部分传递给用户。我们正在构建这样的网址:QUrl contains括号

ppmPath = "http://" + ipAddress + ":13007/" + folder + ".ppm" + "?filePath=" 
      + applicationDirPath + "/" + FIRMWARE; 
QUrl ppmURL(ppmPath, QUrl::TolerantMode); 
ppmPath = QString(ppmURL.toEncoded()); 

变量类型和含义通常都是这样。由于用于Windows 7 64位的“applicationDirPath”包含一个右括号“)” - “(x86)”子字符串中 - 显然该URL已损坏。如果我们将它安装到任何其他位置,即使该位置具有任何其他特殊字符,它也能正常工作。

如何处理URL中的“)”字符,这样就不会被破坏?

回答

2

the documentation它看起来不像圆括号由QUrl自动编码,即使在宽容模式下。如果您首先将您的网址封装在QString中,然后用“%28”替换所有(字符,并用“%29”替换所有)字符,那么它应该像您期望的那样行事。

QString ppmPath = QString("http://" + ipAddress + ":13007/" + folder + ".ppm" + "?filePath=" 
      + applicationDirPath + "/" + FIRMWARE); 
QUrl ppmURL(ppmPath, QUrl::TolerantMode); 
ppmPath = QString(ppmURL.toEncoded()); 
ppmPath.replace(QChar('('), "%%28"); 
ppmPath.replace(QChar(')'), "%%29"); 

我不是100%确定double-%需要在那里,但我记得过去遇到过麻烦。尝试两种方式。

或者,您可以尝试玩QUrl::toPercentEncoding()并跳过构造函数。它似乎将括号转换。

QUrl ppmURL(QString("http://" + ipAddress + ":13007/" + folder + ".ppm"), QUrl::TolerantMode); 
QString filepath = QUrl::toPercentEncoding(applicationDirPath + "/" + FIRMWARE); 
ppmUrl.addEncodedQueryItem("filepath", filepath.toLocal8Bit()); 
ppmPath = QString(ppmURL.toEncoded());