curl下载https

/*
"https://xc.zhongtong.com/pic/studentPic/9ad72112fc9f4a6d8898f1ceb970eba6.jpg",
"https://xc.zhongtong.com/operation/file/info?path=studentPic/4278953fd865427e8f132ab71778af7f.jpg",
"https://xc.zhongtong.com/operation/file/info?path=studentPic/636335209b994af198acd863393bdfbc.jpg"
*/
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
	size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);    
	return written;
}

void downLoadFile(char *filename, char *newFilename)
{
    CURL *curl_handle;
    static const char *pagefilename = newFilename;//(char *)newFilename.data();
    FILE *pagefile;
    char *p = filename;//(char *)filename.data();
    curl_global_init(CURL_GLOBAL_ALL);

    /* init the curl session */
    curl_handle = curl_easy_init();

    /* set URL to get here */
    curl_easy_setopt(curl_handle, CURLOPT_URL, p);

    /* Switch on full protocol/debug output while testing */
    curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);

    /* disable progress meter, set to 0L to enable and disable debug output */
    curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
    /* google.com is redirected, so we tell LibCurl to follow redirection */
	curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
    /* SSL Options */
    curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
    curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0);

    /* Provide CA Certs from http://curl.haxx.se/docs/caextract.html */
    curl_easy_setopt(curl_handle, CURLOPT_CAINFO, "ca-bundle.crt");
    /* send all data to this function  */
    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);

    /* open the file */
    pagefile = fopen(pagefilename, "wb");
    if (pagefile) {

        /* write the page body to this file handle */
        curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);

        /* get it! */
        curl_easy_perform(curl_handle);

        /* close the header file */
        fclose(pagefile);
    }

    /* cleanup curl stuff */
    curl_easy_cleanup(curl_handle);
	printf("downLoadFile name:%s\n", newFilename);
    return ;
}
	

int testCurl(char *newFilename)
{
  CURL *curl;
  CURLcode res;
 
  curl_global_init(CURL_GLOBAL_DEFAULT);
 
  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
 
#if 1//def SKIP_PEER_VERIFICATION
    /*
     * If you want to connect to a site who is not using a certificate that is
     * signed by one of the certs in the CA bundle you have, you can skip the
     * verification of the server's certificate. This makes the connection
     * A LOT LESS SECURE.
     *
     * If you have a CA cert for the server stored someplace else than in the
     * default bundle, then the CURLOPT_CAPATH option might come handy for
     * you.
     */
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif

#if 1//def SKIP_HOSTNAME_VERIFICATION
    /*
     * If the site you are connecting to uses a different host name that what
     * they have mentioned in their server certificate's commonName (or
     * subjectAltName) fields, libcurl will refuse to connect. You can skip
     * this check, but this will make the connection less secure.
     */
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif

	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

	FILE *pagefile;
	/* open the file */
	pagefile = fopen(newFilename, "wb");
	if (pagefile) {

		/* write the page body to this file handle */
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, pagefile);

		/* get it! */
		curl_easy_perform(curl);

		/* close the header file */
		fclose(pagefile);

		printf("CURLOPT_WRITEDATA name:%s\n", newFilename);
	}
	
		
#if 0
    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));
 
    /* always cleanup */
    curl_easy_cleanup(curl);	
#endif 
  }

 
  curl_global_cleanup();
 
  return 0;
}


int HttpsPicDownload()
{
    ft_http_client_t* http = 0;	
    ft_http_init();	
	char buf[256] = {0};
	static u32 id = 10000000;
    http = ft_http_new();
	static int count = 0;
	N_TRACE_INF("HttpsPicDownload\n");

	const char* pHttpUrl[3] = 
	{
		"https://xc.zhongtong.com/pic/studentPic/9ad72112fc9f4a6d8898f1ceb970eba6.jpg",
		"https://xc.zhongtong.com/operation/file/info?path=studentPic/4278953fd865427e8f132ab71778af7f.jpg",
		"https://xc.zhongtong.com/operation/file/info?path=studentPic/636335209b994af198acd863393bdfbc.jpg"
	};

	sprintf(buf, "/sata_1/bd_conf/FaceIdPic1/img%d.jpg", id++);

	int code = 0;

	N_TRACE_INF("FacePicByHttps result:%d count:%d \n", code, count);

	int flag = count%3;
	if (flag == 0)
    {
		static int index = 0;
		code = ft_http_sync_download_file(http, pHttpUrl[index%3], buf);
    	if (http) 
		{
        	ft_http_destroy(http);
    	}
		index++;
    	ft_http_deinit();
	}
	else if (flag == 1)
	{
		static int index = 0;
		downLoadFile(pHttpUrl[index%3], buf);
		index++;
	}
	else
	{
		testCurl(buf);
	}
	count++;
//#endif
	return (code == 0)?0:-1;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值