1

I have URL: https://example.com/hello?param=first:last. I expect that it should be percent-encoded as https://example.com/hello?param=first%3Alast. But Qt leaves it as-is. My code:

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

How should I encode colon? Manually format parameter with QString::toPercentEncoding?

demi
  • 5,384
  • 6
  • 37
  • 57

4 Answers4

2

You must replace colons because of security issues.

More information: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm

You can use percent encoding (":" -> "%3A") for colons, see http://qt-project.org/doc/qt-5.0/qtcore/qurl.html#fromPercentEncoding and http://qt-project.org/doc/qt-5.0/qtcore/qurl.html#toPercentEncoding.

user2448027
  • 1,628
  • 10
  • 11
0

There has been some discussion regarding colon safety in URLs. It sounds like it comes down to the RFC adherence which I'm not versed in.

Is a colon safe for friendly-URL use?

Looks like you may have to replace any ":" characters (after https:) yourself.

Community
  • 1
  • 1
Son-Huy Pham
  • 1,899
  • 18
  • 19
0

I think QUrl::toPercentEncoding() is your best bet. It encodes all standard characters by default, and you can manually specify your own list of additional characters to encode:

Reference URL:
http://doc.qt.digia.com/4.7/qurl.html#toPercentEncoding

ZalewaPL
  • 1,104
  • 1
  • 6
  • 14
  • Unfortunately it will encode everything. Event colons in http: `http://hello.com` will be `http%3A%2F%2Fhello.com`. That not i want. I mentioned this method as manually encoding parameters. – demi Jun 17 '13 at 19:48
-1

Pretty sure you want QUrl::setEncodedURL and QUrl::toEncoded

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

Which version of Qt are you using?

IIRC Qt 3 used QUrl:encode

echolocation
  • 1,120
  • 1
  • 10
  • 28
  • `QUrl::setEncodedUrl` excepts already percent-encoded string in the input. But I have raw string. – demi Jun 17 '13 at 18:08