有人知道有什么好的 C++ 代码可以做到这一点吗?
最佳答案
前几天我遇到了这个问题的一半编码。对可用选项不满意,查看 this C sample code 后,我决定推出自己的 C++ url-encode 函数:
#include <cctype>
#include <iomanip>
#include <sstream>
#include <string>
using namespace std;
string url_encode(const string &value) {
ostringstream escaped;
escaped.fill('0');
escaped << hex;
for (string::const_iterator i = value.begin(), n = value.end(); i != n; ++i) {
string::value_type c = (*i);
// Keep alphanumeric and other accepted characters intact
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
escaped << c;
continue;
}
// Any other characters are percent-encoded
escaped << uppercase;
escaped << '%' << setw(2) << int((unsigned char) c);
escaped << nouppercase;
}
return escaped.str();
}
decode 函数的实现留给读者作为练习。 :P
关于c++ - 在 C++ 中编码/解码 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/154536/