【问题标题】:Qt QUrl: doesn't url-encode colonQt QUrl:不对冒号进行url编码
【发布时间】:2013-06-13 18:38:52
【问题描述】:

我有网址:https://example.com/hello?param=first:last。我希望它应该被百分比编码为https://example.com/hello?param=first%3Alast。但 Qt 保持原样。我的代码:

QUrl url("https://example.com/hello?param=first:last");
printf("Encoded: %s\n", url.toEncoded().constData());

我应该如何编码冒号?使用QString::toPercentEncoding手动格式化参数?

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

    由于安全问题,您必须替换冒号。

    更多信息:http://www.blooberry.com/indexdot/html/topics/urlencoding.htm

    您可以对冒号使用百分比编码(“:”->“%3A”),请参阅http://qt-project.org/doc/qt-5.0/qtcore/qurl.html#fromPercentEncodinghttp://qt-project.org/doc/qt-5.0/qtcore/qurl.html#toPercentEncoding

    【讨论】:

    • 这些安全原因是什么?
    【解决方案2】:

    关于 URL 中的冒号安全性已经进行了一些讨论。听起来这归结为我不精通的 RFC 遵守情况。

    Is a colon safe for friendly-URL use?

    看来您可能必须自己替换任何“:”字符(在 https: 之后)。

    【讨论】:

      【解决方案3】:

      我认为 QUrl::toPercentEncoding() 是你最好的选择。它默认编码所有标准字符,您可以手动指定自己的附加字符列表进行编码:

      参考网址:
      http://doc.qt.digia.com/4.7/qurl.html#toPercentEncoding

      【讨论】:

      • 不幸的是,它会编码所有内容。 http:http://hello.com 中的事件冒号将是 http%3A%2F%2Fhello.com。那不是我想要的。我提到这个方法是手动编码参数。
      【解决方案4】:

      很确定你想要QUrl::setEncodedURLQUrl::toEncoded

      http://harmattan-dev.nokia.com/docs/platform-api-reference/xml/daily-docs/libqt4/qurl.html

      您使用的是哪个版本的 Qt?

      使用 IIRC Qt 3 QUrl:encode

      【讨论】:

      • QUrl::setEncodedUrl 输入中已经有百分比编码的字符串除外。但我有原始字符串。
      猜你喜欢
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 2012-04-21
      相关资源
      最近更新 更多