用vc下载和获取远程文件大小

char m_URL[] = "http://www.baidu.com/img/logo.gif";
CString strServer,strObject;
CString szHeaders;
INTERNET_PORT nPort;

CInternetSession session("My Sesssion");
CHttpConnection * pServer = NULL;
CHttpFile * pFile = NULL;
DWORD dwRet , dwRead;
DWORD nServiceType;
char szBuff[1024*100];

if(TRUE!=AfxParseURL(m_URL,nServiceType,strServer,strObject,nPort))
{
   delete pServer;
   AfxMessageBox("URL error");
   return;
}

pServer = session.GetHttpConnection( strServer, nPort);
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,strObject);
pFile->SendRequest();
pFile->QueryInfoStatusCode(dwRet);

if (dwRet == HTTP_STATUS_OK)
{
  
   memset( szBuff, 0, sizeof(szBuff));
   DWORD dwFileLen = m_size = pFile->GetLength();
   CString str;
   str.Format("filelen=%d",dwFileLen);

   AfxMessageBox(str);
   DWORD ret;
   dwRead=10;
   ret=0;
   HANDLE hFile;

   hFile=CreateFile("d://p.gif",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);

  
   while(dwRead>0)
   {
    dwRead=pFile->Read(szBuff,sizeof(szBuff)-1);
    WriteFile(hFile,szBuff,dwRead,&ret,NULL);
   }
  

   CloseHandle(hFile);
}

delete pFile;
delete pServer;

session.Close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值