c++ urlencode使用注意事项

#include <cctype>
#include <iomanip>
#include <sstream>
#include <string>
#include <iostream>
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();
}

int main()
{
    cout << url_encode("空间") ;
    return 0;
 } 

源码来自于网上,但是有一点需要注意编码问题,文本为gbk和utf-8编码出来的内容不一样,这儿纠结了很久,网上也查不到答案。

猜你喜欢

转载自blog.csdn.net/a909204013/article/details/81098921