编码/解码C+中的URl+; 我如何在客户端编码URL并在服务器端解码相同的API。是否有内置API用于此目的。请任何人建议解决方案。我还想知道如何在C++中进行百分比编码? < p>您可以查看这个和
编码:编码/解码C+中的URl+; 我如何在客户端编码URL并在服务器端解码相同的API。是否有内置API用于此目的。请任何人建议解决方案。我还想知道如何在C++中进行百分比编码? < p>您可以查看这个和,c++,http,C++,Http,编码: std::string UriEncode(const std::string & sSrc) { const char DEC2HEX[16 + 1] = "0123456789ABCDEF"; const unsigned char * pSrc = (const unsigned char *)sSrc.c_str(); const int SRC_LEN = sSrc.length(); unsigned char * const pStart =
std::string UriEncode(const std::string & sSrc)
{
const char DEC2HEX[16 + 1] = "0123456789ABCDEF";
const unsigned char * pSrc = (const unsigned char *)sSrc.c_str();
const int SRC_LEN = sSrc.length();
unsigned char * const pStart = new unsigned char[SRC_LEN * 3];
unsigned char * pEnd = pStart;
const unsigned char * const SRC_END = pSrc + SRC_LEN;
for (; pSrc < SRC_END; ++pSrc)
{
if (SAFE[*pSrc])
*pEnd++ = *pSrc;
else
{
// escape this char
*pEnd++ = '%';
*pEnd++ = DEC2HEX[*pSrc >> 4];
*pEnd++ = DEC2HEX[*pSrc & 0x0F];
}
}
std::string sResult((char *)pStart, (char *)pEnd);
delete [] pStart;
return sResult;
}
std::string UriDecode(const std::string & sSrc)
{
// Note from RFC1630: "Sequences which start with a percent
// sign but are not followed by two hexadecimal characters
// (0-9, A-F) are reserved for future extension"
const unsigned char * pSrc = (const unsigned char *)sSrc.c_str();
const int SRC_LEN = sSrc.length();
const unsigned char * const SRC_END = pSrc + SRC_LEN;
// last decodable '%'
const unsigned char * const SRC_LAST_DEC = SRC_END - 2;
char * const pStart = new char[SRC_LEN];
char * pEnd = pStart;
while (pSrc < SRC_LAST_DEC)
{
if (*pSrc == '%')
{
char dec1, dec2;
if (-1 != (dec1 = HEX2DEC[*(pSrc + 1)])
&& -1 != (dec2 = HEX2DEC[*(pSrc + 2)]))
{
*pEnd++ = (dec1 << 4) + dec2;
pSrc += 3;
continue;
}
}
*pEnd++ = *pSrc++;
}
// the last 2- chars
while (pSrc < SRC_END)
*pEnd++ = *pSrc++;
std::string sResult(pStart, pEnd);
delete [] pStart;
return sResult;
}
std::string UriEncode(const std::string&sSrc)
{
常量字符DEC2HEX[16+1]=“0123456789ABCDEF”;
const unsigned char*pSrc=(const unsigned char*)sSrc.c_str();
const int SRC_LEN=sSrc.length();
无符号字符*const pStart=新的无符号字符[SRC_LEN*3];
无符号字符*pEnd=pStart;
const unsigned char*const SRC_END=pSrc+SRC_LEN;
对于(;pSrc>4];
*pEnd++=DEC2HEX[*pSrc&0x0F];
}
}
std::字符串sResult((char*)pStart,(char*)pEnd);
删除[]pStart;
返回sResult;
}
解码:
std::string UriEncode(const std::string & sSrc)
{
const char DEC2HEX[16 + 1] = "0123456789ABCDEF";
const unsigned char * pSrc = (const unsigned char *)sSrc.c_str();
const int SRC_LEN = sSrc.length();
unsigned char * const pStart = new unsigned char[SRC_LEN * 3];
unsigned char * pEnd = pStart;
const unsigned char * const SRC_END = pSrc + SRC_LEN;
for (; pSrc < SRC_END; ++pSrc)
{
if (SAFE[*pSrc])
*pEnd++ = *pSrc;
else
{
// escape this char
*pEnd++ = '%';
*pEnd++ = DEC2HEX[*pSrc >> 4];
*pEnd++ = DEC2HEX[*pSrc & 0x0F];
}
}
std::string sResult((char *)pStart, (char *)pEnd);
delete [] pStart;
return sResult;
}
std::string UriDecode(const std::string & sSrc)
{
// Note from RFC1630: "Sequences which start with a percent
// sign but are not followed by two hexadecimal characters
// (0-9, A-F) are reserved for future extension"
const unsigned char * pSrc = (const unsigned char *)sSrc.c_str();
const int SRC_LEN = sSrc.length();
const unsigned char * const SRC_END = pSrc + SRC_LEN;
// last decodable '%'
const unsigned char * const SRC_LAST_DEC = SRC_END - 2;
char * const pStart = new char[SRC_LEN];
char * pEnd = pStart;
while (pSrc < SRC_LAST_DEC)
{
if (*pSrc == '%')
{
char dec1, dec2;
if (-1 != (dec1 = HEX2DEC[*(pSrc + 1)])
&& -1 != (dec2 = HEX2DEC[*(pSrc + 2)]))
{
*pEnd++ = (dec1 << 4) + dec2;
pSrc += 3;
continue;
}
}
*pEnd++ = *pSrc++;
}
// the last 2- chars
while (pSrc < SRC_END)
*pEnd++ = *pSrc++;
std::string sResult(pStart, pEnd);
delete [] pStart;
return sResult;
}
std::string-UriDecode(const-std::string&sSrc)
{
//RFC1630中的注释:“以百分比开头的序列
//符号,但后面不跟两个十六进制字符
//(0-9,A-F)保留供将来扩展”
const unsigned char*pSrc=(const unsigned char*)sSrc.c_str();
const int SRC_LEN=sSrc.length();
const unsigned char*const SRC_END=pSrc+SRC_LEN;
//最后可解码“%”
const unsigned char*const SRC_LAST_DEC=SRC_END-2;
char*const pStart=新字符[SRC_LEN];
char*pEnd=pStart;
而(pSrc编码:
您可以使用glib.h提供的“g_uri_escape_string()”函数。
您应该指定使用的OS/库。这似乎是显而易见的可能性。最后,我找到了一个URL编码/解码-->URLCononicalize的API。这是一个shell API,我们可以使用此API对URL进行编码或解码。请参阅下面的链接。这是否回答了您的问题?更好--现在稍微重写一下,以摆脱new[]
和删除[]
,以及…最好使用std::string
或std::vector
而不是使用new[]
和delete[]
直接。举个例子,如果在new
和delete
之间的任何地方抛出异常,这些都会泄漏内存。好的……明白你的意思了。实际上这只是我参考过的站点的复制粘贴。现在我想OP可以知道他必须做什么了!!!;)最后,我找到了一个URL编码/解码-->URLCononicalize的API。这是一个shell API,我们可以使用此API对URL进行编码或解码。请参阅下面的链接hmmm…编码函数是否缺少一个SAFE
声明?如果(SAFE[*pSrc])
将如何处理?