RuningPigNO1 2018-04-08 01:54 采纳率: 50%
浏览 4735
已采纳

libcurl 进行HTTPS进行双向认证返回CURLE_SSL_CONNECT_ERROR失败

最近公司项目需要SSL双向认证,我就用libcurl来实现,但是在做Demo的时候请求是没问题的,可是吧代码放到主程序(windows下 Unicode字符集)里面,返回的总是 CURLE_SSL_CONNECT_ERROR不知道是哪里出问题了,下面是源码
int CUrlHttp::Request(std::string strRequestType,
std::string strUrl,
std::string &strReport,
std::string &strRetHeader,
std::vectorstd::string vecHeader,
std::string strParam/* ="" /,
std::string strCookie/
="" /,
std::string strCaPath/
="" /,
int nTimeOut/
=0 */)
{
strUrl = "https://192.168.5.176:4433/ipass00/http/localhost/welcome";
CURL * curl;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());

if ( strRequestType.compare("post")==0 || strRequestType.compare("POST") == 0 )
{
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(strParam.c_str()));//post内容长度
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strParam.c_str());
}
else
{
    curl_easy_setopt(curl, CURLOPT_POST, 0);//get请求
}

//设置http头
curl_slist * headers = NULL;
for ( int i=0; i<vecHeader.size(); i++ )
{
    if (!vecHeader.at(i).empty())
    {
        headers = curl_slist_append(headers, vecHeader.at(i).c_str());
    }
}
if (headers != NULL)
{
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
}

curl_easy_setopt(curl,CURLOPT_SSLVERSION,1);

//判断是否有证书
if(!strCaPath.empty())
{
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
}
else
{
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, true);
    curl_easy_setopt(curl,CURLOPT_CAINFO,"C:/ssl/cacert.pem");

    curl_easy_setopt(curl,CURLOPT_SSLCERT,"C:/ssl/client.pem");  
    curl_easy_setopt(curl,CURLOPT_SSLCERTPASSWD,"11111111");  
    curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");  
    curl_easy_setopt(curl,CURLOPT_SSLKEY,"C:/ssl/clientkey.pem");  
    curl_easy_setopt(curl,CURLOPT_SSLKEYPASSWD,"11111111");  
    curl_easy_setopt(curl,CURLOPT_SSLKEYTYPE,"PEM");


}

//Web服务器一般会重定向链接,比如访问http:/xxx/x1.do自动转到http:/xxx/x2.do  
//所以一定要设置CURLOPT_FOLLOWLOCATION为1,否则重定向后的数据不会返回。  
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,1); 

curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);     //可以看到调试信息

curl_easy_setopt(curl,CURLOPT_HEADERFUNCTION,_CURL_::write_data);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &strRetHeader);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _CURL_::write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strReport); 
if ( nTimeOut > 0  )
{
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, nTimeOut);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, nTimeOut);
}

if (!strCookie.empty())
{

    curl_easy_setopt(curl, CURLOPT_COOKIEFILE, strCookie.c_str());
}

CURLcode code = curl_easy_perform(curl);
if(code != CURLE_OK){  

    printf("curl_wasy_perform error = %s",curl_easy_strerror(code));  
}  
if ( headers != NULL )
{
    curl_slist_free_all(headers);
}
curl_easy_cleanup(curl);
//打印出来

// std::string strReportData;
// strReportData.append(strReportHeader);
// strReportData.append(strReport);
// TRACE("request:%s url:%s report:%s", strRequestType.c_str(), strUrl.c_str(), strReportData.c_str());
return code;
}

  • 写回答

3条回答 默认 最新

  • RuningPigNO1 2018-04-10 01:37
    关注

    无奈自己结帖,其实原因很简单,项目中之前用到过libcurl,之前的版本和现在的版本冲突了才导致请求不成功

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?