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;
}

你可能感兴趣的:(其他,p2p,网络协议,网络)