VC:网络下载(WinInet)

WinInet开发:
1、添加一个下载按钮和一个保存地址按钮,和两个编辑框;并添加各自的变量;
2、C*Dlg类添加成员变量:
 CString url;
 CString savefilename;
3、保存地址按钮的消息映射函数:
void CTestDlg::OnWhere()
{
 // TODO: Add your control notification handler code here
 GetDlgItemText(IDC_URL,url);
 if(url=="")
 {
  AfxMessageBox("请填入URL地址");
  return;
 }
 //下面得到文件类型,即.后面的字符
 CString aboutfile="";
 int len = url.GetLength();
 for(int i=len-1;url[i] != '.';i--)
  aboutfile.Insert(0,url[i]);
 CString strFileName,strFilePath;
 CString m_szDir = "c:\\";
 char BASED_CODE szFilter[] = "All Files (*.*)|*.*||";        
 CFileDialog OpenDlg(false, NULL, NULL, /*OFN_ALLOWMULTISELECT |*/ OFN_EXPLORER, szFilter);        
 OpenDlg.m_ofn.lStructSize = sizeof(OpenDlg.m_ofn);        
 OpenDlg.m_ofn.hwndOwner = NULL;        
 OpenDlg.m_ofn.lpstrTitle = "保存目标文件";        
 OpenDlg.m_ofn.lpstrInitialDir=m_szDir;       //初始目录  
 if( OpenDlg.DoModal() == IDOK)        
 { 
  strFilePath = OpenDlg.GetPathName();
 }
 savefilename=strFilePath+"."+aboutfile;
 SetDlgItemText(IDC_PATH,savefilename);
}
4、开始下载按钮的消息映射函数:
void CTestDlg::OnStart()
{
 // TODO: Add your control notification handler code here
 CInternetSession aInternetSession;  //一个会话
 CHttpConnection* pHttpConnection = NULL; //链接
 CHttpFile*   pHttpFile = NULL;
 const TCHAR szHeaders[] = _T("Accept: */*\r\nUser-Agent:  Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)\r\n"); //协议
 DWORD    dwFileStatus;
 BYTE    szBuffer[1024];
 DWORD    nLen;
 CFile    aFile;
 DWORD    nSuccess = -1;
 CString    strServer;
 INTERNET_PORT  nPort;
 CString    strObject;
 DWORD    dwServiceType;
 DWORD    dwDownloadSize = 0;
 CString    strLogs;
 
 //分解URL
 if(AfxParseURL(url, dwServiceType, strServer, strObject, nPort))
 {
  //如果服务类型不是HTTP下载
  if(dwServiceType != AFX_INET_SERVICE_HTTP && dwServiceType != AFX_INET_SERVICE_HTTPS)
  {
   return;
  }
 }
 while(pHttpConnection == NULL)
  pHttpConnection = aInternetSession.GetHttpConnection(strServer, nPort);
 if(pHttpConnection != NULL) 
 {
  //取得HttpFile对象
  pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET,
            strObject,
            NULL,
            1,
            NULL,
            NULL,
            INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE);
  if(pHttpFile != NULL)
   if(pHttpFile->AddRequestHeaders(szHeaders))
   {
    if(pHttpFile->SendRequest()) //发送文件请求
    { 
     if(pHttpFile->QueryInfoStatusCode(dwFileStatus))//查询文件状态
     {     
      if ((dwFileStatus/100)*100 == HTTP_STATUS_OK)//如果文件状态正常
      {
       if(aFile.Open(savefilename, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary)) //打开文件
       {
        DWORD dwFileLen;
        DWORD dwWordSize = sizeof(dwFileLen);
        pHttpFile->QueryInfo(HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,&dwFileLen,&dwWordSize,NULL);
        nLen = pHttpFile->Read(szBuffer, 1024);//接收数据
        while(nLen >= 1024)//接收文件数据每次接收一个块
        {
         if(nLen > 0)//如果接收到数据
          aFile.Write(szBuffer, nLen); //写入文件
         nLen = pHttpFile->Read(szBuffer, 1024);//接收一个块的数据
        }
        if(nLen > 0)//如果接收到数据
        {
         aFile.Write(szBuffer, nLen);//写入文件
         dwDownloadSize += nLen;
        }
        aFile.Close();//关闭文件
        nSuccess = 0;//设置成功标志
        AfxMessageBox("下载完成");
       }
      }
     }
    }
    delete pHttpFile;
   }
   delete pHttpConnection;//删除http连接对象
 }
 aInternetSession.Close();//关闭http连接
 
}

原文地址:https://www.cnblogs.com/shenchao/p/2940955.html