使用wininet的InternetReadFile下载文件
2012-08-10 15:02
330 查看
简单地说,wininet是微软提供的用来制作网络客户端程序的类库,它封装了winsock,为开发人员提供易用的开发接口。
基本上每天我们都会从网络上上传或下载一些文件。今天就简单地使用wininet函数实现下载文件的功能。代码如下:
#include <windows.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <wininet.h>
#include <shellApi.h>
#include <ShlObj.h>
#pragma comment(lib,"wininet")
using namespace std;
bool DownloadFile(LPCTSTR szUrl,LPCTSTR szLocalFile,BOOL bFailIfExists);
int main(int argc, char* argv[])
{
std::cout<<DownloadFile("http://hi.baidu.com/new/failnorth","d:\\failnorth.htm",TRUE)<<std::endl;
getchar();
return 0;
}
bool DownloadFile(LPCTSTR szUrl,LPCTSTR szLocalFile,BOOL bFailIfExists)
{
if (bFailIfExists &&
!PathIsDirectory(szLocalFile) &&
(GetFileAttributes(szLocalFile) != INVALID_FILE_ATTRIBUTES))
{
return false;
}
HANDLE hFile = INVALID_HANDLE_VALUE;
HINTERNET hInet = NULL;
HINTERNET hUrl = NULL;
DWORD dwBuf = 1024*1024,dwRead = 0; //1M
auto_ptr<char> szBuf(new char[dwBuf]);
memset(szBuf.get(),0,dwBuf);
std::string strTmp;
bool bRet = false;
try
{
hFile = CreateFile(szLocalFile,GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile == INVALID_HANDLE_VALUE)
throw "error";
hInet = InternetOpen(NULL,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
if (hInet == NULL)
throw "error";
hUrl = InternetOpenUrl(hInet,szUrl,NULL,0,
INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_RELOAD,0);
if (hUrl == NULL)
throw "error";
for(;;)
{
if (!InternetReadFile(hUrl,szBuf.get(),dwBuf,&dwRead))
{
bRet = false;
break;
}
if (dwRead == 0)
{
bRet = true;
break;
}
//strTmp += std::string(szBuf,dwRead);
WriteFile(hFile,szBuf.get(),dwRead,&dwRead,NULL);
}
throw "ok";
}
catch(...)
{
if (hFile != INVALID_HANDLE_VALUE)
CloseHandle(hFile);
if (hUrl != NULL)
InternetCloseHandle(hUrl);
if (hInet != NULL)
InternetCloseHandle(hInet);
}
return bRet;
}
基本上每天我们都会从网络上上传或下载一些文件。今天就简单地使用wininet函数实现下载文件的功能。代码如下:
#include <windows.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <wininet.h>
#include <shellApi.h>
#include <ShlObj.h>
#pragma comment(lib,"wininet")
using namespace std;
bool DownloadFile(LPCTSTR szUrl,LPCTSTR szLocalFile,BOOL bFailIfExists);
int main(int argc, char* argv[])
{
std::cout<<DownloadFile("http://hi.baidu.com/new/failnorth","d:\\failnorth.htm",TRUE)<<std::endl;
getchar();
return 0;
}
bool DownloadFile(LPCTSTR szUrl,LPCTSTR szLocalFile,BOOL bFailIfExists)
{
if (bFailIfExists &&
!PathIsDirectory(szLocalFile) &&
(GetFileAttributes(szLocalFile) != INVALID_FILE_ATTRIBUTES))
{
return false;
}
HANDLE hFile = INVALID_HANDLE_VALUE;
HINTERNET hInet = NULL;
HINTERNET hUrl = NULL;
DWORD dwBuf = 1024*1024,dwRead = 0; //1M
auto_ptr<char> szBuf(new char[dwBuf]);
memset(szBuf.get(),0,dwBuf);
std::string strTmp;
bool bRet = false;
try
{
hFile = CreateFile(szLocalFile,GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile == INVALID_HANDLE_VALUE)
throw "error";
hInet = InternetOpen(NULL,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
if (hInet == NULL)
throw "error";
hUrl = InternetOpenUrl(hInet,szUrl,NULL,0,
INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_RELOAD,0);
if (hUrl == NULL)
throw "error";
for(;;)
{
if (!InternetReadFile(hUrl,szBuf.get(),dwBuf,&dwRead))
{
bRet = false;
break;
}
if (dwRead == 0)
{
bRet = true;
break;
}
//strTmp += std::string(szBuf,dwRead);
WriteFile(hFile,szBuf.get(),dwRead,&dwRead,NULL);
}
throw "ok";
}
catch(...)
{
if (hFile != INVALID_HANDLE_VALUE)
CloseHandle(hFile);
if (hUrl != NULL)
InternetCloseHandle(hUrl);
if (hInet != NULL)
InternetCloseHandle(hInet);
}
return bRet;
}
相关文章推荐
- 使用wininet的InternetReadFile下载文件
- 解决InternetReadFile下载带有中文字符的文件失败的方案
- HOWTO: 使用 InternetReadFile 获取文件
- 使用 InternetReadFile 来获取文件
- InternetReadFile 下载文件
- 使用 InternetReadFile 来获取文件
- 使用 CInternetSession ,CHttpFile下载文件引起内存泄漏
- 使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- Android开发中,使用线程下载apk文件,出现FileNotFound的问题,解决办法
- 使用 CInternetSession 封装多线程 http 文件下载
- 开源安卓Http文件下载框架file-downloader的使用
- 使用wininet进行FTP文件上传和下载
- Apache,Nginx,Lighttpd分别使用X-sendfile功能提升文件下载性能
- 【转帖】InternetOpen\InternetOpenUrl\InternetReadFile 等相关Win32 网络API 使用详细说明
- Linux解决问题篇——ftp上传文件(使用put)时,permission denied,下载文件(使用get)时,failed to open file
- 使用 CInternetSession 封装多线程 http 文件下载
- IE使用ajaxFileUpload上传文件后台返回json类型前台弹出下载提示框
- 使用FileReference+HttpHandler实现文件上传/下载(Flex与.NET互操作)
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载