您的位置:首页 > 其它

关于UNICODE转化成UTF8和POST

2009-02-12 17:34 155 查看
为这点小事也能郁闷。。

注意参数:

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: