1、配置curl https请求需要提供 CA证书、客户端证书和客户端秘钥,这三个文件的pem格式。

  分别对应 curl_easy_setopt() 函数的 下面三个参数:

  CURLOPT_CAINFO - path to Certificate Authority (CA) bundle
  CURLOPT_SSLKEY - specify private keyfile for TLS and SSL client cert
  CURLOPT_SSLCERT - set SSL client certificate

一般创建SSL证书时会生成 ca.crt , client.crt, client.key, server.crt, server.key 等,而 curl客户端请求,只需要将 ca.crt , client.crt, client.key转成相应的 pem格式 使用。转换方法如下:

  1)将 CRT 转成 PEM---
    不能直接将 .crt 转成 .pem,需要经过 .der 中转

   openssl x509 -in client.crt -out client.der -outform der
   openssl x509 -in client.der -inform der -outform pem -out client.pem    openssl x509 -in ca.crt -out ca.der -outform der
   openssl x509 -in ca.der -inform der -outform pem -out ca_info.pem

  2)将 .key 转成 .pem

    不能直接将 .key 转成 .pem,需要经过 .der 中转    

   openssl rsa -in client.key -out client.der -outform DER
   openssl rsa -inform DER -outform PEM -in client.der -out client_key.pem

2、配置 curl https请求

  1) 官方例程如下:
  curl 接口文档说明:
   https://curl.haxx.se/libcurl/c/curl_easy_setopt.html
   https://curl.haxx.se/libcurl/c/CURLOPT_CAINFO.html
      https://curl.haxx.se/libcurl/c/https.html

  CURLOPT_CAINFO - path to Certificate Authority (CA) bundle
  CURLOPT_SSLKEY - specify private keyfile for TLS and SSL client cert
  CURLOPT_SSLCERT - set SSL client certificate

按下面代码部分进行配置,即可访问

    CURL *curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");   // 下面两个为验证对方和验证主机名,若为0,则跳过验证,我这个服务器必须验证才能得到请求数据
  curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 1L);
  curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, 1L);    
    
// 配置 https 请求所需证书
  curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/certs/cabundle.pem");
  curl_easy_setopt(curl, CURLOPT_SSLCERT, "client.pem");
  curl_easy_setopt(curl, CURLOPT_SSLKEY, "key.pem");
  curl_easy_setopt(curl, CURLOPT_KEYPASSWD, "s3cret");   ret = curl_easy_perform(curl);
  curl_easy_cleanup(curl);
  }

  2)我的代码如下

  这是一个完整的 curl 发送 https get 请求,并带中文参数  

#include <iostream>
#include <sstream>
#include <jsoncpp/json/json.h>
#include <curl/curl.h>
#include <exception>
#include <string>
#include <iostream>
#include <stdlib.h> int writer(char *data, size_t size, size_t nmemb, string *writerData)
{
  unsigned long sizes = size * nmemb;
  if (writerData == NULL)
  return -;   writerData->append(data, sizes);
  return sizes;
} string parseJsonResponse_question(string input)
{
Json::Value root;
Json::Reader reader;
bool parsingSuccessful = reader.parse(input, root);
if(!parsingSuccessful)
{
std::cout<<"!!! Failed to parse the response data"<< std::endl;
return "";
}
const Json::Value text = root["obj"]["question"];
string result = text.asString();
return result;
} string HttpsGetRequest_question(string input)
{
string buffer, ling_result; // 对请求参数中的中文和特殊字符(如空格等)进行处理,方可使用
char * escape_control = curl_escape(input.c_str(), input.size());
input = escape_control;
curl_free(escape_control); string str_url= "https://*.*.*.*/question?question=" + input; // alter *.*.*.* by your server address try
{
CURL *pCurl = NULL;
CURLcode res; // In windows, this will init the winsock stuff
curl_global_init(CURL_GLOBAL_ALL);
// get a curl handle
pCurl = curl_easy_init();
if (NULL != pCurl)
{
// 设置超时时间为8秒
curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, );
curl_easy_setopt(pCurl, CURLOPT_URL, str_url.c_str()); // 下面两个为验证对方和验证主机名,若为0,则跳过验证,我这个服务器必须验证才能得到请求数据
curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, 1L); // 配置 https 请求所需证书
curl_easy_setopt(pCurl,CURLOPT_CAINFO,"/etc/msc/ca_info.pem");
curl_easy_setopt(pCurl, CURLOPT_SSLCERT, "/etc/msc/client.pem");
curl_easy_setopt(pCurl, CURLOPT_SSLKEY, "/etc/msc/client_key.pem");
curl_easy_setopt(pCurl, CURLOPT_KEYPASSWD, "your_key_password"); curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, writer);
curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &buffer); // Perform the request, res will get the return code
res = curl_easy_perform(pCurl);
// Check for errors
if (res != CURLE_OK)
{
printf("curl_easy_perform() failed:%s\n", curl_easy_strerror(res));
}
curl_easy_cleanup(pCurl);
}
curl_global_cleanup();
}
catch (std::exception &ex)
{
printf("curl exception %s.\n", ex.what());
}
if(buffer.empty())
{
std::cout<< "!!! ERROR The sever response NULL" << std::endl;
}
else
{
ling_result = parseJsonResponse_question(buffer);
}
return ling_result;
}

