打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C++读取网络url文件内容


文件地址为“http://www.baidu.com/xxx.txt”

  1. // readTxt.cpp :Defines the entry point for the console application.  
  2. //  
  3.    
  4. #include "stdafx.h"  
  5. #include <Windows.h>  
  6. #include <wininet.h>  
  7. #include <string>  
  8. using namespace std;  
  9. #pragma comment(lib, "wininet.lib")  
  10. #define BUF_SIZE 1024  
  11.    
  12. LPSTR GetInterNetURLText(LPSTR lpcInterNetURL, char* buff);  
  13.    
  14. int _tmain(int argc, _TCHAR* argv[])  
  15. {  
  16.     char buf[BUF_SIZE] = {0};  
  17.     char url[MAX_PATH] = "http://www.baidu.com/xxx.txt";  
  18.     GetInterNetURLText(url, buf);  
  19.    
  20.     return 0;  
  21. }  
  22.    
  23. LPSTR GetInterNetURLText(LPSTR lpcInterNetURL, char* buff)  
  24. {     
  25.     HINTERNET hSession;     
  26.     LPSTR lpResult = NULL;  
  27.     hSession = InternetOpen("WinInet", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);     
  28.     __try  
  29.     {         
  30.        if(hSession != NULL)          
  31.        {            
  32.            HINTERNET hRequest;              
  33.            hRequest = InternetOpenUrlA(hSession,lpcInterNetURL, NULL,0, INTERNET_FLAG_RELOAD, 0);          
  34.            __try           
  35.            {                  
  36.               if(hRequest != NULL)          
  37.               {             
  38.                   DWORD dwBytesRead;                    
  39.                   char szBuffer[BUF_SIZE] = {0};  
  40.    
  41.                   if(InternetReadFile(hRequest, szBuffer, BUF_SIZE, &dwBytesRead))             
  42.                   {                   
  43.                      RtlMoveMemory(buff, szBuffer, BUF_SIZE);     
  44.                      return 0;                
  45.                   }                 
  46.               }             
  47.            }__finally       
  48.            {                
  49.               InternetCloseHandle(hRequest);    
  50.            }         
  51.        }     
  52.     }__finally      
  53.     {         
  54.        InternetCloseHandle(hSession);    
  55.     }     
  56.     return lpResult;  
  57. }  


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
在VC中WININET使用HTTP的POSTA和GET方法
C++下载该二进制文件
mciSendString的使用2
MFC中通过CURL 下载,断点续传,
ffmpeg分析系列
ffmpeg分析系列之五(打开输入的文件)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服