02_libcurl

这个没写太多,有问题看官方文档说明和例子,在这个基础上改是没问题了。

一、libcurl官网

官网:
https://curl.haxx.se/libcurl/c/curl_easy_setopt.html
post的例子:
http://m.blog.csdn.net/lacoucou/article/details/56011326

二、libcurl和http协议

http协议与libcurl密不可分,所以必须先了解了http协议和https之后才能对这个libcurl库的应用有进一步的了解。

三、post例子

static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
{
    std::string* str = dynamic_cast<std::string*>((std::string *)lpVoid);
    if( NULL == str || NULL == buffer )
    {
        return -1;
    }

    char* pData = (char*)buffer;
    str->append(pData, size * nmemb);
    return nmemb;
}


int Http( std::string &header , bool flag , const std::string & strUrl,  std::string & strResponse,  
         enum HttpAction action,const std::string  strPost, 
         enum HeadFormat  headFormat,
        int connectionTimeout,int timeout)
{
    CURLcode res;
    curl_global_init(CURL_GLOBAL_ALL);
    CURL* curl = curl_easy_init();
    if(NULL == curl)
    {
        return CURLE_FAILED_INIT;
    }
    try
    {
        struct curl_slist *headers = NULL;
        if(JSON == headFormat)
        {
            headers = curl_slist_append(headers, "Accept: application/json");
            headers = curl_slist_append(headers, "Content-Type: application/json");
            headers = curl_slist_append(headers, "Charsets: utf-8");
        }
        else if(JSONUrlEncode == headFormat)
        {
            qDebug() << "Content-Type: application/x-www-form-urlencoded" ;
            headers = curl_slist_append(headers, "Accept: application/json");
            //headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
            //官方文档说:如果指定post传输,指定 CURLOPT_POSTFIELDS时,要指定指定发送的大小: CURLOPT_POSTFIELDSIZE,如果不想指定大小就要指定传输编码chunked
            headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");//通知服务器解析浏览器报文body的格式
            headers = curl_slist_append(headers, "charsets: utf-8");
        }
        else
        {
            headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
            headers = curl_slist_append(headers, "charsets: utf-8");
        }
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        curl_easy_setopt(curl,CURLOPT_HTTPAUTH,CURLAUTH_BASIC);//http auto basic验证开启
        string usepwd= APIKEY+":"+"" ;//AppKey+":"+Password;开发要求验证密码为空
        qDebug()<< "use pwd = " <<QString::fromStdString(usepwd) ;
        curl_easy_setopt(curl,CURLOPT_USERPWD,usepwd.c_str());//填写http auto basic的客户名和密码
        if( !flag )
        {  
            qDebug() << "ssl no" ;
            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0 ); //0:https跳过ssl证书检查 ,默认是1
            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);  
        }  
        else  
        {  
            qDebug() << "ssl" ;
            //缺省情况就是PEM,所以无需设置,另外支持DER  
            curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");  
            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);  
        //  curl_easy_setopt(curl, CURLOPT_CAINFO, CaPath.c_str());  指定检查证书的路径
        } 
        qDebug() << "url:" << strUrl.c_str() ;
        curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());//post的url="https://test-pos.guopika.com/api/v2/pay"
        if(POST == action || POSTS == action)
        {
            qDebug() << "POST" ;
            curl_easy_setopt(curl, CURLOPT_POST, 1);//1、设置post请求,0设置为GET请求
            if(strPost.length() > 0) {
                qDebug()<<"data:"<< strPost.c_str() ;
        //      curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) strlen(strPost.c_str()));
                curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str()) ;//发送请求内容:"payment_code="+paycode+"&"+"total_amount="+totalAmt+"&"+"request_id="+"9196";
            }
        }
        curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
        curl_easy_setopt(curl, CURLOPT_WRITEHEADER, (void*)&header );//返回请求头部结果
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);//返回请求内容结果
        curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
        curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, connectionTimeout);
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
        res = curl_easy_perform(curl);
    }
    catch(...)
    {
        res=CURLE_SEND_ERROR;
    }
    curl_easy_cleanup(curl);
    curl_global_cleanup(); 
    return res;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值