Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4229884
  • 博文数量: 447
  • 博客积分: 1241
  • 博客等级: 中尉
  • 技术积分: 5786
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-27 06:48
个人简介

读好书,交益友

文章分类

全部博文(447)

文章存档

2024年(1)

2023年(5)

2022年(29)

2021年(49)

2020年(16)

2019年(15)

2018年(23)

2017年(67)

2016年(42)

2015年(51)

2014年(57)

2013年(52)

2012年(35)

2011年(5)

分类: C/C++

2019-09-09 18:04:14

源码

点击(此处)折叠或打开

  1. #include <stdio.h>

  2. #include <curl/curl.h>

  3. int main() {
  4.     CURL *curl;
  5.     CURLcode res;
  6.     //FILE *headerfile;
  7.     const char *pPassphrase = "123456";

  8.     static const char *pCertFile = "/opt/pki/client/xxmeng-cert.pem";
  9.     static const char *pCACertFile = "/opt/pki/ca/ca.crt";
  10.     //static const char *pHeaderFile = "dumpit";

  11.     const char *pKeyName;
  12.     const char *pKeyType;

  13.     const char *pEngine;


  14.     pKeyName = "/opt/pki/client/xxmeng.pem";
  15.     pKeyType = "PEM";
  16.     //pEngine = NULL;

  17.     //headerfile = fopen(pHeaderFile, "wb");

  18.     curl_global_init(CURL_GLOBAL_DEFAULT);

  19.     curl = curl_easy_init();
  20.     if(curl) {
  21.         /* what call to write: */
  22.         curl_easy_setopt(curl, CURLOPT_URL, "");
  23.         //curl_easy_setopt(curl, CURLOPT_HEADERDATA, headerfile);

  24.         do { /* dummy loop, just to break out from */

  25.             /* cert is stored PEM coded in file... */
  26.             /* since PEM is default, we needn't set it for PEM */
  27.             curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, "PEM");

  28.             /* set the cert for client authentication */
  29.             curl_easy_setopt(curl, CURLOPT_SSLCERT, pCertFile);

  30.             /* sorry, for engine we must set the passphrase
  31.                (if the key has one...) */
  32.             if(pPassphrase)
  33.                 curl_easy_setopt(curl, CURLOPT_KEYPASSWD, pPassphrase);

  34.             /* if we use a key stored in a crypto engine,
  35.                we must set the key type to "ENG" */
  36.             curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, pKeyType);

  37.             /* set the private key (file or ID in engine) */
  38.             curl_easy_setopt(curl, CURLOPT_SSLKEY, pKeyName);

  39.             /* set the file with the certs vaildating the server */
  40.             curl_easy_setopt(curl, CURLOPT_CAINFO, pCACertFile);

  41.             /* disconnect if we can't validate server's cert */
  42.             curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
  43.             curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
  44.             curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  45.             /* Perform the request, res will get the return code */
  46.             res = curl_easy_perform(curl);
  47.             /* Check for errors */
  48.             if(res != CURLE_OK)
  49.                 fprintf(stderr, "curl_easy_perform() failed: %s\n",
  50.                         curl_easy_strerror(res));

  51.             /* we are done... */
  52.         } while(0);
  53.         /* always cleanup */
  54.         curl_easy_cleanup(curl);
  55.     }

  56.     curl_global_cleanup();

  57.     return 0;
  58. }
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)
阅读(2098) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~