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;

原文地址:https://www.cnblogs.com/zhwl/p/2784891.html