libcurl使用认证证书 https认证
2015-12-11 15:29
375 查看
在server.xml中增加下面的内容:
[cpp] viewplaincopy
ciphers="SSL_RSA_WITH_RC4_128_SHA"
下面是libcurl 的测试代码:
[cpp] viewplaincopy
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
static size_t save_response_callback(void *buffer,size_t size,size_t count,void **response)
{
char * ptr = NULL;
printf("buffer is %s\n",(char *)buffer);
ptr =(char *) malloc(count*size + 4);
memcpy(ptr,buffer,count*size);
*response = ptr;
return count;
}
int main(int argc,char *argv[])
{
CURL * curl;
CURLcode res;
char * response = NULL;
if(argc !=2){
printf("Usage:file<url>;\n");
return;
}
//curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl!=NULL){
printf("Usage:file<%s>;\n",argv[1]);
curl_easy_setopt(curl,CURLOPT_URL,argv[1]);
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,&save_response_callback);
curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response);
curl_easy_setopt(curl,CURLOPT_COOKIESESSION,1L);
curl_easy_setopt(curl,CURLOPT_COOKIEFILE,"/dev/null");
curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,1);
//curl_easy_setopt(curl,CURLOPT_CAPATH,"/etc/ssl/cert/");
curl_easy_setopt(curl,CURLOPT_CAINFO,"ca-cert.pem");
curl_easy_setopt(curl,CURLOPT_SSL_VERIFYHOST,1);
curl_easy_setopt(curl,CURLOPT_VERBOSE,1L);
curl_easy_setopt(curl,CURLOPT_TIMEOUT,30);
#if 0
/* 双向验证下面是客户端的CA*/
// curl_easy_setopt(curl,CURLOPT_CAPATH,"./");
curl_easy_setopt(curl,CURLOPT_SSLCERT,"client-cert.pem");
curl_easy_setopt(curl,CURLOPT_SSLCERTPASSWD,"password");
curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
curl_easy_setopt(curl,CURLOPT_SSLKEY,"client-key.pem");
curl_easy_setopt(curl,CURLOPT_SSLKEYPASSWD,"password");
curl_easy_setopt(curl,CURLOPT_SSLKEYTYPE,"PEM");
#endif
res = curl_easy_perform(curl);
if(res != CURLE_OK){
printf("curl_wasy_perform error = %s",curl_easy_strerror(res));
}
printf("response<%s>\n",response);
curl_easy_cleanup(curl);
}
}
参考资料:
源码测试用例:http://curl.haxx.se/libcurl/c/https.html
教程:http://blog.csdn.net/jgood/article/details/4787670
SSL证书制作:http://blog.chinaunix.net/uid-7591044-id-1742977.html
相关文章推荐
- Android Asynchronous Http Client-Android异步网络请求客户端接口
- Linux 下curl模拟Http 的get or post请求
- 如何解决XMLHttpRequest cannot load file~~~~~~~Origin 'null' is therefore not allowed access
- 黑马程序员——网络编程
- 使用xutils请求网络参数为json字符串时报错IOException
- TCP 和 UDP 能否使用相同的端口号?
- http://www.iconfont.cn/
- 网络应用的编程模型
- Android Asynchronous Http Client
- PHP漏洞全解(七)-HTTP响应拆分
- iOS开源项目大全(一)(整理自网络)
- 简单的网络协议介绍
- iOS开源项目大全(二)(整理自网络)
- Android网络编程---HttpURLConnection
- 处理TCP连包的一小段代码
- spring(基础十) Spring HTTP Invoker使用介绍
- 用Xutils 加载网络图片。设置Appliction
- linux使用wget下载https开头url的文件
- TCP三次握手
- weblogic.net.http.SOAPHttpsURLConnection cannot be cast to javax.net.ssl.HttpsURLConnection