怎么把字符串转成浏览器所需要的格式 怎么把勉強 转换成浏览器中可以识别的%E5%8B%89%E5%BC%B7 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 static int UrlEncodeChunk(BYTE ch, LPTSTR pszOutput){ return wsprintf(pszOutput, "%%%.2x", ch);}int UrlEncode(LPCTSTR lpszSource, LPTSTR lpszBuffer, DWORD dwSize){ LPTSTR pCursor=(LPTSTR)lpszSource; TCHAR ch; LPTSTR pOutput=lpszBuffer; while(ch=*pCursor++) { pOutput+=UrlEncodeChunk(ch, pOutput); } *pOutput=0; return pOutput-lpszBuffer;} UrlEscape/UrlUnescape http://msdn2.microsoft.com/en-us/library/ms628636.aspx 自己写的一个小程序,呵呵。URL编码的规则: 1、对每个字节进行编码; 2、在 <input> 中输入的空格转为"+"号; 3、安全字符不编码 4、其他字节以十六进制显示并加"%"前缀。 安全字符的判断标准: 所有的字母、数字和如下几个字符: '\'、 '('、')'、'*'、 '-'、'.'、'_'、'!' inline BOOL bIsURLSafeCode(TCHAR ch) { if ((((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z'))) || ((ch >= '0') && (ch <= '9'))) { return TRUE; } switch (ch) { case '\'': case '(': case ')': case '*': case '-': case '.': case '_': case '!': return TRUE; } return FALSE; }/************************************************************************************************************************对URL进行编码如果-1则失败,其它值返回编码之后的字符串长度.bIsInput:标识是否是Input中的内容,因为在这个位置的空格编码和URL中的不一样。input中的空格编码成+************************************************************************************************************************/int CMyGetWebData::UrlEncode(TCHAR *pszTargetStr, CString &cOutStr, BOOL bIsInput){ int iStrlen = 0, iBufLen = 0, i = 0, j = 0; TCHAR *pszEncodeBuf = NULL; unsigned char ch; iStrlen = (int)strlen(pszTargetStr); if(iStrlen == 0) { return -1; } //按照编码字符串长度的三倍申请BUFFER,因为一个字符最多编码三个,%20。 iBufLen = iStrlen * 3; if((pszEncodeBuf = (TCHAR*)malloc(iBufLen)) == NULL) { return -1; } //开始编码 for(i = 0, j = 0; i < iStrlen; i++) { TCHAR szTemp[256]; ch = pszTargetStr[i]; if(bIsURLSafeCode(ch)) { pszEncodeBuf[j] = ch; j++; } else { if(bIsInput && (ch == ' ') ) //input中的空格编码 { pszEncodeBuf[j] = '+'; j++; } else { _stprintf_s(szTemp, 256, "%%%2x", ch); memcpy(&pszEncodeBuf[j], szTemp, 3); j += 3; } } } pszEncodeBuf[j] = '\0'; cOutStr = pszEncodeBuf; ASSERT((int)strlen(pszEncodeBuf) == cOutStr.GetLength()); return cOutStr.GetLength();} http://www.vckbase.com/document/viewdoc/?id=1730URL编码 static int UrlEncodeChunk(BYTE ch, LPTSTR pszOutput) { return wsprintf(pszOutput, "%%%.2x ", ch); } int UrlEncode(LPCTSTR lpszSource, LPTSTR lpszBuffer, DWORD dwSize) { LPTSTR pCursor=(LPTSTR)lpszSource; TCHAR ch; LPTSTR pOutput=lpszBuffer; while(ch=*pCursor++) { pOutput+=UrlEncodeChunk(ch, pOutput); } *pOutput=0; return pOutput-lpszBuffer; } 在一个线程内用HOSTENT获取本地主机信息的问题 Lib链接出错 程序调试用哪个工具? VC下怎么实现所见 非 所得的打印和打印预览呢? VC2005调用delphi6实现的COM组件问题,如何传递结构指针 无窗体DLL怎样响应系统广播的消息? 用VC控制powerpoint的问题.在线等. 一个关于this指针的问题 自已做个EDIT括展类,怎么能和CString联合使用UpdateData啊! 子窗口的创建(我分不多,不好意思) vc 如何实现屏幕截图 难题:图形选定问题
{
return wsprintf(pszOutput, "%%%.2x", ch);
}int UrlEncode(LPCTSTR lpszSource, LPTSTR lpszBuffer, DWORD dwSize)
{
LPTSTR pCursor=(LPTSTR)lpszSource;
TCHAR ch;
LPTSTR pOutput=lpszBuffer;
while(ch=*pCursor++)
{
pOutput+=UrlEncodeChunk(ch, pOutput);
}
*pOutput=0;
return pOutput-lpszBuffer;
}
http://msdn2.microsoft.com/en-us/library/ms628636.aspx
URL编码的规则:
1、对每个字节进行编码;
2、在 <input> 中输入的空格转为"+"号;
3、安全字符不编码
4、其他字节以十六进制显示并加"%"前缀。 安全字符的判断标准:
所有的字母、数字和如下几个字符: '\'、 '('、')'、'*'、 '-'、'.'、'_'、'!' inline BOOL bIsURLSafeCode(TCHAR ch)
{
if ((((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z'))) || ((ch >= '0') && (ch <= '9')))
{
return TRUE;
} switch (ch)
{
case '\'':
case '(':
case ')':
case '*':
case '-':
case '.':
case '_':
case '!':
return TRUE;
}
return FALSE;
}/************************************************************************************************************************
对URL进行编码
如果-1则失败,其它值返回编码之后的字符串长度.
bIsInput:标识是否是Input中的内容,因为在这个位置的空格编码和URL中的不一样。input中的空格编码成+
************************************************************************************************************************/
int CMyGetWebData::UrlEncode(TCHAR *pszTargetStr, CString &cOutStr, BOOL bIsInput)
{
int iStrlen = 0, iBufLen = 0, i = 0, j = 0;
TCHAR *pszEncodeBuf = NULL;
unsigned char ch; iStrlen = (int)strlen(pszTargetStr);
if(iStrlen == 0)
{
return -1;
}
//按照编码字符串长度的三倍申请BUFFER,因为一个字符最多编码三个,%20。
iBufLen = iStrlen * 3;
if((pszEncodeBuf = (TCHAR*)malloc(iBufLen)) == NULL)
{
return -1;
} //开始编码
for(i = 0, j = 0; i < iStrlen; i++)
{
TCHAR szTemp[256]; ch = pszTargetStr[i];
if(bIsURLSafeCode(ch))
{
pszEncodeBuf[j] = ch;
j++;
}
else
{
if(bIsInput && (ch == ' ') ) //input中的空格编码
{
pszEncodeBuf[j] = '+';
j++;
}
else
{
_stprintf_s(szTemp, 256, "%%%2x", ch);
memcpy(&pszEncodeBuf[j], szTemp, 3);
j += 3;
}
}
}
pszEncodeBuf[j] = '\0';
cOutStr = pszEncodeBuf; ASSERT((int)strlen(pszEncodeBuf) == cOutStr.GetLength()); return cOutStr.GetLength();
}
{
return wsprintf(pszOutput, "%%%.2x ", ch);
} int UrlEncode(LPCTSTR lpszSource, LPTSTR lpszBuffer, DWORD dwSize)
{
LPTSTR pCursor=(LPTSTR)lpszSource;
TCHAR ch;
LPTSTR pOutput=lpszBuffer;
while(ch=*pCursor++)
{
pOutput+=UrlEncodeChunk(ch, pOutput);
}
*pOutput=0;
return pOutput-lpszBuffer;
}