- // FacePlusPlus.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <string>
- #include <iostream>
- using namespace std;
- #include "HttpClient.h"
- #include "curl/curl.h"
- #include "curl/easy.h"
- std::string strResult;
- std::string HTTPRESULT;
- void Write_data(void* buffer, size_t size, size_t nmemb, void* user_p){
- HTTPRESULT += (const char*)buffer;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- /*
- HttpClient httpClient;
- //httpClient.Post(HttpClient::URL_DETECT,"url=http://g.hiphotos.baidu.com/image/pic/item/0df431adcbef7609758498962cdda3cc7cd99e2f.jpg&api_secret=自己secret&api_key=自己key",strResult);
- std::map<std::string,std::string> mapPost;
- //mapPost["url"] = "http://g.hiphotos.baidu.com/image/pic/item/0df431adcbef7609758498962cdda3cc7cd99e2f.jpg";
- mapPost["img"] = "c:\\test.jpg";
- httpClient.Post(HttpClient::URL_DETECT,mapPost,strResult);
- std::cout<<std::endl<<strResult<<std::endl;
- */
- CURL *curl = curl_easy_init();
- CURLcode res = curl_global_init(CURL_GLOBAL_WIN32);
- struct curl_httppost *formpost=NULL;
- struct curl_httppost *lastptr=NULL;
- // struct curl_slist *headerlist=NULL;
- // static const char buf[] = "Expect:";
- curl_formadd(&formpost,
- &lastptr,
- CURLFORM_COPYNAME, "api_key",
- CURLFORM_COPYCONTENTS, "自己的key",
- CURLFORM_END);
- curl_formadd(&formpost,
- &lastptr,
- CURLFORM_COPYNAME, "api_secret",
- CURLFORM_COPYCONTENTS, "自己的secret",
- CURLFORM_END);
- /*
- curl_formadd(&formpost,
- &lastptr,
- CURLFORM_COPYNAME, "img",
- CURLFORM_FILE, "c:\\test.png",
- CURLFORM_END);
- */
- char* file_data = NULL;
- long file_size = 0;
- FILE* fp = fopen("c:\\test.jpg","rb");
- if (fp)
- {
- fseek(fp, 0, SEEK_END);
- file_size = ftell(fp);
- fseek(fp, 0, SEEK_SET);
- file_data = new char[file_size+1];
- fread(file_data,1,file_size,fp);
- cout<<file_data<<endl;
- fclose(fp);
- }
- curl_formadd(&formpost, &lastptr,
- CURLFORM_COPYNAME, "img",
- CURLFORM_BUFFER, "test.jpg",
- CURLFORM_BUFFERPTR, file_data,
- CURLFORM_BUFFERLENGTH, file_size,
- CURLFORM_CONTENTTYPE, "image/jpeg",
- CURLFORM_END);
- if(curl) {
- /* what URL that receives this POST */
- curl_easy_setopt(curl, CURLOPT_URL, "http://apicn.faceplusplus.com/v2/detection/detect");
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
- curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &HTTPRESULT);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Write_data);
- char error[1024];
- curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
- res = curl_easy_perform(curl);
- if(res != CURLE_OK) cout<<endl<<error<<endl;
- }
- curl_easy_cleanup(curl);
- curl_formfree(formpost);
- cout<<endl<<HTTPRESULT<<endl;
- if(file_data != NULL)
- delete [] file_data;
- system("pause");
- return 0;
- }
- </pre><pre code_snippet_id="381082" snippet_file_name="blog_20140607_2_8925846" name="code" class="cpp"><pre name="code" class="html">
- #include <string>
- #include <map>
- class HttpClient
- {
- public:
- HttpClient(void);
- ~HttpClient(void);
- public:
- /**
- * @brief HTTP POST请求
- * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com
- * @param mapPost 输入参数,key value 格式
- * @param strResponse 输出参数,返回的内容
- * @return 返回是否Post成功
- */
- int Post(const std::string & strUrl, const std::map<std::string,std::string> & mapPost, std::string & strResponse);
- /**
- * @brief HTTP POST请求
- * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com
- * @param strPost 输入参数,使用如下格式para1=val1¶2=val2&…
- * @param strResponse 输出参数,返回的内容
- * @return 返回是否Post成功
- */
- int Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse);
- /**
- * @brief HTTP GET请求
- * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com
- * @param strResponse 输出参数,返回的内容
- * @return 返回是否Post成功
- */
- int Get(const std::string & strUrl, std::string & strResponse);
- /**
- * @brief HTTPS POST请求,无证书版本
- * @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com
- * @param strPost 输入参数,使用如下格式para1=val1¶2=val2&…
- * @param strResponse 输出参数,返回的内容
- * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
- * @return 返回是否Post成功
- */
- int Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath = NULL);
- /**
- * @brief HTTPS GET请求,无证书版本
- * @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com
- * @param strResponse 输出参数,返回的内容
- * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
- * @return 返回是否Post成功
- */
- int Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath = NULL);
- public:
- static std::string FACE_KEY;
- static std::string FACE_SECRET;
- static std::string URL_DETECT;
- static std::string URL_COMPARE;
- static std::string URL_RECOGNIZE;
- static std::string URL_SEARCH;
- static std::string URL_TRAIN;
- static std::string URL_VERIFY;
- static std::string URL_PERSON_ADDFACE;
- static std::string URL_PERSON_CREATE;
- static std::string URL_PERSON_DELETE;
- static std::string URL_PERSON_GETINFO;
- static std::string URL_PERSON_REMOVEFACE;
- static std::string URL_PERSON_SETINFO;
- static std::string URL_GROUP_ADDPERSON;
- static std::string URL_GROUP_CREATE;
- static std::string URL_GROUP_DELETE;
- static std::string URL_GROUP_GETINFO;
- static std::string URL_GROUP_REMOVEPERSON;
- static std::string URL_GROUP_SETINFO;
- static std::string URL_INFO_GETAPP;
- static std::string URL_INFO_GETFACE;
- static std::string URL_INFO_GETGROUPLIST;
- static std::string URL_INFO_GETIMAGE;
- static std::string URL_INFO_GETPERSONLIST;
- static std::string URL_INFO_GETQUOTA;
- static std::string URL_INFO_GETSESSION;
- static std::string URL_INFO_GET_FACESETLIST;
- static std::string URL_FACESET_CREATE;
- static std::string URL_FACESET_DELETE;
- static std::string URL_FACESET_ADDFACE;
- static std::string URL_FACESET_REMOVEFACE;
- static std::string URL_FACESET_SETINFO;
- static std::string URL_FACESET_GET_INFO;
- static std::string URL_TRAIN_VERIFY;
- static std::string URL_TRAIN_SEARCH;
- static std::string URL_TRAIN_IDENTIFY;
- static std::string URL_GROUPING_GROUPING;
- };
- #endif
httpClient.cpp
- #include "httpclient.h"
- #include "curl/curl.h"
- #include <string>
- std::string HttpClient::FACE_KEY = "自己key";
- std::string HttpClient::FACE_SECRET = "自己secret";
- std::string HttpClient::URL_DETECT = "http://apicn.faceplusplus.com/v2/detection/detect";
- std::string HttpClient::URL_COMPARE = "http://apicn.faceplusplus.com/v2/recognition/compare";
- std::string HttpClient::URL_RECOGNIZE = "http://apicn.faceplusplus.com/v2/recognition/recognize";
- std::string HttpClient::URL_SEARCH = "http://apicn.faceplusplus.com/v2/recognition/search";
- std::string HttpClient::URL_TRAIN = "http://apicn.faceplusplus.com/v2/recognition/train";
- std::string HttpClient::URL_VERIFY = "http://apicn.faceplusplus.com/v2/recognition/verify";
- std::string HttpClient::URL_PERSON_ADDFACE = "http://apicn.faceplusplus.com/v2/person/add_face";
- std::string HttpClient::URL_PERSON_CREATE = "http://apicn.faceplusplus.com/v2/person/create";
- std::string HttpClient::URL_PERSON_DELETE = "http://apicn.faceplusplus.com/v2/person/delete";
- std::string HttpClient::URL_PERSON_GETINFO = "http://apicn.faceplusplus.com/v2/person/get_info";
- std::string HttpClient::URL_PERSON_REMOVEFACE = "http://apicn.faceplusplus.com/v2/person/remove_face";
- std::string HttpClient::URL_PERSON_SETINFO = "http://apicn.faceplusplus.com/v2/person/set_info";
- std::string HttpClient::URL_GROUP_ADDPERSON = "http://apicn.faceplusplus.com/v2/group/add_person";
- std::string HttpClient::URL_GROUP_CREATE = "http://apicn.faceplusplus.com/v2/group/create";
- std::string HttpClient::URL_GROUP_DELETE = "http://apicn.faceplusplus.com/v2/group/delete";
- std::string HttpClient::URL_GROUP_GETINFO = "http://apicn.faceplusplus.com/v2/group/get_info";
- std::string HttpClient::URL_GROUP_REMOVEPERSON = "http://apicn.faceplusplus.com/v2/group/remove_person";
- std::string HttpClient::URL_GROUP_SETINFO = "http://apicn.faceplusplus.com/v2/group/set_info";
- std::string HttpClient::URL_INFO_GETAPP = "http://apicn.faceplusplus.com/v2/info/get_app";
- std::string HttpClient::URL_INFO_GETFACE = "http://apicn.faceplusplus.com/v2/info/get_face";
- std::string HttpClient::URL_INFO_GETGROUPLIST = "http://apicn.faceplusplus.com/v2/info/get_group_list";
- std::string HttpClient::URL_INFO_GETIMAGE = "http://apicn.faceplusplus.com/v2/info/get_image";
- std::string HttpClient::URL_INFO_GETPERSONLIST = "http://apicn.faceplusplus.com/v2/info/get_person_list";
- std::string HttpClient::URL_INFO_GETQUOTA = "http://apicn.faceplusplus.com/v2/info/get_quota";
- std::string HttpClient::URL_INFO_GETSESSION = "http://apicn.faceplusplus.com/v2/info/get_session";
- std::string HttpClient::URL_INFO_GET_FACESETLIST = "http://apicn.faceplusplus.com/v2/info/get_faceset_list";
- std::string HttpClient::URL_FACESET_CREATE = "http://apicn.faceplusplus.com/v2/faceset/create";
- std::string HttpClient::URL_FACESET_DELETE = "http://apicn.faceplusplus.com/v2/faceset/delete";
- std::string HttpClient::URL_FACESET_ADDFACE = "http://apicn.faceplusplus.com/v2/faceset/add_face";
- std::string HttpClient::URL_FACESET_REMOVEFACE = "http://apicn.faceplusplus.com/v2/faceset/remove_face";
- std::string HttpClient::URL_FACESET_SETINFO = "http://apicn.faceplusplus.com/v2/faceset/set_info";
- std::string HttpClient::URL_FACESET_GET_INFO = "http://apicn.faceplusplus.com/v2/faceset/get_info";
- std::string HttpClient::URL_TRAIN_VERIFY = "http://apicn.faceplusplus.com/v2/train/verify";
- std::string HttpClient::URL_TRAIN_SEARCH = "http://apicn.faceplusplus.com/v2/train/search";
- std::string HttpClient::URL_TRAIN_IDENTIFY = "http://apicn.faceplusplus.com/v2/train/identify";
- std::string HttpClient::URL_GROUPING_GROUPING = "http://apicn.faceplusplus.com/v2/grouping/grouping";
- HttpClient::HttpClient(void)
- {
- }
- HttpClient::~HttpClient(void)
- {
- }
- 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 HttpClient::Post(const std::string & strUrl, const std::map<std::string,std::string> & mapPost, std::string & strResponse)
- {
- std::string strPost = "api_secret="+FACE_SECRET+"&api_key="+FACE_KEY;
- std::map<std::string,std::string>::const_iterator it = mapPost.begin();
- while (it != mapPost.end())
- {
- strPost += "&";
- strPost += it->first;
- strPost += "=";
- strPost += it->second;
- it++;
- }
- return Post(strUrl,strPost,strResponse);
- }
- int HttpClient::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse)
- {
- CURLcode res;
- CURL* curl = curl_easy_init();
- if(NULL == curl)
- {
- return CURLE_FAILED_INIT;
- }
- curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
- curl_easy_setopt(curl, CURLOPT_POST, 1);
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
- curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
- 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_CONNECTTIMEOUT, 3);
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
- res = curl_easy_perform(curl);
- curl_easy_cleanup(curl);
- return res;
- }
- int HttpClient::Get(const std::string & strUrl, std::string & strResponse)
- {
- CURLcode res;
- CURL* curl = curl_easy_init();
- if(NULL == curl)
- {
- return CURLE_FAILED_INIT;
- }
- curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
- curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
- /**
- * 当多个线程都使用超时处理的时候,同时主线程中有sleep或是wait等操作。
- * 如果不设置这个选项,libcurl将会发信号打断这个wait从而导致程序退出。
- */
- curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
- res = curl_easy_perform(curl);
- curl_easy_cleanup(curl);
- return res;
- }
- int HttpClient::Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath)
- {
- CURLcode res;
- CURL* curl = curl_easy_init();
- if(NULL == curl)
- {
- return CURLE_FAILED_INIT;
- }
- curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
- curl_easy_setopt(curl, CURLOPT_POST, 1);
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
- curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
- curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
- if(NULL == pCaPath)
- {
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
- }
- else
- {
- //缺省情况就是PEM,所以无需设置,另外支持DER
- //curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
- curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
- }
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
- res = curl_easy_perform(curl);
- curl_easy_cleanup(curl);
- return res;
- }
- int HttpClient::Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath)
- {
- CURLcode res;
- CURL* curl = curl_easy_init();
- if(NULL == curl)
- {
- return CURLE_FAILED_INIT;
- }
- curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
- curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
- curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
- if(NULL == pCaPath)
- {
- 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_CAINFO, pCaPath);
- }
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
- res = curl_easy_perform(curl);
- curl_easy_cleanup(curl);
- return res;
- }