Background of the Issue
When sending form data using HTTP POST, if the data is sent as a string connected by <span>&</span> (i.e., in <span>application/x-www-form-urlencoded</span> format), there often occurs a garbled text issue when the data contains Chinese characters. This is because Chinese characters are non-ASCII characters and need to be properly encoded to be transmitted correctly over HTTP.
Solution
In Qt, you can use the <span>QString::toUtf8().toPercentEncoding()</span> method to perform URL encoding (also known as percent encoding) on Chinese characters. This encoding method converts non-ASCII characters and special characters into the form of <span>%</span> followed by two hexadecimal digits.
Code Example
QString content = "测试中文";
QString note = content.toUtf8().toPercentEncoding();
Principle Analysis
-
<span>toUtf8()</span>: First, it converts the QString to a UTF-8 encoded QByteArray. UTF-8 is the most commonly used Unicode encoding on the internet and can represent all Unicode characters. -
<span>toPercentEncoding()</span>: Then, it performs percent encoding on the UTF-8 byte sequence. For example:
- The UTF-8 encoded bytes for “测试中文” are:
<span>\xE6\xB5\x8B\xE8\xAF\x95\xE4\xB8\xAD\xE6\x96\x87</span> - After percent encoding, it becomes:
<span>"%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87"</span>
Practical Application
When constructing the POST request body, all parameter values that may contain non-ASCII characters should be encoded:
QString name = "张三";
QString address = "北京市海淀区";
QString postData = QString("name=%1&address=%2")
.arg(name.toUtf8().toPercentEncoding())
.arg(address.toUtf8().toPercentEncoding());
// postData will be: "name=%E5%BC%A0%E4%B8%89&address=%E5%8C%97%E4%BA%AC%E5%B8%82%E6%B5%B7%E6%B7%80%E5%8C%BA"
Notes
-
Only the parameter values need to be encoded; parameter names usually contain only ASCII characters and do not need encoding.
-
Spaces in URL encoding are typically encoded as
<span>+</span>or<span>%20</span>, and Qt’s<span>toPercentEncoding()</span>will default to encoding as<span>%20</span>. -
If you need to preserve certain characters from being encoded, you can pass parameters to
<span>toPercentEncoding()</span>, for example:// Preserve the slash from being encoded content.toUtf8().toPercentEncoding("/"); -
The server will automatically perform URL decoding after receiving the data, so no manual handling is required.
By using this method, you can ensure that Chinese characters are transmitted correctly in HTTP POST requests, avoiding garbled text issues.