• 3291阅读
  • 1回复

如何不要转义URL中的特殊字符 [复制链接]

上一主题 下一主题
离线yearl
 

只看楼主 倒序阅读 楼主  发表于: 2015-09-24
按规范,URL中的特殊字符需要被转义,包括(^)。QUrl忠实执行,所有的特殊字符都被强制转义了。
但是问题来了。
有个网站,其URL中包含有^,用IE等浏览器可正常访问,没被转义。用QWebKit就不行了,^被转义成了%5E,无法访问这个链接了。
怎么办?
没有办法要求QUrl不要转义^这个字符?

space / double-quote / "<" / ">" / "" / "^" / "`" / "{" / "|" / "}"


Constant    Value    Description
QUrl::TolerantMode    0    QUrl will try to correct some common errors in URLs. This mode is useful for parsing URLs coming from sources not known to be strictly standards-conforming.
QUrl::StrictMode    1    Only valid URLs are accepted. This mode is useful for general URL validation.
QUrl::DecodedMode    2    QUrl will interpret the URL component in the fully-decoded form, where percent characters stand for themselves, not as the beginning of a percent-encoded sequence. This mode is only valid for the setters setting components of a URL; it is not permitted in the QUrl constructor, in fromEncoded() or in setUrl(). For more information on this mode, see the documentation for QUrl::FullyDecoded.
离线stlcours

只看该作者 1楼 发表于: 2015-09-28
QByteArray QUrl::toPercentEncoding(const QString & input, const QByteArray & exclude = QByteArray(), const QByteArray & include = QByteArray())

Returns an encoded copy of input. input is first converted to UTF-8, and all ASCII-characters that are not in the unreserved group are percent encoded. To prevent characters from being percent encoded pass them to exclude. To force characters to be percent encoded pass them to include.

Unreserved is defined as: ALPHA / DIGIT / "-" / "." / "_" / "~"

QByteArray ba = QUrl::toPercentEncoding("{a fishy string?}", "{}", "s");
qDebug(ba.constData());
// prints "{a fi%73hy %73tring%3F}"
快速回复
限100 字节
 
上一个 下一个