CString strHeader = "Content-Type: application/x-www-form-urlencoded";
CInternetSession session;
CHttpFile* pHttpFile = session.OpenURL(strURL,1, INTERNET_FLAG_TRANSFER_ASCII, strHeader, strHeader.GetLength());
DWORD dwRes;
BOOL res = pHttpFile->QueryInfoStatusCode(dwRes);
CString strSentence, strGetSentence = "";
if (res && dwRes == 200) //返回不一定是200,也可能是201,201,302(重定向)
{
BOOL result = fileGet->SendRequest(strHeaders, (char *)(LPCTSTR)strFormData, strFormData.GetLength());
while(fileGet->ReadString(strSentence)) // 讀取提交數據后的返回結果
{
strGetSentence = strGetSentence + strSentence + char(13) + char(10);
strSentence = strSentence + char(13) + char(10);//得到的是一行
char temp[128] = {0};
strcpy(temp,m_strMid.GetBuffer(m_strMid.GetLength()));
int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, temp, strlen(temp), NULL, 0);
//分配空間要給'\0'留個空間,MultiByteToWideChar不會給'\0'空間
wchar_t* wszString = new wchar_t[wcsLen + 1];
//轉換
::MultiByteToWideChar(CP_UTF8, NULL, temp, strlen(temp), wszString, wcsLen);
//最后加上'\0'
wszString[wcsLen] = '\0';
int ansiLen = ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL);
//同上,分配空間要給'\0'留個空間
char* szAnsi = new char[ansiLen + 1];
//轉換
//unicode版對應的strlen是wcslen
::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), szAnsi, ansiLen, NULL, NULL);
//最后加上'\0'
szAnsi[ansiLen] = '\0';
............szAnsi就是最終的結果
if(wszString)
{
delete []wszString;
wszString = NULL;
}
if (szAnsi)
{
delete []szAnsi;
szAnsi = NULL;
}
}
}
但在VC8.0下,就不一樣了。如果用CString來做ReadString的參數,始終得不到ASCII字符,因為VC8下默認的就是寬字符,而UTF-8就是寬字符,所以必須用char*來做ReadString的參數,然后在用函數MultiByteToWideChar轉成UNICODE。如果想得到一行數據,就找標記\r\n就可以