CInternetSession session;
 CInternetFile *pFile = NULL;
 try{
  pFile = (CInternetFile*)session.OpenURL("http://www.baidu.com"); //打开一个URL链接
 }catch(CInternetException *pEx){
  pEx->ReportError();
  pFile = NULL;
  pEx->Delete();
 }
 if(pFile)
 {
  DWORD len = pFile->GetLength();//得到文件长度
  CString s;
  s.Format("%d",len);
  MessageBox(s);
  }为什么我得到的文件长度有时候是840,而有时候是2048呢?

解决方案 »

  1.   

    840的时候文件内容是:
    <html><head><meta http-equiv=Content-Type content="text/html;charset=gb2312"><title>百度一下,你就知道      </title><style>body{margin:4px 0}p{margin:0;padding:0}img{border:0}td,p,#u{font-size:12px}#b,#u,#l td,a{font-family:arial}#kw{font:16px Verdana;height:1.78em;padding-top:2px}#b{height:30px;padding-top:4px}#b,#b a{color:#77c}#u{padding-right:10px;line-height:19px;text-align:right;margin:0 0 3px !important;margin:0 0 10px}#sb{height:2em;width:5.6em}#km{height:50px}#l{margin:0 0 5px 15px}#l td{padding-left:107px}p,table{width:650px;border:0}#l td,#sb,#km{font-size:14px}#l a,#l b{margin-right:1.14em}a{color:#00c}a:active{color:#f60}#hp{position:absolute;margin-left:6px}#lg{margin:-26px 0 -44px}#lk{width:auto;line-height:18px;vertical-align:top}form{position:relative;z-index:9}</style></head>
    <body><div id=u><!--[if gte IE 6]><2048的时候文件内容是:<html><head><meta http-equiv=Content-Type content="text/html;charset=gb2312"><title>百度一下,你就知道      </title><style>body{margin:4px 0}p{margin:0;padding:0}img{border:0}td,p,#u{font-size:12px}#b,#u,#l td,a{font-family:arial}#kw{font:16px Verdana;height:1.78em;padding-top:2px}#b{height:30px;padding-top:4px}#b,#b a{color:#77c}#u{padding-right:10px;line-height:19px;text-align:right;margin:0 0 3px !important;margin:0 0 10px}#sb{height:2em;width:5.6em}#km{height:50px}#l{margin:0 0 5px 15px}#l td{padding-left:107px}p,table{width:650px;border:0}#l td,#sb,#km{font-size:14px}#l a,#l b{margin-right:1.14em}a{color:#00c}a:active{color:#f60}#hp{position:absolute;margin-left:6px}#lg{margin:-26px 0 -44px}#lk{width:auto;line-height:18px;vertical-align:top}form{position:relative;z-index:9}</style></head>
    <body><div id=u><!--[if gte IE 6]><a href="http://bar.baidu.com/sobar/prom23.html">工具栏</a>&nbsp;|&nbsp;<![endif]--><a href=http://passport.baidu.com/?login&tpl=mn>登录</a></div><center><img src=http://www.baidu.com/img/baidu_logo.gif width=270 height=129 usemap="#mp" id=lg><br><br><br><br><table cellpadding=0 cellspacing=0 id=l><tr><td><div id=m><a onclick=s(this) href=http://news.baidu.com>新&nbsp;闻</a><b>网&nbsp;页</b><a onclick=s(this) href=http://tieba.baidu.com>贴&nbsp;吧</a><a onclick=s(this) href=http://zhidao.baidu.com>知&nbsp;道</a><a onclick=s(this) href=http://mp3.baidu.com>MP3</a><a onclick=s(this) href=http://image.baidu.com>图&nbsp;片</a><a onclick=s(this) href=http://video.baidu.com>视&nbsp;频</a></div></td></tr></table>
    <table cellpadding=0 cellspacing=0 style="margin-left:15px"><tr valign=top><td style="height:62px;padding-left:92px" nowrap><form name=f action=s><input type=text name=wd id=kw size=42 maxlength=100> <input type=submit value=百度一下 id=sb><span id=hp><a href=/gaoji/preferences.html>设置</a><br><a href=/gaoji/advanced.html>高级</a></span></form></td></tr></table>
    <p id=km><a href=http://hi.baidu.com>空间</a>&nbsp;&nbsp;<a href=http://www.hao123.com>hao123</a>&nbsp;|&nbsp;<a href=/more/>更两个都不全
      

  2.   

    你要直接装化成CHttpFile* ,并设定POST参数,这样才能确定是以什么方式访问的。
    Session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,CON_TIMEOUT);   
    Session.SetOption(INTERNET_OPTION_CONNECT_RETRIES,1); 
    //Post a request
    try {
    DWORD dwFlags = INTERNET_FLAG_TRANSFER_ASCII|INTERNET_FLAG_RELOAD;
    pFile=(CHttpFile *)Session.OpenURL(URL , 1 , dwFlags);
    TRACE(_T("URL=%s \r\n"),CompleteURL);
    }
      

  3.   

    你那得到的并不是网页内容的大小,
    如果要用FTP的话可是试下这样
    pInternetFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,   buf,   &len,   NULL);  
    http://topic.csdn.net/t/20010226/17/75717.html