现在网络连接很多都是使用https连接来提高安全性,比如:淘宝,网银等。如何使用curl实现https连接?
首先导入curl.h和curlib.lib文件。
加入一下宏实现https连接
#define USE_OPENSSL
#define CURL_STATICLIB
#define USE_SSLEAY
整个cpp文件
#include <stdlib.h>
#include <iostream>
using namespace std;
#include "../../include/curl/curl.h"
#pragma comment(lib,"../../lib/curllib.lib")
#define USE_OPENSSL
#define CURL_STATICLIB
#define USE_SSLEAY
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://login.taobao.com/member/login.jhtml?f=top&redirectURL=http%3A%2F%2Fwww.taobao.com%2F");
//#ifdef SKIP_PEER_VERIFICATION
/*
* If you want to connect to a site who isn't using a certificate that is
* signed by one of the certs in the CA bundle you have, you can skip the
* verification of the server's certificate. This makes the connection
* A LOT LESS SECURE.
*
* If you have a CA cert for the server stored someplace else than in the
* default bundle, then the CURLOPT_CAPATH option might come handy for
* you.
*/
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
//#endif
//#ifdef SKIP_HOSTNAME_VERFICATION
/*
* If the site you're connecting to uses a different host name that what
* they have mentioned in their server certificate's commonName (or
* subjectAltName) fields, libcurl will refuse to connect. You can skip
* this check, but this will make the connection less secure.
*/
//curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
//#endif
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK){
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
curl_version_info_data * vinfo = curl_version_info(CURLVERSION_NOW);
if(vinfo->features & CURL_VERSION_SSL){
cout<<"CURL: SSL enabled"<<endl;
}else{
cout<<"CURL: SSL not enabled"<<endl;
}
/* always cleanup */
curl_easy_cleanup(curl);
return 0;
}
}
使用curl实现webqq httpget 和httppost
//HTTP get
string CQQLogin::HttpWebGet( string url, string referer_url )
{
string buffer;
string get_url = url;
// 初始化libcurl
// string referer_url = "http://s.web2.qq.com/proxy.html?v=20110412001&callback=1&id=2";
CURLcode return_code;
return_code = curl_global_init(CURL_GLOBAL_ALL);
if (CURLE_OK != return_code) return "";
// 获取easy handle
CURL *easy_handle = curl_easy_init();
if (NULL == easy_handle)
{
curl_global_cleanup();
return "";
}
// 设置easy handle属性
return_code = curl_easy_setopt(easy_handle, CURLOPT_URL, get_url.c_str());
curl_easy_setopt(easy_handle, CURLOPT_REFERER, referer_url.c_str());
curl_easy_setopt(easy_handle, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0)");
curl_easy_setopt(easy_handle, CURLOPT_ENCODING,"gzip, deflate" );
curl_easy_setopt(easy_handle, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, writer);
curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, &buffer);
curl_easy_setopt(easy_handle, CURLOPT_NOSIGNAL, 1L);
//curl_easy_setopt(easy_handle, CURLOPT_SSL_VERIFYPEER, 0L);
//提交第一步保存的cookie
return_code = curl_easy_setopt(easy_handle, CURLOPT_COOKIEFILE,"cookie_login.txt");
//把服务器发过来的cookie保存到cookie_open.txt
return_code = curl_easy_setopt(easy_handle, CURLOPT_COOKIEJAR, "cookie_login.txt");
// 执行数据请求
return_code = curl_easy_perform(easy_handle);
// 释放资源
curl_easy_cleanup(easy_handle);
curl_global_cleanup();
buffer = UTF8Convert(buffer,CP_UTF8,CP_ACP);
return buffer;
}
//HTTP post
string CQQLogin::HttpWebPost(string url,string Ref, string post)
{
CURLcode return_code;
return_code = curl_global_init(CURL_GLOBAL_ALL);
if (CURLE_OK != return_code) return NULL;
// 获取easy handle
CURL *easy_handle = curl_easy_init();
if (NULL == easy_handle)
{
curl_global_cleanup();
return NULL;
}
string buffer;
string post_url = url;
string referer_url = Ref;
string fields = post;
// 设置easy handle属性
curl_easy_setopt(easy_handle, CURLOPT_URL,post_url.c_str());
curl_easy_setopt(easy_handle, CURLOPT_REFERER,referer_url.c_str());
curl_easy_setopt(easy_handle, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0)");
curl_easy_setopt(easy_handle, CURLOPT_POST, 1);
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, writer);
curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, &buffer);
curl_easy_setopt(easy_handle, CURLOPT_POSTFIELDS,fields.c_str());
curl_easy_setopt(easy_handle, CURLOPT_NOSIGNAL, 1L);
return_code = curl_easy_setopt(easy_handle, CURLOPT_SSL_VERIFYPEER, 0L);
//提交第一步保存的cookie
curl_easy_setopt(easy_handle, CURLOPT_COOKIEFILE,"cookie_login.txt");
//保存登陆后的cookie
curl_easy_setopt(easy_handle, CURLOPT_COOKIEJAR,"cookie_login.txt");
curl_easy_setopt(easy_handle, CURLOPT_FOLLOWLOCATION, 1);
// 执行数据请求
return_code = curl_easy_perform(easy_handle);
buffer = UTF8Convert(buffer,CP_ACP,CP_UTF8);
// 释放资源
curl_easy_cleanup(easy_handle);
curl_global_cleanup();
return buffer;
}