关于UNICODE转化成UTF8和POST

来源:互联网 发布:node.js和js 编辑:程序博客网 时间:2024/05/20 05:10

为这点小事也能郁闷。。

 

注意参数:

WideCharToMultiByte(CP_UTF8, 0, pszValue, -1, buf, 255, NULL, NULL );

 

BOOL result = FALSE;
 CInternetSession  session;
 CHttpFile*  pFile;
 CHttpConnection*  pConnection;
 try
 {
  char strUtf8Req[512]; 
  memset( strUtf8Req, 0, 512);
  WCHAR* strWchar;
  strWchar = request.GetBuffer();
  string str;
  GetAnsiString( str, strWchar);
  strcpy( strUtf8Req, str.c_str() );


  CString  strServerName = _T("a.b.c.d:8080");
  CString  strFormAction = _T("/xx/api/xxxxx");
  CString  strHeaders    = _T("Content-Type: application/x-www-form-urlencoded");

  pConnection = session.GetHttpConnection(strServerName);
  if( pConnection == NULL)
   return FALSE;
  pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, strFormAction);
  if( pFile == NULL)
   return FALSE;
    
  //result = pFile->SendRequest( strHeaders, (void*)strFormData, strlen(strFormData) );
  result = pFile->SendRequest( strHeaders, (void*)strUtf8Req, strlen(strUtf8Req) );
  DWORD  dwRet;
  pFile->QueryInfoStatusCode(dwRet);
  if( dwRet == HTTP_STATUS_OK)
  {
   CString responseUtf8;
   CString strLine;
   while(pFile->ReadString(strLine))
   {
    responseUtf8 += strLine;
   }

   char* strUtf8 = (char*)responseUtf8.GetBuffer();
   result = ParseResponse(strUtf8, response);
   responseUtf8.ReleaseBuffer();
  }
  pFile->Close();
  session.Close();
  pConnection->Close();
    }
    catch (CInternetException* e)
 {
  pFile->Close();
  session.Close();
  pConnection->Close();
        CString s;
        s.Format(_T("Internet Exception/r/nm_dwError%u,m_dwContextError%u"),e->m_dwError,e->m_dwContext);
        AfxMessageBox(s);
  return FALSE;
    }

 return result;

原创粉丝点击