zoukankan      html  css  js  c++  java
  • C++ curl跨平台HttpClient

    1. // FacePlusPlus.cpp : 定义控制台应用程序的入口点。  
    2. //  
    3.   
    4. #include "stdafx.h"  
    5.   
    6. #include <string>  
    7. #include <iostream>  
    8. using namespace std;  
    9. #include "HttpClient.h"  
    10. #include "curl/curl.h"  
    11. #include "curl/easy.h"  
    12. std::string strResult;  
    13.   
    14. std::string HTTPRESULT;  
    15.   
    16. void Write_data(void* buffer, size_t size, size_t nmemb, void* user_p){  
    17.     HTTPRESULT += (const char*)buffer;  
    18. }  
    19.   
    20. int _tmain(int argc, _TCHAR* argv[])  
    21. {  
    22.     /* 
    23.     HttpClient httpClient; 
    24.  
    25.     //httpClient.Post(HttpClient::URL_DETECT,"url=http://g.hiphotos.baidu.com/image/pic/item/0df431adcbef7609758498962cdda3cc7cd99e2f.jpg&api_secret=自己secret&api_key=自己key",strResult); 
    26.     std::map<std::string,std::string> mapPost; 
    27.     //mapPost["url"] = "http://g.hiphotos.baidu.com/image/pic/item/0df431adcbef7609758498962cdda3cc7cd99e2f.jpg"; 
    28.     mapPost["img"] = "c:\test.jpg"; 
    29.     httpClient.Post(HttpClient::URL_DETECT,mapPost,strResult);   
    30.     std::cout<<std::endl<<strResult<<std::endl; 
    31.     */  
    32.   
    33.     CURL *curl = curl_easy_init();  
    34.     CURLcode res = curl_global_init(CURL_GLOBAL_WIN32);  
    35.   
    36.     struct curl_httppost *formpost=NULL;  
    37.     struct curl_httppost *lastptr=NULL;  
    38.     //        struct curl_slist *headerlist=NULL;  
    39.     //        static const char buf[] = "Expect:";  
    40.   
    41.     curl_formadd(&formpost,  
    42.         &lastptr,  
    43.         CURLFORM_COPYNAME, "api_key",   
    44.         CURLFORM_COPYCONTENTS, "自己的key",   
    45.         CURLFORM_END);  
    46.     curl_formadd(&formpost,  
    47.         &lastptr,  
    48.         CURLFORM_COPYNAME, "api_secret",   
    49.         CURLFORM_COPYCONTENTS, "自己的secret",  
    50.         CURLFORM_END);  
    51.     /* 
    52.     curl_formadd(&formpost, 
    53.         &lastptr, 
    54.         CURLFORM_COPYNAME, "img",  
    55.         CURLFORM_FILE, "c:\test.png",  
    56.         CURLFORM_END); 
    57.         */  
    58.       
    59.     char* file_data = NULL;  
    60.     long file_size = 0;  
    61.       
    62.     FILE* fp = fopen("c:\test.jpg","rb");  
    63.     if (fp)  
    64.     {         
    65.         fseek(fp, 0, SEEK_END);  
    66.         file_size = ftell(fp);  
    67.         fseek(fp, 0, SEEK_SET);  
    68.         file_data = new char[file_size+1];  
    69.         fread(file_data,1,file_size,fp);  
    70.         cout<<file_data<<endl;  
    71.         fclose(fp);  
    72.     }  
    73.       
    74.       
    75.     curl_formadd(&formpost, &lastptr,  
    76.         CURLFORM_COPYNAME, "img",  
    77.         CURLFORM_BUFFER, "test.jpg",  
    78.         CURLFORM_BUFFERPTR, file_data,  
    79.         CURLFORM_BUFFERLENGTH, file_size,  
    80.         CURLFORM_CONTENTTYPE, "image/jpeg",  
    81.         CURLFORM_END);  
    82.           
    83.       
    84.     if(curl) {  
    85.         /* what URL that receives this POST */   
    86.         curl_easy_setopt(curl, CURLOPT_URL, "http://apicn.faceplusplus.com/v2/detection/detect");  
    87.         curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);  
    88.   
    89.         curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);  
    90.         curl_easy_setopt(curl, CURLOPT_WRITEDATA, &HTTPRESULT);  
    91.         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Write_data);  
    92.   
    93.         char error[1024];  
    94.         curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);  
    95.   
    96.         res = curl_easy_perform(curl);  
    97.         if(res != CURLE_OK) cout<<endl<<error<<endl;  
    98.     }  
    99.     curl_easy_cleanup(curl);  
    100.     curl_formfree(formpost);  
    101.     cout<<endl<<HTTPRESULT<<endl;  
    102.   
    103.     if(file_data != NULL)  
    104.         delete [] file_data;  
    105.     system("pause");  
    106.     return 0;  
    107. }  
    
    
    1. </pre><pre code_snippet_id="381082" snippet_file_name="blog_20140607_2_8925846" name="code" class="cpp"><pre name="code" class="html">  
    
    
    1. #include <string>  
    2. #include <map>  
    3.   
    4. class HttpClient  
    5. {  
    6. public:  
    7.     HttpClient(void);  
    8.     ~HttpClient(void);  
    9.   
    10. public:  
    11.   
    12.     /** 
    13.     * @brief HTTP POST请求 
    14.     * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com 
    15.     * @param mapPost 输入参数,key value 格式 
    16.     * @param strResponse 输出参数,返回的内容 
    17.     * @return 返回是否Post成功 
    18.     */  
    19.     int Post(const std::string & strUrl, const std::map<std::string,std::string> & mapPost, std::string & strResponse);  
    20.   
    21.     /** 
    22.     * @brief HTTP POST请求 
    23.     * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com 
    24.     * @param strPost 输入参数,使用如下格式para1=val1¶2=val2&… 
    25.     * @param strResponse 输出参数,返回的内容 
    26.     * @return 返回是否Post成功 
    27.     */  
    28.     int Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse);  
    29.   
    30.     /** 
    31.     * @brief HTTP GET请求 
    32.     * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com 
    33.     * @param strResponse 输出参数,返回的内容 
    34.     * @return 返回是否Post成功 
    35.     */  
    36.     int Get(const std::string & strUrl, std::string & strResponse);  
    37.   
    38.     /** 
    39.     * @brief HTTPS POST请求,无证书版本 
    40.     * @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com 
    41.     * @param strPost 输入参数,使用如下格式para1=val1¶2=val2&… 
    42.     * @param strResponse 输出参数,返回的内容 
    43.     * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性. 
    44.     * @return 返回是否Post成功 
    45.     */  
    46.     int Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath = NULL);  
    47.   
    48.     /** 
    49.     * @brief HTTPS GET请求,无证书版本 
    50.     * @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com 
    51.     * @param strResponse 输出参数,返回的内容 
    52.     * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性. 
    53.     * @return 返回是否Post成功 
    54.     */  
    55.     int Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath = NULL);  
    56.   
    57. public:  
    58.     static std::string FACE_KEY;  
    59.     static std::string FACE_SECRET;  
    60.   
    61.     static std::string URL_DETECT;   
    62.     static std::string URL_COMPARE;   
    63.     static std::string URL_RECOGNIZE;   
    64.     static std::string URL_SEARCH;   
    65.     static std::string URL_TRAIN;   
    66.     static std::string URL_VERIFY;   
    67.   
    68.     static std::string URL_PERSON_ADDFACE;  
    69.     static std::string URL_PERSON_CREATE;  
    70.     static std::string URL_PERSON_DELETE;  
    71.     static std::string URL_PERSON_GETINFO;  
    72.     static std::string URL_PERSON_REMOVEFACE;  
    73.     static std::string URL_PERSON_SETINFO;  
    74.   
    75.     static std::string URL_GROUP_ADDPERSON;  
    76.     static std::string URL_GROUP_CREATE;  
    77.     static std::string URL_GROUP_DELETE;  
    78.     static std::string URL_GROUP_GETINFO;  
    79.     static std::string URL_GROUP_REMOVEPERSON;  
    80.     static std::string URL_GROUP_SETINFO;  
    81.   
    82.     static std::string URL_INFO_GETAPP;  
    83.     static std::string URL_INFO_GETFACE;  
    84.     static std::string URL_INFO_GETGROUPLIST;  
    85.     static std::string URL_INFO_GETIMAGE;  
    86.     static std::string URL_INFO_GETPERSONLIST;  
    87.     static std::string URL_INFO_GETQUOTA;  
    88.     static std::string URL_INFO_GETSESSION;  
    89.     static std::string URL_INFO_GET_FACESETLIST;  
    90.   
    91.     static std::string URL_FACESET_CREATE;  
    92.     static std::string URL_FACESET_DELETE;  
    93.     static std::string URL_FACESET_ADDFACE;  
    94.     static std::string URL_FACESET_REMOVEFACE;  
    95.     static std::string URL_FACESET_SETINFO;  
    96.     static std::string URL_FACESET_GET_INFO;  
    97.   
    98.     static std::string URL_TRAIN_VERIFY;  
    99.     static std::string URL_TRAIN_SEARCH;  
    100.     static std::string URL_TRAIN_IDENTIFY;  
    101.     static std::string URL_GROUPING_GROUPING;  
    102. };  
    103.   
    104. #endif  

    httpClient.cpp
    1. #include "httpclient.h"  
    2. #include "curl/curl.h"  
    3. #include <string>  
    4.   
    5. std::string HttpClient::FACE_KEY                    = "自己key";  
    6. std::string HttpClient::FACE_SECRET                 = "自己secret";  
    7. std::string HttpClient::URL_DETECT                  = "http://apicn.faceplusplus.com/v2/detection/detect";   
    8. std::string HttpClient::URL_COMPARE                 = "http://apicn.faceplusplus.com/v2/recognition/compare";   
    9. std::string HttpClient::URL_RECOGNIZE               = "http://apicn.faceplusplus.com/v2/recognition/recognize";   
    10. std::string HttpClient::URL_SEARCH                  = "http://apicn.faceplusplus.com/v2/recognition/search";   
    11. std::string HttpClient::URL_TRAIN                   = "http://apicn.faceplusplus.com/v2/recognition/train";   
    12. std::string HttpClient::URL_VERIFY                  = "http://apicn.faceplusplus.com/v2/recognition/verify";   
    13.   
    14. std::string HttpClient::URL_PERSON_ADDFACE          = "http://apicn.faceplusplus.com/v2/person/add_face";  
    15. std::string HttpClient::URL_PERSON_CREATE           = "http://apicn.faceplusplus.com/v2/person/create";  
    16. std::string HttpClient::URL_PERSON_DELETE           = "http://apicn.faceplusplus.com/v2/person/delete";  
    17. std::string HttpClient::URL_PERSON_GETINFO          = "http://apicn.faceplusplus.com/v2/person/get_info";  
    18. std::string HttpClient::URL_PERSON_REMOVEFACE       = "http://apicn.faceplusplus.com/v2/person/remove_face";  
    19. std::string HttpClient::URL_PERSON_SETINFO          = "http://apicn.faceplusplus.com/v2/person/set_info";  
    20.   
    21. std::string HttpClient::URL_GROUP_ADDPERSON         = "http://apicn.faceplusplus.com/v2/group/add_person";  
    22. std::string HttpClient::URL_GROUP_CREATE            = "http://apicn.faceplusplus.com/v2/group/create";  
    23. std::string HttpClient::URL_GROUP_DELETE            = "http://apicn.faceplusplus.com/v2/group/delete";  
    24. std::string HttpClient::URL_GROUP_GETINFO           = "http://apicn.faceplusplus.com/v2/group/get_info";  
    25. std::string HttpClient::URL_GROUP_REMOVEPERSON      = "http://apicn.faceplusplus.com/v2/group/remove_person";  
    26. std::string HttpClient::URL_GROUP_SETINFO           = "http://apicn.faceplusplus.com/v2/group/set_info";  
    27.   
    28. std::string HttpClient::URL_INFO_GETAPP             = "http://apicn.faceplusplus.com/v2/info/get_app";  
    29. std::string HttpClient::URL_INFO_GETFACE            = "http://apicn.faceplusplus.com/v2/info/get_face";  
    30. std::string HttpClient::URL_INFO_GETGROUPLIST       = "http://apicn.faceplusplus.com/v2/info/get_group_list";  
    31. std::string HttpClient::URL_INFO_GETIMAGE           = "http://apicn.faceplusplus.com/v2/info/get_image";  
    32. std::string HttpClient::URL_INFO_GETPERSONLIST      = "http://apicn.faceplusplus.com/v2/info/get_person_list";  
    33. std::string HttpClient::URL_INFO_GETQUOTA           = "http://apicn.faceplusplus.com/v2/info/get_quota";  
    34. std::string HttpClient::URL_INFO_GETSESSION         = "http://apicn.faceplusplus.com/v2/info/get_session";  
    35. std::string HttpClient::URL_INFO_GET_FACESETLIST    = "http://apicn.faceplusplus.com/v2/info/get_faceset_list";  
    36.   
    37. std::string HttpClient::URL_FACESET_CREATE          = "http://apicn.faceplusplus.com/v2/faceset/create";  
    38. std::string HttpClient::URL_FACESET_DELETE          = "http://apicn.faceplusplus.com/v2/faceset/delete";  
    39. std::string HttpClient::URL_FACESET_ADDFACE         = "http://apicn.faceplusplus.com/v2/faceset/add_face";  
    40. std::string HttpClient::URL_FACESET_REMOVEFACE      = "http://apicn.faceplusplus.com/v2/faceset/remove_face";  
    41. std::string HttpClient::URL_FACESET_SETINFO         = "http://apicn.faceplusplus.com/v2/faceset/set_info";  
    42. std::string HttpClient::URL_FACESET_GET_INFO        = "http://apicn.faceplusplus.com/v2/faceset/get_info";  
    43.   
    44. std::string HttpClient::URL_TRAIN_VERIFY            = "http://apicn.faceplusplus.com/v2/train/verify";  
    45. std::string HttpClient::URL_TRAIN_SEARCH            = "http://apicn.faceplusplus.com/v2/train/search";  
    46. std::string HttpClient::URL_TRAIN_IDENTIFY          = "http://apicn.faceplusplus.com/v2/train/identify";  
    47. std::string HttpClient::URL_GROUPING_GROUPING       = "http://apicn.faceplusplus.com/v2/grouping/grouping";  
    48.   
    49. HttpClient::HttpClient(void)  
    50. {  
    51.   
    52. }  
    53.   
    54. HttpClient::~HttpClient(void)  
    55. {  
    56.   
    57. }  
    58.   
    59. static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)  
    60. {  
    61.     std::string* str = dynamic_cast<std::string*>((std::string *)lpVoid);  
    62.     if( NULL == str || NULL == buffer )  
    63.     {  
    64.         return -1;  
    65.     }  
    66.   
    67.     char* pData = (char*)buffer;  
    68.     str->append(pData, size * nmemb);  
    69.     return nmemb;  
    70. }  
    71.   
    72. int HttpClient::Post(const std::string & strUrl, const std::map<std::string,std::string> & mapPost, std::string & strResponse)  
    73. {  
    74.     std::string strPost = "api_secret="+FACE_SECRET+"&api_key="+FACE_KEY;  
    75.     std::map<std::string,std::string>::const_iterator it = mapPost.begin();  
    76.     while (it != mapPost.end())  
    77.     {     
    78.         strPost += "&";  
    79.         strPost += it->first;  
    80.         strPost += "=";  
    81.         strPost += it->second;  
    82.         it++;  
    83.     }  
    84.     return Post(strUrl,strPost,strResponse);  
    85. }  
    86.   
    87. int HttpClient::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse)  
    88. {  
    89.     CURLcode res;  
    90.     CURL* curl = curl_easy_init();  
    91.     if(NULL == curl)  
    92.     {  
    93.         return CURLE_FAILED_INIT;  
    94.     }  
    95.     curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());  
    96.     curl_easy_setopt(curl, CURLOPT_POST, 1);  
    97.     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());  
    98.     curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);  
    99.     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);  
    100.     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);  
    101.     curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);  
    102.     curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);  
    103.     curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);  
    104.     res = curl_easy_perform(curl);  
    105.     curl_easy_cleanup(curl);  
    106.     return res;  
    107. }  
    108.   
    109. int HttpClient::Get(const std::string & strUrl, std::string & strResponse)  
    110. {  
    111.     CURLcode res;  
    112.     CURL* curl = curl_easy_init();  
    113.     if(NULL == curl)  
    114.     {  
    115.         return CURLE_FAILED_INIT;  
    116.     }  
    117.     curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());  
    118.     curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);  
    119.     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);  
    120.     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);  
    121.     /** 
    122.     * 当多个线程都使用超时处理的时候,同时主线程中有sleep或是wait等操作。 
    123.     * 如果不设置这个选项,libcurl将会发信号打断这个wait从而导致程序退出。 
    124.     */  
    125.     curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);  
    126.     curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);  
    127.     curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);  
    128.     res = curl_easy_perform(curl);  
    129.     curl_easy_cleanup(curl);  
    130.     return res;  
    131. }  
    132.   
    133. int HttpClient::Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath)  
    134. {  
    135.     CURLcode res;  
    136.     CURL* curl = curl_easy_init();  
    137.     if(NULL == curl)  
    138.     {  
    139.         return CURLE_FAILED_INIT;  
    140.     }  
    141.     curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());  
    142.     curl_easy_setopt(curl, CURLOPT_POST, 1);  
    143.     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());  
    144.     curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);  
    145.     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);  
    146.     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);  
    147.     curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);  
    148.     if(NULL == pCaPath)  
    149.     {  
    150.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);  
    151.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);  
    152.     }  
    153.     else  
    154.     {  
    155.         //缺省情况就是PEM,所以无需设置,另外支持DER  
    156.         //curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");  
    157.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);  
    158.         curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);  
    159.     }  
    160.     curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);  
    161.     curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);  
    162.     res = curl_easy_perform(curl);  
    163.     curl_easy_cleanup(curl);  
    164.     return res;  
    165. }  
    166.   
    167. int HttpClient::Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath)  
    168. {  
    169.     CURLcode res;  
    170.     CURL* curl = curl_easy_init();  
    171.     if(NULL == curl)  
    172.     {  
    173.         return CURLE_FAILED_INIT;  
    174.     }  
    175.     curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());  
    176.     curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);  
    177.     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);  
    178.     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);  
    179.     curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);  
    180.     if(NULL == pCaPath)  
    181.     {  
    182.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);  
    183.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);  
    184.     }  
    185.     else  
    186.     {  
    187.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);  
    188.         curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);  
    189.     }  
    190.     curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);  
    191.     curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);  
    192.     res = curl_easy_perform(curl);  
    193.     curl_easy_cleanup(curl);  
    194.     return res;  
    195. }  

  • 相关阅读:
    词云(WordCloud)
    Pandas常用方法
    PCA降维的原理及实现
    支持向量机(SVM)公式整理
    《小狗钱钱》
    初识 Netty
    hello world
    算法-归并排序
    算法-堆与堆排序
    Java并发编程05-线程池
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318033.html
Copyright © 2011-2022 走看看