c++ 使用curl发送https POST请求并获取返回数据(包含appKey与appSecret)

借鉴连接:https://yq.aliyun.com/articles/629830?spm=a2c4e.11153940.blogcont630369.18.3dca5d3c1QHa9l

使用到libcurl库及openssl,

用VS2015写了个demo,记录一下,demo下载链接在文章末尾。

直接上代码:

#include 
#include 
#include 
#include 
#include 

#include "curl/curl.h"
#include 
#include 

using namespace std;

size_t base64Encode(const void *data, int data_len, char *buffer)
{
	BIO *b64 = BIO_new(BIO_f_base64());
	BIO *bio = BIO_new(BIO_s_mem());

	bio = BIO_push(b64, bio);
	BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
	BIO_write(bio, data, data_len);
	BIO_ctrl(bio, BIO_CTRL_FLUSH, 0, NULL);

	BUF_MEM *bptr = NULL;
	BIO_get_mem_ptr(bio, &bptr);

	size_t slen = bptr->length;
	memcpy(buffer, bptr->data, slen);
	buffer[slen] = '\0';

	BIO_free_all(bio);
	return slen;
}

int HmacEncode(const char * algo,
	const char * key, unsigned int key_length,
	const char * input, unsigned int input_length,
	string &strOutput, unsigned int &output_length) {
	const EVP_MD * engine = NULL;
	if (strcmp("sha512", algo) == 0) {
		engine = EVP_sha512();
	}
	else if (strcmp("sha256", algo) == 0) {
		engine = EVP_sha256();
	}
	else if (strcmp("sha1", algo) == 0) {
		engine = EVP_sha1();
	}
	else if (strcmp("md5", algo) == 0) {
		engine = EVP_md5();
	}
	else if (strcmp("sha224", algo) == 0) {
		engine = EVP_sha224();
	}
	else if (strcmp("sha384", algo) == 0) {
		engine = EVP_sha384();
	}
	else {
		cout << "Algorithm " << algo << " is not supported by this program!" << endl;
		return -1;
	}

	unsigned char* output = (unsigned char*)malloc(EVP_MAX_MD_SIZE);

	HMAC_CTX *ctx = HMAC_CTX_new();
	HMAC_Init_ex(ctx, key, strlen(key), engine, NULL);
	HMAC_Update(ctx, (unsigned char*)input, strlen(input));        // input is OK; &input is WRONG !!!

	HMAC_Final(ctx, output, &output_length);
	HMAC_CTX_free(ctx);

	strOutput = (char*)output;

	return 0;
}

// 将请求返回的数据写在本地文件
size_t HttpsReturnMsg(void* buffer, size_t size, size_t nmemb, void *stream)
{
	FILE *fptr = (FILE*)stream;
	fwrite(buffer, size, nmemb, fptr);
	return size*nmemb;
}

int main()
{
	CURL *curl;
	CURLcode res;

	curl_global_init(CURL_GLOBAL_ALL);
	curl = curl_easy_init();

	FILE* fp = fopen("e:/1.txt", "wb+");

	if (curl)
	{
		// 参考连接:https://yq.aliyun.com/articles/629830?spm=a2c4e.11153940.blogcont630369.18.3dca5d3c1QHa9l
		char Url[1024] = { 0 };
		sprintf_s(Url, "https://yq.aliyun.com/articles/629830?spm=a2c4e.11153940.blogcont630369.18.3dca5d3c1QHa9l");

		curl_slist *pList = NULL;
		pList = curl_slist_append(pList, "Accept:application/json, text/plain, */*");
		pList = curl_slist_append(pList, "Content-Type:application/json;charset=UTF-8");
		pList = curl_slist_append(pList, "X-Ca-Key:12345678");

		string appKey = "12345678";
		string appSecret = "TEST1234ABCD5678AAAA";
		string customHeaders = "x-ca-key:" + appKey + "\n";
		string httpHeaders = "POST\napplication/json, text/plain, */*\napplication/json;charset=UTF-8\n";
		string strNeedEncode = httpHeaders + customHeaders + "articles/629830?spm=a2c4e.11153940.blogcont630369.18.3dca5d3c1QHa9l";

		string strMac;
		unsigned int mac_length = 0;

		int ret = HmacEncode("sha256", appSecret.c_str(), appSecret.length(), strNeedEncode.c_str(), strNeedEncode.length(), strMac, mac_length);
		char *p = new char[128];
		memset(p, 0, 128);
		base64Encode(strMac.c_str(), mac_length, p);
		
		char strX_Ca_Signature[256] = { 0 };
		sprintf_s(strX_Ca_Signature, "X-Ca-Signature:%s", p);

		pList = curl_slist_append(pList, strX_Ca_Signature);
		pList = curl_slist_append(pList, "X-Ca-Signature-Headers:x-ca-key");
		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, pList);

		curl_easy_setopt(curl, CURLOPT_URL, Url);
		curl_easy_setopt(curl, CURLOPT_POST, 1); // post req  
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HttpsReturnMsg);	//对返回的数据进行操作的函数地址
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);					//这是HttpsReturnMsg的第四个参数值

		/* example.com is redirected, so we tell libcurl to follow redirection */
		curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

		char strMsgBody[1024] = { 0 };
		sprintf_s(strMsgBody, 1024, "{\"name\":\"hhhh\",\"age\":\"12\"}");
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strMsgBody);

		/* Perform the request, res will get the return code */
		res = curl_easy_perform(curl);

		/* Check for errors */
		if (res != CURLE_OK)
			fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));

		/* always cleanup */
		curl_easy_cleanup(curl);
	}
	curl_global_cleanup();

	return 0;
}

 

demo下载链接:https://download.csdn.net/download/m0_37684310/11199820

你可能感兴趣的:(c/c++)