1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| // gethttps.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "pch.h"
//#include "http_download.hh"
#include <Windows.h>
#include <curl/curl.h>
//#include <sstream>
//#include <stdexcept>
//using namespace std;
typedef struct {
BYTE data[1 * 1024 * 1024];
DWORD pos;
}TBUF,*PBUF;
TBUF bb;
size_t callback(void* contents, size_t size, size_t nmemb, void* user)
{
PBUF pb = (PBUF)user;
size_t sizeIncrease = size * nmemb;
memcpy(pb->data + pb->pos, contents, sizeIncrease);
pb->pos += sizeIncrease;
return sizeIncrease;
}
void download(wchar_t* url, long* responseCode)
{
//vector<char> data;
curl_global_init(CURL_GLOBAL_ALL);
CURL* handle = curl_easy_init();
curl_easy_setopt(handle, CURLOPT_URL, url);
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, callback);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &bb);
curl_easy_setopt(handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
CURLcode result = curl_easy_perform(handle);
if (responseCode != nullptr)
curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, responseCode);
curl_easy_cleanup(handle);
curl_global_cleanup();
if (result != CURLE_OK)
{
/*stringstream err;
err << "Error downloading from URL \"" << url << "\": " << curl_easy_strerror(result);
throw runtime_error(err.str());*/
}
//return move(data);
}
int main()
{
bb.pos = 0;
long rcode = 0;
download((wchar_t*)L"https://www.google.fr/?gws_rd=ssl", &rcode);
if (bb.pos > 0)
{
HANDLE f = CreateFileW(L"test.htm", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
if (f != INVALID_HANDLE_VALUE)
{
DWORD dt = 0;
WriteFile(f, bb.data, bb.pos, &dt, NULL);
CloseHandle(f);
}
}
} |
Partager