使用 WinHttp 实现文件下载

使用 WinHttp 实现文件下载
 1 #include <Windows.h>
 2 #include <Winhttp.h>
 3 #pragma comment(lib,"Winhttp.lib")
 4 
 5 typedef struct _URL_INFO
 6 {
 7     WCHAR szScheme[512];
 8     WCHAR szHostName[512];
 9     WCHAR szUserName[512];
10     WCHAR szPassword[512];
11     WCHAR szUrlPath[512];
12     WCHAR szExtraInfo[512];
13 }URL_INFO, *PURL_INFO;
14 
15 
16 int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd )
17 {
18     URL_INFO url_info = {0};
19     URL_COMPONENTSW url = {0};
20     url.dwStructSize = sizeof(url);
21     url.lpszExtraInfo = url_info.szExtraInfo;
22     url.lpszHostName = url_info.szHostName;
23     url.lpszPassword = url_info.szPassword;
24     url.lpszScheme = url_info.szScheme;
25     url.lpszUrlPath = url_info.szUrlPath;
26     url.lpszUserName = url_info.szUserName;
27 
28     url.dwExtraInfoLength = 
29         url.dwHostNameLength = 
30         url.dwPasswordLength = 
31         url.dwSchemeLength = 
32         url.dwUrlPathLength = 
33         url.dwUserNameLength = 512;
34 
35     WinHttpCrackUrl(L"http://www.pediy.com/tools/Debuggers/ollydbg/OllyICE.rar", 0, ICU_ESCAPE, &url);
36 
37     // 创建一个会话
38     HINTERNET hSession = WinHttpOpen(NULL, WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0);
39     BYTE *pBuffer = NULL;
40     DWORD dwReadBytes, dwSizeDW = sizeof(dwSizeDW), dwContentSize, dwIndex = 0;
41     // 创建一个连接
42     HINTERNET hConnect = WinHttpConnect(hSession, url.lpszHostName, url.nPort, 0);
43     // 创建一个请求,先查询内容的大小
44     HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"HEAD", url.lpszUrlPath, L"HTTP/1.1", WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH);
45     WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
46     WinHttpReceiveResponse(hRequest, 0);
47     WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_CONTENT_LENGTH|WINHTTP_QUERY_FLAG_NUMBER, NULL, &dwContentSize, &dwSizeDW, &dwIndex);
48     WinHttpCloseHandle(hRequest);
49 
50     // 创建一个请求,获取数据
51     hRequest = WinHttpOpenRequest(hConnect, L"GET", url.lpszUrlPath, L"HTTP/1.1", WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH);
52     WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
53     WinHttpReceiveResponse(hRequest, 0);
54     pBuffer = new BYTE[dwContentSize];
55     ZeroMemory(pBuffer, dwContentSize);
56     WinHttpReadData(hRequest, pBuffer, dwContentSize, &dwReadBytes);
57 
58     HANDLE hFile = CreateFileW(L"c:\\ollydbg.rar", GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
59     WriteFile(hFile, pBuffer, dwContentSize, &dwReadBytes, NULL);
60     CloseHandle(hFile);
61 
62     delete pBuffer;
63 
64     WinHttpCloseHandle(hRequest);
65     WinHttpCloseHandle(hConnect);
66     WinHttpCloseHandle(hSession);
67 
68     return 0;
69 }
使用 WinHttp 实现文件下载

 

使用 WinHttp 实现文件下载,布布扣,bubuko.com

使用 WinHttp 实现文件下载

上一篇:Windows下配置cygwin和ndk编译环境


下一篇:Yeelink平台推送传感器结果——套接字编程 Windows平台