用InternetOpen下载小文件 vc c++

文章改自:

http://hi.baidu.com/hacknothack/blog/item/4ca77710bc8e97fdc3ce79ad.html

使用vc6.0通过

  1. #include"stdafx.h"
  2. #include<windows.h>
  3. #include<wininet.h>
  4. #include<iostream.h>
  5. #pragmacomment(lib,"wininet.lib")
  6. voidmain()
  7. {
  8. DWORDbyteread=0;
  9. charbuffer[100];
  10. memset(buffer,0,100);
  11. HINTERNETinternetopen;
  12. internetopen=InternetOpen("Testing",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
  13. if(internetopen==NULL)
  14. {
  15. cout<<"Internetopenfailed!"<<endl;
  16. return;
  17. }
  18. HINTERNETinternetopenurl;
  19. internetopenurl=InternetOpenUrl(internetopen,"http://dt.163.com/images/news/0605/news02053101_5.jpg",NULL,0,INTERNET_FLAG_RELOAD,0);
  20. if(internetopenurl==NULL)
  21. {
  22. cout<<"Internetopenurlfailed!"<<endl;
  23. gotothere;
  24. }
  25. BOOLhwrite;
  26. DWORDwritten;
  27. HANDLEcreatefile;
  28. createfile=CreateFile("c://sss.jpg",GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
  29. if(createfile==INVALID_HANDLE_VALUE)
  30. {
  31. cout<<"CreateFilefailed!"<<endl;
  32. gotonext;
  33. }
  34. BOOLinternetreadfile;
  35. while(1)
  36. {
  37. internetreadfile=InternetReadFile(internetopenurl,buffer,sizeof(buffer),&byteread);
  38. if(byteread==0)
  39. break;
  40. hwrite=WriteFile(createfile,buffer,sizeof(buffer),&written,NULL);
  41. if(hwrite==0)
  42. {
  43. cout<<"Writetofilefailed!"<<endl;
  44. gotohere;
  45. }
  46. }
  47. cout<<"Finisheddownloading!"<<endl;
  48. here:
  49. CloseHandle(createfile);
  50. next:
  51. InternetCloseHandle(internetopenurl);
  52. there:
  53. InternetCloseHandle(internetopen);
  54. }

你可能感兴趣的:(internet)