Face++ C++ curl跨平台HttpClient

// 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;
}


### 回答1: 要在Mac M1上配置C++环境,可以按照以下步骤进行操作: 1. 安装Xcode:在Mac App Store中下载并安装Xcode,它包含了C++编译器和其他开发工具。 2. 安装Homebrew:打开终端,运行以下命令安装Homebrew: ``` /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ``` 3. 安装GCC:在终端中运行以下命令安装GCC: ``` brew install gcc ``` 4. 配置VSCode:打开VSCode,安装C/C++插件。在VSCode中打开C++文件,按下F1键,输入“C++:Edit Configurations”,选择“C++(GDB/LLDB)”作为调试器。 5. 配置任务:在VSCode中按下F1键,输入“Tasks:Configure Task”,选择“C++:g++ build active file”,然后按照提示进行配置。 6. 编译运行:在VSCode中按下F5键,编译并运行C++程序。 以上是在Mac M1上配置C++环境的步骤,希望对您有所帮助。 ### 回答2: 首先,在Mac M1上配置C语言开发环境需要以下几个步骤: 1.安装Xcode工具并勾选Command Line Tools 2.安装homebrew管理器 3.安装LLVM编译器 4.安装Visual Studio Code(VSCode) 5.安装C/C++插件和Code Runner插件 接下来,我们将详细介绍这些步骤: 1.安装Xcode工具并勾选Command Line Tools Xcode是苹果公司开发的一款集成开发环境,可以编写IOS、MacOS和其他苹果相关软件。在Mac下开发C语言,我们需要Xcode工具中的Command Line Tools,因此在安装Xcode的过程中,需要勾选Command Line Tools。 2.安装homebrew管理器 homebrew是Mac系统上一款非常好用的包管理器,可以用来安装和卸载各种软件包,包括LLVM编译器和VSCode等软件。在终端输入以下指令安装homebrew: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" 3.安装LLVM编译器 Mac系统自带的Clang编译器已经可以编译C语言程序,但为了更好的编译效果和调试支持,我们可以使用LLVM,输入以下指令安装: brew install llvm 安装完成后,我们可以使用命令“which clang”来查看LLVM是否安装成功。 4.安装Visual Studio Code(VSCode) VSCode是一款由微软开发的轻量级代码编辑器,提供了丰富的插件和扩展,可以满足我们进行C语言开发所需的各种功能。在VSCode的官网上下载安装包并安装即可。 5.安装C/C++插件和Code Runner插件 在VSCode中,我们需要安装C/C++插件和Code Runner插件来加强对C语言的支持和调试功能。在VSCode的Extensions界面搜索并安装即可。 以上就是在Mac M1上配置C语言开发环境的步骤。需要注意的是,在使用LLVM编译器时,需要将其添加到环境变量中,可以通过修改.bash_profile文件并添加“export PATH="/usr/local/opt/llvm/bin:$PATH"”来实现。 ### 回答3: 首先,需要下载并安装适用于 M1 芯片的 Xcode。可以在 App Store 中搜索并安装。这是因为在 M1 芯片上编译 C 语言需要使用 Clang 编译器,而 Clang 是由 Xcode 提供的。 安装完 Xcode 后,需要安装 Homebrew。Homebrew 是 Mac 上一个常用的包管理器。打开终端,输入以下命令即可安装 Homebrew: ``` /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ``` 安装完 Homebrew 后,再输入以下命令安装 Clang 编译器: ``` brew install clang ``` 安装完 Clang 后,需要打开 VS Code。可以在 VS Code 搜索框中输入 C/C++,找到官方提供的 C/C++ 扩展,点击安装。安装完成后,需要打开 VS Code 的设置,找到 C/C++ 扩展的配置项。 在 C/C++ 扩展配置项中,需要设置 C/C++ 编译器路径。配置方法如下: 找到 “C_Cpp:Default:clang++ Path” 配置项,将其设置为 Clang 编译器的路径。 默认情况下,Clang 编译器的路径如下: ``` /usr/local/Cellar/llvm/13.0.0/bin/clang++ ``` 需要根据自己的 Clang 编译器安装路径进行设置。 在配置好 C/C++ 编译器路径后,就可以在 VS Code 中编写并运行 C 语言程序了。新建一个 C 语言文件,输入代码后,可以按 F5 键编译运行程序,或者在终端中输入以下命令进行编译运行: ``` clang file.c -o file && ./file ``` 其中,file.c 是 C 语言源文件名,file 是编译后的程序名。 最后,需要注意的是,在 M1 芯片上编译 C 语言时,需要使用 -arch arm64 标志指定编译架构。例如,使用 Clang 编译 hello.c 程序时,需要输入以下命令: ``` clang -arch arm64 -o hello hello.c ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值