需要添加下面两个引用。
#include <winhttp.h>
#pragma comment(lib,"winhttp.lib")
重要的部分是这个
// Set client certificate is option and credentials. Then send a request.
if( hRequest )
{
BOOL fRet = WinHttpSetOption ( hRequest, WINHTTP_OPTION_CLIENT_CERT_CONTEXT, WINHTTP_NO_CLIENT_CERT_CONTEXT, 0);
bResults = WinHttpSendRequest( hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0 );
}
完整的下载函数如下,
///szPath为服务器一级域名后的路径
///szFileName为下载到本地的路径加文件名
int Download(char * szPath, char * szFileName)
{
//printf("szPath is %s, szFileName is %s\n",szPath,szFileName);
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,hConnect = NULL,hRequest = NULL;
LPWSTR szURL = ConvertCharToLPWSTR(szPath);
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen( L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
// Specify an HTTP server.
if( hSession )
hConnect = WinHttpConnect( hSession, L"网站一级域名,'https://‘到第一个'/’部分",INTERNET_DEFAULT_HTTPS_PORT, 0 );
// Create an HTTP request handle.
if( hConnect )
hRequest = WinHttpOpenRequest( hConnect, L"GET", szURL,NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE );
if (!WinHttpSetTimeouts( hRequest, 300000, 300000,300000, 300000))
printf( "Error %u in WinHttpSetTimeouts.\n", GetLastError());
// Set client certificate is option and credentials. Then send a request.
if( hRequest )
{
BOOL fRet = WinHttpSetOption ( hRequest, WINHTTP_OPTION_CLIENT_CERT_CONTEXT, WINHTTP_NO_CLIENT_CERT_CONTEXT, 0);
bResults = WinHttpSendRequest( hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0 );
}
// End the request.
if( bResults )
bResults = WinHttpReceiveResponse( hRequest,NULL);
// Keep checking for data until there is nothing left.
if( bResults )
{
do
{
// Check for available data.
dwSize = 0;
if( !WinHttpQueryDataAvailable( hRequest, &dwSize ) )
{
printf( "Error %u in WinHttpQueryDataAvailable.\n", GetLastError( ) );
bResults = FALSE;
}
if(dwSize!=0)
{
// Allocate space for the buffer.
pszOutBuffer = new char[dwSize+1];
if( !pszOutBuffer )
{
printf( "Out of memory\n" );
bResults = FALSE;
dwSize=0;
}
else
{
ZeroMemory( pszOutBuffer, dwSize+1 );
if( !WinHttpReadData( hRequest, (LPVOID)pszOutBuffer,dwSize, &dwDownloaded ) )
printf( "Error %u in WinHttpReadData.\n", GetLastError( ) );
else
{
HANDLE hFile;
hFile = CreateFile(szFileName,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile==INVALID_HANDLE_VALUE)
{
printf("Faile to create file %s.\n",szFileName);
bResults = FALSE;
break;
}
DWORD dwBytes;
SetFilePointer(hFile,NULL,NULL,FILE_END);
WriteFile(hFile,pszOutBuffer,dwDownloaded,&dwBytes,NULL);
if(dwBytes!=dwSize)
{
printf("File size is %d but only write %d\n",dwSize,dwBytes);
bResults = FALSE;
CloseHandle(hFile);
break;
}
CloseHandle(hFile);
}
// Free the memory allocated to the buffer.
delete [] pszOutBuffer;
}
}
} while( dwSize > 0 );
}
// Close any open handles.
if( hRequest ) WinHttpCloseHandle( hRequest );
if( hConnect ) WinHttpCloseHandle( hConnect );
if( hSession ) WinHttpCloseHandle( hSession );
// Report any errors.
if( !bResults )
{
printf( "Error %d occurred.\n", GetLastError( ) );
DeleteFile(szFileName);
return 1;
}
else
{
printf("Download %s successfully.\n",szPath);
return 0;
}
}