源码
-
#include <stdio.h>
-
-
#include <curl/curl.h>
-
-
int main() {
-
CURL *curl;
-
CURLcode res;
-
//FILE *headerfile;
-
const char *pPassphrase = "123456";
-
-
static const char *pCertFile = "/opt/pki/client/xxmeng-cert.pem";
-
static const char *pCACertFile = "/opt/pki/ca/ca.crt";
-
//static const char *pHeaderFile = "dumpit";
-
-
const char *pKeyName;
-
const char *pKeyType;
-
-
const char *pEngine;
-
-
-
pKeyName = "/opt/pki/client/xxmeng.pem";
-
pKeyType = "PEM";
-
//pEngine = NULL;
-
-
//headerfile = fopen(pHeaderFile, "wb");
-
-
curl_global_init(CURL_GLOBAL_DEFAULT);
-
-
curl = curl_easy_init();
-
if(curl) {
-
/* what call to write: */
-
curl_easy_setopt(curl, CURLOPT_URL, "");
-
//curl_easy_setopt(curl, CURLOPT_HEADERDATA, headerfile);
-
-
do { /* dummy loop, just to break out from */
-
-
/* cert is stored PEM coded in file... */
-
/* since PEM is default, we needn't set it for PEM */
-
curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, "PEM");
-
-
/* set the cert for client authentication */
-
curl_easy_setopt(curl, CURLOPT_SSLCERT, pCertFile);
-
-
/* sorry, for engine we must set the passphrase
-
(if the key has one...) */
-
if(pPassphrase)
-
curl_easy_setopt(curl, CURLOPT_KEYPASSWD, pPassphrase);
-
-
/* if we use a key stored in a crypto engine,
-
we must set the key type to "ENG" */
-
curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, pKeyType);
-
-
/* set the private key (file or ID in engine) */
-
curl_easy_setopt(curl, CURLOPT_SSLKEY, pKeyName);
-
-
/* set the file with the certs vaildating the server */
-
curl_easy_setopt(curl, CURLOPT_CAINFO, pCACertFile);
-
-
/* disconnect if we can't validate server's cert */
-
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
-
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
-
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 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));
-
-
/* we are done... */
-
} while(0);
-
/* always cleanup */
-
curl_easy_cleanup(curl);
-
}
-
-
curl_global_cleanup();
-
-
return 0;
-
}
cmake文件
cmake_minimum_required(VERSION 3.12)
project(curltest C)
set(CMAKE_C_STANDARD 11)
set( CURL_LIBRARY ${CMAKE_SOURCE_DIR}/lib )
set( CURL_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include )
find_package( CURL )
include_directories( ${CURL_INCLUDE_DIRS} )
link_directories( ${CURL_LIBRARIES} )
add_executable(curltest main.c)
阅读(2114) | 评论(0) | 转发(0) |