在VS中关于使用CInternetSession 获取网页的问题

q1137068603 2010-10-08 01:31:05
在VS中关于使用CInternetSession 获取网页的问题,为什么我最后读取了内容少了一些字符


如打开http://www.baidu.com/时最后显示的前面一些字符为<dcyehm>hm>ha>mt tpeuv"otn-ye otn=tx/tlcastg21"<il>偃宦酮? /il>syebd{ot1p }k,s{etclainmdl}l{agn3p }l pnfn:4x"五}l{................
实际应为<!doctype html><html><head><meta http-equiv="Content-Type" co ntent="text/html;charset=gb2312"><title>百度一下,你就知道................

显然是恰好是每两个字符中少了一个

我用的代码如下:

void CConnectLastDlg::OnClick()
{
CString m_Url=_T("http://www.baidu.com/");
CString m_Text=_T("");
//GetDlgItem(IDC_URL)->GetWindowText(m_Url);
///////////////
// TODO: Add your control notification handler code here
CInternetSession httpsession; //定义网络连接Session
CString Line;
CInternetFile * webfile=NULL; //定义一个文件对象指针
UpdateData();
if(m_Url.Left(7)!= _T("http://")) //如果地址不含HTTP类型标志则添加本地文件类型标志
{
m_Url=_T("http://") + m_Url;
}
try
{
webfile=(CInternetFile*)httpsession.OpenURL(m_Url);//打开连接
}
catch(CInternetException*pException) //捕捉异常
{
MessageBox (L"wrong");
webfile=NULL;
pException->Delete();
}
if(webfile)
{
int i=0;
CString kongge(_T("\r\n"));
bool mybool=webfile->ReadString(Line);
m_Text.Empty ();
while(mybool && i<2)/ /读出几行
{
m_Text=m_Text+Line+kongge; //加入换行标志
i++;
mybool=webfile->ReadString(Line);

}
}
else {
MessageBox (L"NULL");

}

StringPro* myPro=new StringPro;//辅助函数类,进行编码转换
myPro->CSrChangeToUn( m_Text);//在后面列出
TOSHOWSTR=m_Text;

UpdateData(FALSE); //更新显示文件
delete webfile; //删除文件
//GetDlgItem (IDC_SHOW)->EnableWindow (FALSE); //禁用显示按钮
httpsession.Close();
}



void StringPro::CSrChangeToUn(CString &str)//当用CString直接读取ANSI文本数据时如何再次转换为UNICODE
{

char *szBuf = new char[str.GetLength()];
for (int i = 0 ; i < str.GetLength(); i++)
{
szBuf[i] = str.GetAt(i);
}
CharToUnicode(szBuf , &str);
delete []szBuf;
}



// 将Char型字符转换为Unicode字符
int StringPro::CharToUnicode(char *pchIn, CString *pstrOut)
{
int nLen;
WCHAR *ptch;
if(pchIn == NULL)
{
return 0;
}
nLen = MultiByteToWideChar(CP_ACP, 0, pchIn, -1, NULL, 0);
ptch = new WCHAR[nLen+1];
MultiByteToWideChar(CP_ACP, 0, pchIn, -1, ptch, nLen);
ptch[nLen] = '\0';

pstrOut->Format(_T("%s"), ptch);

delete [] ptch;
return nLen;
}
...全文
107 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