2017-02-20 64 views
0

嗨我想通過使用c + +和cURL的URL獲取圖像。使用C++&cURL,如何獲得需要用戶名和密碼的url圖像

我發現這個代碼,它的工作原理。不過,需要密碼的網址映像無法使用此代碼。

#include <stdio.h> 
#include <curl/curl.h> 

size_t callbackfunction(void *ptr, size_t size, size_t nmemb, void* userdata) 
{ 
FILE* stream = (FILE*)userdata; 
if (!stream) 
{ 
    printf("!!! No stream\n"); 
    return 0; 
} 

size_t written = fwrite((FILE*)ptr, size, nmemb, stream); 
return written; 
} 

bool download_jpeg(char* url) 
{ 
FILE* fp = fopen("out.jpg", "wb"); 
if (!fp) 
{ 
    printf("!!! Failed to create file on the disk\n"); 
    return false; 
} 

CURL* curlCtx = curl_easy_init(); 
curl_easy_setopt(curlCtx, CURLOPT_URL, url); 
curl_easy_setopt(curlCtx, CURLOPT_WRITEDATA, fp); 
curl_easy_setopt(curlCtx, CURLOPT_WRITEFUNCTION, callbackfunction); 
curl_easy_setopt(curlCtx, CURLOPT_FOLLOWLOCATION, 1); 

CURLcode rc = curl_easy_perform(curlCtx); 
if (rc) 
{ 
    printf("!!! Failed to download: %s\n", url); 
    return false; 
} 

long res_code = 0; 
curl_easy_getinfo(curlCtx, CURLINFO_RESPONSE_CODE, &res_code); 
if (!((res_code == 200 || res_code == 201) && rc != CURLE_ABORTED_BY_CALLBACK)) 
{ 
    printf("!!! Response code: %d\n", res_code); 
    return false; 
} 

curl_easy_cleanup(curlCtx); 

fclose(fp); 

return true; 
} 

int main(int argc, char** argv) 
{ 
if (argc < 2) 
{ 
    printf("Usage: %s <url>\n", argv[0]); 
    return -1; 
} 

if (!download_jpeg(argv[1])) 
{ 
    printf("!! Failed to download file: %s\n", argv[1]); 
    return -1; 
} 

return 0; 
} 

我該怎麼補充一些東西?請幫忙。

回答

0

您可以使用捲曲的訪問內置功能爲HTTP認證,也CURLOPT_HTTPAUTH設置身份驗證類型CURLOPT_USERPWD選項。

curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY); 
curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password"); 

Source

HTTPAUTH documentation

USERPWD documentation

相關問題