InternetOpen系列函数进行下载的测试例子

  • 时间:
  • 来源:互联网
  • 文章标签:

转自 :http://hi.baidu.com/wlpscu/item/d10a2a0ecf14b491a2df4376

一个完整利用InternetOpen等系列函数进行下载的测试例子

 // TODO: Add extra validation here
 HINTERNET hssetion;
 HINTERNET hConnect;
 HINTERNET hRequrest;
 
 hssetion = InternetOpen( "httpDownload", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
 if ( NULL == hssetion )
 {
  ::AfxMessageBox("open failed");
  return;
 }
 
 hConnect = InternetConnect( hssetion, "bbmedia.qq.com", INTERNET_DEFAULT_HTTP_PORT,
  NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0 );
 if ( NULL == hConnect )
 {
  ::AfxMessageBox("connect failed");
  return;
 }
 
 const char* pFileType = "*/*";
 hRequrest = HttpOpenRequest( hConnect, "GET", "musictop//new//1214793378.wma", "HTTP/1.1", NULL, 
  &pFileType, INTERNET_FLAG_RELOAD, 0 );
 
 if ( NULL == hRequrest )
 {
  ::AfxMessageBox("open requrest failed");
  return;
 }
 
 BOOL bBet = HttpSendRequest( hRequrest, NULL, 0, NULL, 0 );
 if ( !bBet ) 
 {
  ::AfxMessageBox("send request failed");
  return;
 }
 DWORD BufLen=4*1024*10;
 char buffer[4*1024*10] = {0};
 DWORD infoLen=4*1024*10;
 
 DWORD dwIndex=0;
 bool RetQueryInfo=HttpQueryInfo(hRequrest,
  HTTP_QUERY_CONTENT_LENGTH,
  buffer, &infoLen,
  &dwIndex);
 if ( !RetQueryInfo)
 {
  ::AfxMessageBox("query info failed");
  return;
 }
 int FileSize = atoi(buffer); // 文件大小
 
 
 DWORD dwRequest; // 请求下载的字节数
 DWORD dwRead; // 实际读出的字节数
 dwRequest=BufLen;
 
 
 FILE* f = fopen( "Download", "ab" );
 while(true)
 {
  memset( buffer, 0, BufLen );
  bool ReadReturn = InternetReadFile(hRequrest,
   (LPVOID)buffer,
   dwRequest,
   &dwRead);
  if(!ReadReturn)break;
  if(dwRead==0)break;
  // 保存数据
  //  buffer[dwRead]='\0';
  
  //  FileWrite(iFileHandle, buffer, dwRead);
  fwrite( buffer, 1, dwRead, f );
  //  dwCount = dwCount + dwRead;
  // 发出下载进程事件
  
 }
 fclose( f );
 
 InternetCloseHandle( hRequrest );
 InternetCloseHandle( hConnect );
 InternetCloseHandle( hssetion );
 
 ::AfxMessageBox("finished");

本文链接http://www.taodudu.cc/news/show-647742.html