在C中编码/解码URL
作者:互联网
有谁知道这样做的任何好的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();
}
解码功能的实现留给读者练习. :P
标签:urldecode,c,urlencode,percent-encoding 来源: https://codeday.me/bug/20190916/1806643.html