face++ c++接口 以及 curl 库的使用,json库在另一个博客介绍
来源:互联网 发布:补水的水乳推荐知乎 编辑:程序博客网 时间:2024/06/07 13:56
- // 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;
- }
下边是使用CURL库记录
先去官网下载curl。我先下下载了libcurl-7.19.3_win32_ssl_msvc.zip,然后提示"应用程序无法正常启动(0xc000007)",反复测试没用(本人是win7 64位环境),它自带的也是这样。看到也有人说这个问题,好像是64位环境的问题,于是就重新搞一下。首先到官网下载64位的: http://curl.haxx.se/download.html ,在这里选择 Use the curl Download Wizard !,然后按照要求,下载了一个64位版本。下载后的文件名是 curl-7.34.0-devel-mingw64.7z。然后解压就可以了。
vs工程也要加一个,选择工程树具体的某个工程,点该工程的属性,终出下面的对话框后,选择配置管理器(图中所示)
设置先成后,选择相应的工程配置方案,就可以了。
下步就是如何加到工程,在下载的libcurl中,是用mingw编译了,没有的lib文件,但是他提供了def文件,我们可以用它来生成lib。在curl-7.34.0-devel-mingw64\lib64里面。
在windows的开始菜单找到visual studio 2013,选择visual studio tools,会打开一个目录,然后选择“VS2013 x64 本机工具命令提示”进入命令提示符下面。
进入到libcurl解压的目录,在lib64目录下面。然后输入下面两个命令,就可以得到两个lib了
lib /DEF:libcurl.def /MACHINE:X64
lib /DEF:libssh2.def /MACHINE:X64
把生成的libcurl.lib和libssh2.lib放到工程可以找到的lib目录,再把curl-7.34.0-devel-mingw64\bin下的所有dll复制到程序生成.exe的目录,这样,就可以在工程中使用curl了。
下面的博客提供了一个curl的使用,可以直接用,并编译通过。http://dewei.iteye.com/blog/1572016 ,唯一不同的指定lib
把生成的libcurl.lib和libssh2.lib放到工程可以找到的lib目录,再把curl-7.34.0-devel-mingw64\bin下的所有dll复制到程序生成.exe的目录,这样,就可以在工程中使用curl了。
下面的博客提供了一个curl的使用,可以直接用,并编译通过。http://dewei.iteye.com/blog/1572016 ,唯一不同的指定lib
#include "curl/curl.h"
#pragma comment(lib, "libcurl.lib")
20160329 今天发现脑电设备64的驱动没有了。没办法,立马把工程设置为32位的。下面的做法差不多,只是把64位的改成32位的
在windows的开始菜单找到visual studio 2013,选择visual studio tools,会打开一个目录,然后选择“VS2013 x64 本机工具命令提示”进入命令提示符下面。
进入到libcurl解压的目录,在lib64目录下面。然后输入下面两个命令,就可以得到两个lib了
lib /DEF:libcurl.def /MACHINE:X86
lib /DEF:libssh2.def /MACHINE:X86
把生成的libcurl.lib和libssh2.lib放到工程可以找到的lib目录,再把curl-7.34.0-devel-mingw32\bin下的所有dll复制到程序生成.exe的目录,这样,就可以在工程中使用curl了。
下面的博客提供了一个curl的使用,可以直接用,并编译通过。http://dewei.iteye.com/blog/1572016 ,唯一不同的指定lib
把生成的libcurl.lib和libssh2.lib放到工程可以找到的lib目录,再把curl-7.34.0-devel-mingw32\bin下的所有dll复制到程序生成.exe的目录,这样,就可以在工程中使用curl了。
下面的博客提供了一个curl的使用,可以直接用,并编译通过。http://dewei.iteye.com/blog/1572016 ,唯一不同的指定lib
#include "curl/curl.h"
#pragma comment(lib, "libcurl.lib")
特别注意的是: 选择visual studio tools,会打开一个目录,然后选择“VS2013 x64 本机工具命令提示”进入命令提示符下面。
因为我的系统是64位的,所以这里选择了64位。
0 0
- face++ c++接口 以及 curl 库的使用,json库在另一个博客介绍
- JSON的简单介绍以及C语言的JSON库使用
- JSON的简单介绍以及C语言的JSON库使用
- JSON的简单介绍以及C语言的JSON库使用
- Face++在线接口在Android下的使用,以及简单的Demo实现(判断两张脸是否为同一个人)
- C语言的JSON 解析库 - MJSON使用介绍
- 在C语言程序中使用cURL库(libcurl)
- php的cURL库介绍
- php的cURL库介绍
- php的cURL库介绍
- curl 库的使用
- 使用face++的API接口-手势识别
- Curl 及 Curl的使用介绍
- JSON C库的使用
- curl c/c++ api接口使用例程
- curl c/c++ api接口使用例程
- 使用face++接口实现在Android设备上的人脸识别
- C++处理JSON数据和在face++ 调用中的使用
- MySQL和SQL Server比较
- pragma code_seg
- Swift入门———逻辑分支
- 2015’12杭电校赛1002 Polygon (计算几何)
- 致简之美——EvaIO架构分享
- face++ c++接口 以及 curl 库的使用,json库在另一个博客介绍
- 如何解决手机提示不支持Google Play服务问题?
- 再见2015,你好2016
- linux-scp命令
- 2016第1篇--Python查看微信被删好友
- 致简之美——EvaIO基本使用简介
- ireport各个版本的下载地址
- 搜索命令之locate,whereis,which
- (2-1)HDFS( 分布式文件系统和hdfs shell)