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. python实现比对两个json串的方法

    记录瞬间 前段时间为了解决一些实际问题,引出了要对json字符串进行比对的需求. 觉得有意义,作以简单记录. # 比对数据 def compare_data(set_key, src_data, ds ...

  2. Html br 标签

    Html br 标签 <html> <body> <!-- br标签:跳到下一行--> <p>内<br />容</p> 注:br ...

  3. Linux 系统开启最大线程数 调优

    系统最大线程数说明 系统可开启的最大线程数,可根据系统本身负载配置进行调优. 查看系统最大线程数 1.查看系统开启的最大线程数. ulimit -u [root@izbp1brwu1w35r1dmj8 ...

  4. NSIS控制面板中显示安装包的大小和禁止多个安装程序实例

    转载:http://www.yhxs3344.net/jscript/nsis 转载:http://www.yhxs3344.net/archives/1292 1.控制面板中显示安装包的大小 ;需要 ...

  5. 输出JSON

    <?php header("Content-type: text/html; charset=utf-8"); $host = '数据库IP'; $dbname = '数据库 ...

  6. Bytom 移动端钱包SDK FAQ

    比原链移动端钱包SDK项目地址:https://github.com/Bytom-Community/Bytom-Mobile-Wallet-SDK 1.客户端钱包SDK需要实现哪些功能? 创建密钥. ...

  7. Java(1)JDK安装

    1.安装JDK开发环境 下载网站:http://www.oracle.com/ 开始安装JDK: 修改安装目录如下: 确定之后,单击"下一步". 注:当提示安装JRE时,可以选择不 ...

  8. SAP的软件维护费用,交还是不交?

    SAP的软件维护费用,交还是不交? 首先我们要明确一点,什么是软件维护费用?     软件维护费用是指在企业购买了软件厂商的软件产品之后,软件厂商每年按照一定比例向企业收取一定的技术支持维护费用.收取 ...

  9. ssh hibernate修改数据库

     org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in rea ...

  10. Codeforces 792 E. Colored Balls

    题目链接:http://codeforces.com/contest/792/problem/E 假设含球较少的那些堆有 $mi$ 个球,较多的那些堆有$ma$个球,$ma=mi+1$,考虑对于最小的 ...