c++ 对汉字进行url编码(实验管用)

CString urlEncode(CString s)
{

 int len = s.GetLength();
 char *out = new char[len*9+1];
 memset(out , 0 , len*9+1);
 int i , j;
 int ch = 0 ;

 static char  myhex[0xFF+1][4];  //add by zhouzd 2008-10-06
 static bool isinital = false;

 if ( !isinital )
 {
  for ( i = 0 ; i <= 0xFF ; ++i )
  {
   myhex[i][0] = '%';
   sprintf( myhex[i]+1 , "%02X" , i );
  }
  isinital = true;
 }

 for (i = 0 , j = 0; i < len ; ++i )
 {
  ch = s.GetAt(i);

  //printf("%c\n" , s.GetAt(i) );

  if ('A' <= ch && ch <= 'Z')         // 'A'..'Z'
  {
   out[j++] = ch;
  }
  else if ('a' <= ch && ch <= 'z')    // 'a'..'z'
  {
   out[j++] = ch;
  }
  else if ('0' <= ch && ch <= '9')    // '0'..'9'
  {
   out[j++] = ch;
  }
  else if (ch == ' ')           // space
  {
   out[j++] = '+';
  }
  else if (ch == '-' || ch == '_'        // 不需要转化
   || ch == '.' || ch == '!'
   || ch == '~' || ch == '*'
   || ch == '\'' || ch == '('
   || ch == ')')
  {
   out[j++] = ch;
  }
  else if (ch <= 0x007f)     // ASCII控制字符
  {   
   strcat(out , myhex[ch]);
   j += 3;
  }
  else if (ch <= 0x07FF)         // 非ASCII <= 0x7FF
  {
   strcat(out , myhex[0xc0 | (ch >> 6)]);
   strcat(out , myhex[0x80 | (ch & 0x3F)]);
   j += 6;
  }
  else                       // 0x7FF < ch <= 0xFFFF
  {
   strcat(out , myhex[0xe0 | (ch >> 12)]);
   strcat(out , myhex[0x80 | ((ch >> 6) & 0x3F)]);
   strcat(out , myhex[0x80 | (ch & 0x3F)]);
   j += 9;
  }
 }
 out[j] = '\0';
 USES_CONVERSION;
 CString result = A2W(out);

 delete out;
 out = NULL;

 return result;
}

 

 

怎么查看服务器返回值?

 pHTTP->EndRequest(HSR_SYNC);
     
     char strBuff[1025] = {0};
string strHtml; //是string 不是CString
while ((pHTTP->Read((void*)strBuff, 1024)) > 0)
{
strHtml += strBuff;
}
// _cprintf("\n读取内容结束...");

int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, strHtml.c_str(), -1, NULL, 0);
WCHAR *pUnicode = new WCHAR[unicodeLen + 1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));

MultiByteToWideChar(CP_UTF8,0,strHtml.c_str(),-1, pUnicode,unicodeLen);
CString str(pUnicode); //这就是要的内容
delete []pUnicode;

 

 

 

posted @   郑文亮  阅读(2473)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2011-11-23 成功解决 Android 下载中文文件名(转)
点击右上角即可分享
微信分享提示