size_t test_save(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t sizes = size * nmemb;
DWORD dwHasWritten;
WriteFile((HANDLE)stream,ptr,sizes,&dwHasWritten,NULL);
return sizes;
} int test_get(const char *url, HANDLE hFile)
{
CURL *pCurl = curl_easy_init();
int responseCode = -1;
CURLcode returnCode = CURLE_OK;
do
{
if (!pCurl)
{
break;
}
returnCode = curl_easy_setopt(pCurl, CURLOPT_CONNECTTIMEOUT, 10); //设置连接超时时间
CURL_BREAK_IF_NOT_OK(returnCode); returnCode = curl_easy_setopt(pCurl, CURLOPT_URL, url); //设置请求链接
CURL_BREAK_IF_NOT_OK(returnCode); returnCode = curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, hFile); //设置CURLOPT_WRITEFUNCTION的最后一个参数值
CURL_BREAK_IF_NOT_OK(returnCode); returnCode = curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, test_save); //设置接收到数据之后的回调函数
CURL_BREAK_IF_NOT_OK(returnCode); returnCode = curl_easy_perform(pCurl); //开始传输
CURL_BREAK_IF_NOT_OK(returnCode); returnCode = curl_easy_getinfo(pCurl, CURLINFO_RESPONSE_CODE, &responseCode); //获取返回信息
} while (0);
if (pCurl)
{
curl_easy_cleanup(pCurl);
}
return responseCode;
} HANDLE hFile = CreateFileW(L"d:\\22.exe", GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ/*|FILE_SHARE_DELETE*/, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE != hFile)
{
test_get("http://dldir1.qq.com/qqfile/qq/QQ8.4/18380/QQ8.4.exe", hFile);
}
CloseHandle(hFile);
hFile = NULL;