首先當然是利用Wininet.lib庫了,添加頭文件#include "WinInet.h"和庫#pragma comment(lib,"Wininet.lib"),本人最開始時使用同步下載,但是發現下載600K左右的文件都不能下下來,網絡自己會斷掉,很奇怪,於是我采用異步方式。
#include "stdafx.h"
#include "WinInet.h"
#pragma comment(lib,"Wininet.lib")
void CALLBACK InternetProc(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus, LPVOID lpvStatusInformation, DWORD dwStatusInformationLength);
void test()
{
char *buffer = new char[2024] ;
hConnectedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
hRequestOpenedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
hRequestCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
m_hSession = InternetOpen(L"ttt", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
// asynchronous transfer mode
if (m_hSession == NULL)
{
printf("Internet open error!");
return;
}
if(InternetSetStatusCallback(m_hSession, (INTERNET_STATUS_CALLBACK)InternetProc)
== INTERNET_INVALID_STATUS_CALLBACK)
{
printf("創建回調失敗!");
}
m_hConnection = InternetConnect(m_hSession, L"dt.163.com", 80,L"",L"",INTERNET_SERVICE_HTTP, 0, 1);
if (m_hConnection == NULL)
{
if (GetLastError() != ERROR_IO_PENDING)
{
printf( "InternetConnect failed, error " );
return;
}
WaitForSingleObject(hConnectedEvent, INFINITE);
}
m_hRequest = HttpOpenRequest(m_hConnection,L"GET",L"/images/news/0605/news02053101_5.jpg",
HTTP_VERSION, NULL, 0, INTERNET_FLAG_DONT_CACHE, 2); //HTTP_VERSION "HTTP/1.0"
if (m_hRequest == NULL)
{
if (GetLastError() != ERROR_IO_PENDING)
{
printf("InternetRequest Failure!");
return;
}
// Wait until we get the request handle
WaitForSingleObject(hRequestOpenedEvent, INFINITE);
}
BOOL bSendRequest = HttpSendRequest(m_hRequest, NULL, 0, 0, 0);
if (bSendRequest == FALSE)
{
printf("SendRequest Failure!");
}
WaitForSingleObject(hRequestCompleteEvent, INFINITE);
BOOL hwrite;
DWORD written;
HANDLE createfile;
createfile=CreateFile(L"//sss.jpg",GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
if (createfile==INVALID_HANDLE_VALUE)
{
printf("Create jpg error!");
return;
}
TCHAR bufQuery[32] = {0};
DWORD dwLengthBufQuery = sizeof(bufQuery);
HttpQueryInfo(m_hRequest, HTTP_QUERY_CONTENT_LENGTH, bufQuery, &dwLengthBufQuery, NULL);
DWORD dwFileSize = (DWORD)_wtol(bufQuery); //get the length of received file
printf("%d/n",dwFileSize);
DWORD dwBytesRead;
while(1)
{
InternetReadFile(m_hRequest,buffer,sizeof(buffer),&dwBytesRead);
if(dwBytesRead == 0)
break;
hwrite=WriteFile(createfile,buffer,sizeof(buffer),&written,NULL);
if (hwrite==0)
{
printf("Write error!");
break ;
}
}
delete buffer;
CloseHandle(createfile);
InternetCloseHandle(m_hSession);
InternetCloseHandle(m_hConnection);
InternetCloseHandle(m_hRequest);
}
void CALLBACK InternetProc(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus,
LPVOID lpStatusInfo, DWORD dwStatusInformationLength)
{
printf( "Callback dwInternetStatus: %d/r" , dwInternetStatus);
switch(dwContext)
{
case 1: // Connection handle
if (dwInternetStatus == INTERNET_STATUS_HANDLE_CREATED)
{
INTERNET_ASYNC_RESULT *pRes = (INTERNET_ASYNC_RESULT *)lpStatusInfo;
m_hConnection = (HINTERNET)pRes->dwResult;
printf( "Connect handle created/n");
SetEvent(hConnectedEvent);
}
break;
case 2: // Request handle
switch(dwInternetStatus)
{
case INTERNET_STATUS_HANDLE_CREATED:
{
INTERNET_ASYNC_RESULT *pRes = (INTERNET_ASYNC_RESULT *)lpStatusInfo;
m_hRequest = (HINTERNET)pRes->dwResult;
printf( "Request handle created/n");
SetEvent(hRequestOpenedEvent);
}
break;
case INTERNET_STATUS_REQUEST_SENT:
{
DWORD *lpBytesSent = (DWORD*)lpStatusInfo;
printf("Bytes Sent: %d/n", *lpBytesSent);
//dwNumBytesComplete += *lpBytesSent;
}
break;
case INTERNET_STATUS_REQUEST_COMPLETE:
{
INTERNET_ASYNC_RESULT *pAsyncRes = (INTERNET_ASYNC_RESULT *)lpStatusInfo;
printf("Function call finished" );
printf("dwResult: %d/n" ,pAsyncRes->dwResult);
printf( "dwError: %s/n ",pAsyncRes->dwError);
SetEvent(hRequestCompleteEvent);
}
break;
case INTERNET_STATUS_RECEIVING_RESPONSE:
printf("Receiving Response/n" );
break;
case INTERNET_STATUS_RESPONSE_RECEIVED:
{
DWORD *dwBytesReceived = (DWORD*)lpStatusInfo;
printf( "Received %d/n " , *dwBytesReceived);
}
}
}
}
代碼中用一個回調函數來控制每一步是否完成,功能是下載一幅圖片。