参考:

  http://blog.csdn.net/rztyfx/article/details/6919220
  https://segmentfault.com/a/1190000011709784

linux c++ curl https 请求并双向验证SSL证书的更多相关文章

  1. 请求https前缀的网站验证SSL证书的解决方案之一

    from requests.packages.urllib3.exceptions import InsecureRequestWarning # 禁用安全请求警告requests.packages. ...

  2. Requests对HTTPS请求验证SSL证书

    SSL证书通过在客户端浏览器和Web服务器之间建立一条SSL安全通道(Secure socket layer(SSL)安全协议是由Netscape Communication公司设计开发.该安全协议主 ...

  3. .Net Core 发送https请求/.net core 调用数字证书 使用X509Certificate2

    .Net Core 发送https请求 .net core 调用数字证书 使用X509Certificate2 .NET下面的 .netfromwork使用和asp.net core下使用方式不一样 ...

  4. TortoiseGit 访问https远程仓库,上报SSL证书错误解决方法

    报错 在使用TortoiseGit时,clone自己搭建的gitlab报如错SSL certificate problem: self signed certificate 原因:自行搭建的gitla ...

  5. Https系列之一:https的简单介绍及SSL证书的生成

    Https系列会在下面几篇文章中分别作介绍: 一:https的简单介绍及SSL证书的生成二:https的SSL证书在服务器端的部署,基于tomcat,spring boot三:让服务器同时支持http ...

  6. linux下Tomcat+OpenSSL配置单向&双向认证(自制证书)

    背景 由于ios将在2017年1月1日起强制实施ATS安全策略,所有通讯必须使用https传输,本文只针对自制证书,但目前尚不确定自制证书是否能通过appstore审核. 1.必须支持传输层安全(TL ...

  7. linux tomcat【9.0.12】 使用 ssl证书 配置 https 的具体操作 【使用 域名 】

    1.前言 根据上一个随笔,已经可以正式在 阿里云服务器发布 工程了 ,但是用的协议默认是 http ,端口80 但是 http不安全 ,容易被拦截抓包 ,于是出来了个 https tomcat发布 对 ...

  8. [从零开始搭网站六]为域名申请免费SSL证书(https),并为Tomcat配置https域名所用的多SSL证书

    点击下面连接查看从零开始搭网站全系列 从零开始搭网站 由于国内的网络环境比较恶劣,运营商流量劫持的情况比较严重,一般表现为别人打开你的网站的时候会弹一些莫名其妙的广告...更过分的会跳转至别的网站. ...

  9. postman进行https接口测试所遇到的ssl证书问题,参考别人方法

    参考文档: https://learning.getpostman.com/docs/postman/sending_api_requests/certificates/ 随着 https 的推动,更 ...

随机推荐

  1. axf 文件包含太多的调试信息,导致的编译错误

    构建工程时,提示: build\my_test_prj.axf: Error: L6291E: Cannot assign Fixed Execution Region MCU_FLASH1 Load ...

  2. Kubernetes-Host网络模式应用

    目录贴:Kubernetes学习系列 在实际生产环境中,有些容器内应用(比如编码器)需要用到物理层面的网络资源(比如组播流).这就要求Kubernetes中的该Pod以HOST模式来启动.以下实验了K ...

  3. h5 实现定位

    直接上代码,代码使用了vue相关的语法 并且引入了dialog插件 ,使用时直接调用getLocation()方法就可以了! // 定位 function getLocation(){ console ...

  4. CentOS7查看systemctl 控制的服务的相关配置

    例如,启动配置文件 [root@Docker_Machine_192.168.31.130 ~]# systemctl show --property=FragmentPath docker Frag ...

  5. weex安装失败,按照官网步骤多次失败后成功

    在安装Weex Toolkit之前,需要确保安装了node, npm. yangfeifei:~ yff$ node -v v6.10.2 yangfeifei:~ yff$ npm -v 3.10. ...

  6. freeswitch 显示主叫名称和主叫号码

    1.指定主叫号码 origination_caller_id_number 参数来指定显示的主叫号码 2.指定主叫名称 origination_caller_id_name 参数来指定显示的主叫名称 ...

  7. webpack(import路径配置)(自动打开浏览器)(自定义运行命令)

  8. Redis setnx命令 分布式缓存

    setnx命令 将 key 的值设为 value,当且仅当 key 不存在. 若给定的 key 已经存在,则 SETNX 不做任何动作. SETNX 是SET if Not eXists的简写. re ...

  9. jQuery validator plugin之Methods

    step method Makes the element require a given step. step( value ) value Type: Number Step value requ ...

  10. public,private,protected,以及default时的区别

    作用域    当前类   同一package     子孙类   其他package public       √                 √                    √    ...