18,356
社区成员
发帖
与我相关
我的任务
分享
CInternetSession::OpenURL(
LPCTSTR pstrURL,
DWORD_PTR dwContext = 1,
DWORD dwFlags = INTERNET_FLAG_TRANSFER_ASCII,
LPCTSTR pstrHeaders = NULL, //http头信息,多字符集模式下为LPCSTR类型
DWORD dwHeadersLength = 0 //http头信息长度
);
int num = MultiByteToWideChar(0,0,headers,-1,NULL,0);
wchar_t *w_headers = new wchar_t[num];
MultiByteToWideChar(0,0,headers,-1,w_headers,num);
CHttpFile* htmlFile=(CHttpFile*)mySession.OpenURL(url,1,INTERNET_FLAG_TRANSFER_ASCII|INTERNET_FLAG_TRANSFER_BINARY,w_headers,wcslen(w_headers));
int num = MultiByteToWideChar(0,0,headers,-1,NULL,0);
wchar_t *w_headers = new wchar_t[num+1];
memset(w_headers,0,sizeof(wchar_t)*(num+1));
MultiByteToWideChar(CP_ACP,0,headers,-1,w_headers,num);
CHttpFile* htmlFile=(CHttpFile*)mySession.OpenURL(url,1,INTERNET_FLAG_TRANSFER_ASCII|INTERNET_FLAG_TRANSFER_BINARY,w_headers,-1);
在MultiByteToWideChar这句前面打个断点,单步调试,看看转换的w_headers值对不对
// ANSI To UNCODE转换
CString AnsiToUnicode(char * szAnsi, int len=0);
函数中 CString == LPCTSTR 而 CHAR * == LPCSTR,因此你可用此函数进行转换试试
// ANSI To UNCODE转换
CString CXXXX::AnsiToUnicode(char * szAnsi, int len)
{
CString str;
// ansi to unicode
//预转换,得到所需空间的大小
int wcsLen;
if(len>0)
wcsLen=len;
else
wcsLen= ::MultiByteToWideChar(CP_ACP, NULL, szAnsi, strlen(szAnsi), NULL, 0);
//分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间
wchar_t* wszString = new wchar_t[wcsLen + 1];
//转换
::MultiByteToWideChar(CP_ACP, NULL, szAnsi, strlen(szAnsi), wszString, wcsLen);
//最后加上'\0'
wszString[wcsLen] = '\0'; // UNICODE字串
str=wszString;
delete wszString;
return str;
}
相反的转换,返回字符串长度
// Unicod To ANSI转换
int UnicodToAnsi(CString str,char* buff);
// Unicod To ANSI转换
int CXXXX::UnicodToAnsi(CString str,char* buff)
{
int len=0;
// UNICODE编码 转换成 ANSI编码
len = ::WideCharToMultiByte(CP_ACP, NULL, str,
str.GetLength(), NULL, 0, NULL, NULL); // 取字串长度
::WideCharToMultiByte(CP_ACP, NULL, str, str.GetLength(),
buff, len, NULL, NULL);
buff[len++]=0x00; // 写字串尾部结束标志
return len; // 转换长度
}