C++ MFC实现urlencode
最近用MFC写了个发送网页请求的socket,其中GET发送的内容需要urlencode,我以前写PHP一个函数就搞定了,据说C++要写好长。谁能提供一段进行url编码的代码,万分感谢。
最近用MFC写了个发送网页请求的socket,其中GET发送的内容需要urlencode,我以前写PHP一个函数就搞定了,据说C++要写好长。谁能提供一段进行url编码的代码,万分感谢。
答案:static CString URLDecode(CString sIn)
{
CString sOut;
const int nLen = sIn.GetLength() + 1;
register LPBYTE pOutTmp = NULL;
LPBYTE pOutBuf = NULL;
register LPBYTE pInTmp = NULL;
LPBYTE pInBuf =(LPBYTE)sIn.GetBuffer(nLen);
//alloc out buffer
pOutBuf = (LPBYTE)sOut.GetBuffer(nLen);
if(pOutBuf)
{
pInTmp = pInBuf;
pOutTmp = pOutBuf;
// do encoding
while (*pInTmp)
{
if('%'==*pInTmp)
{
pInTmp++;
*pOutTmp++ = (toByte(*pInTmp)%16<<4) + toByte(*(pInTmp+1))%16;//高4位+低4位
pInTmp++;
}
else if('+'==*pInTmp)
*pOutTmp++ = ' ';
else
*pOutTmp++ = *pInTmp;
pInTmp++;
}
*pOutTmp = '\0';
}
sIn.ReleaseBuffer();
return sOut;
}
static string URLEncode(const string sIn)
{
map<char,string> unsafeCharMap;
unsafeCharMap.insert(std::pair<char, string>('\"',"%22"));
unsafeCharMap.insert(std::pair<char, string>('<',"%3C"));
unsafeCharMap.insert(std::pair<char, string>('>',"%3E"));
unsafeCharMap.insert(std::pair<char, string>('#',"%23"));
unsafeCharMap.insert(std::pair<char, string>('%',"%25"));
unsafeCharMap.insert(std::pair<char, string>('{',"%7B"));
unsafeCharMap.insert(std::pair<char, string>('}',"%7D"));
unsafeCharMap.insert(std::pair<char, string>('|',"%7C"));
unsafeCharMap.insert(std::pair<char, string>('\\',"%5C"));
unsafeCharMap.insert(std::pair<char, string>('^',"%5E"));
unsafeCharMap.insert(std::pair<char, string>('~',"%7E"));
unsafeCharMap.insert(std::pair<char, string>('[',"%5B"));
unsafeCharMap.insert(std::pair<char, string>(']',"%5D"));
unsafeCharMap.insert(std::pair<char, string>('`',"%60"));
unsafeCharMap.insert(std::pair<char, string>('$',"%24"));
unsafeCharMap.insert(std::pair<char, string>('&',"%26"));
unsafeCharMap.insert(std::pair<char, string>('+',"%2B"));
unsafeCharMap.insert(std::pair<char, string>(',',"%2C"));
unsafeCharMap.insert(std::pair<char, string>('/',"%2F"));
unsafeCharMap.insert(std::pair<char, string>(':',"%3A"));
unsafeCharMap.insert(std::pair<char, string>(';',"%3B"));
unsafeCharMap.insert(std::pair<char, string>('=',"%3D"));
unsafeCharMap.insert(std::pair<char, string>('?',"%3F"));
unsafeCharMap.insert(std::pair<char, string>('@',"%40"));
string sOut;
for( size_t ix = 0; ix < sIn.size(); ix++ )
{
map<char, string>::iterator item = unsafeCharMap.find(sIn[ix]);
if (item != unsafeCharMap.end())
{
sOut += item->second;
continue;
}
BYTE buf[4];
memset( buf, 0, 4 );
if( isalnum( (BYTE)sIn[ix] ) )
{
buf[0] = sIn[ix];
}
else if ( sIn[ix] == '\n' )
{
sOut += "\n";
continue;
}
else if ( isspace( (BYTE)sIn[ix] ) )
{
buf[0] = '+';
}
else
{
buf[0] = '%';
buf[1] = toHex( (BYTE)sIn[ix] >> 4 );
buf[2] = toHex( (BYTE)sIn[ix] % 16);
}
sOut += (char *)buf;
}
sOut = Replace(sOut, "%E0%82%B7", "%c2%b7");
sOut = Replace(sOut, "%E0%83%97", "%c3%97");
return sOut;
};
上一个:请教,怎么能让php页面不去等待system所调用的命令的返回结果?
下一个:php中为什么可以用$row=mysql_fetch_array($query)作为while条件循环出mysql数据库的表?