头文件

#pragma once

#ifndef __HTTP_CURL_H__
#define __HTTP_CURL_H__ #include <string>
#include "curl.h"
#include "MqBase.h"
#include "Config.h"
class CHttpClient:public MqBase
{
public:
CHttpClient(void);
~CHttpClient(void); public:
/**
* @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); HPR_INT32 Init();
HPR_VOID Fini();
HPR_INT32 Connect();
HPR_VOID DoPublishAlarm();
HPR_VOID CheckMqStatus();
HPR_INT32 CheckServerStatus( HPR_LONG lTimeOut);
HPR_VOID PushMq(char* chMqInfo);
char* PopMq();
HPR_VOID ClearMqList();
HPR_INT32 AddToMqSendQueue(string strMsg);
public:
void SetDebug(bool bDebug); private:
bool m_bDebug;
HPR_HANDLE m_hLoopPublishMq;
HPR_BOOL m_bExit;
list<char*> m_listRabbitMq;
}; #endif

源文件

#include "HttpClientCurl.h"
#include "curl.h"
static HPR_VOIDPTR CALLBACK DoPublishAlarmThread(HPR_VOIDPTR param)
{
CHttpClient *p=(CHttpClient*)param;
if (p==NULL)
{
FIRE_ERROR("input para is NULL");
return NULL;
}
p->DoPublishAlarm();
return NULL;
}
CHttpClient::CHttpClient(void) :
m_bDebug(false)
{ } CHttpClient::~CHttpClient(void)
{ } static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *)
{
if(itype == CURLINFO_TEXT)
{
//printf("[TEXT]%s\n", pData);
}
else if(itype == CURLINFO_HEADER_IN)
{
printf("[HEADER_IN]%s\n", pData);
}
else if(itype == CURLINFO_HEADER_OUT)
{
printf("[HEADER_OUT]%s\n", pData);
}
else if(itype == CURLINFO_DATA_IN)
{
printf("[DATA_IN]%s\n", pData);
}
else if(itype == CURLINFO_DATA_OUT)
{
printf("[DATA_OUT]%s\n", pData);
}
return ;
} 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 -;
} char* pData = (char*)buffer;
str->append(pData, size * nmemb);
return nmemb;
} int CHttpClient::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;
}
if(m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, );
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
curl_easy_setopt(curl, CURLOPT_POST, );
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, );
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, );
curl_easy_setopt(curl, CURLOPT_TIMEOUT, );
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res;
} int CHttpClient::Get(const std::string & strUrl, std::string & strResponse)
{
CURLcode res;
CURL* curl = curl_easy_init();
if(NULL == curl)
{
return CURLE_FAILED_INIT;
}
if(m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, );
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
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, );
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, );
curl_easy_setopt(curl, CURLOPT_TIMEOUT, );
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res;
} int CHttpClient::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;
}
if(m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, );
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
curl_easy_setopt(curl, CURLOPT_POST, );
if (strPost!="")
{
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strPost.size());
}
//curl_easy_setopt(curl, CURLOPT_, headerlist); 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, );
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, );
curl_easy_setopt(curl, CURLOPT_TIMEOUT, );
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res;
} int CHttpClient::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;
}
if(m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, );
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
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, );
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, );
curl_easy_setopt(curl, CURLOPT_TIMEOUT, );
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res;
} /////////////////////////////////////////////////////////////////////////////////////////////// void CHttpClient::SetDebug(bool bDebug)
{
m_bDebug = bDebug;
} HPR_INT32 CHttpClient::AddToMqSendQueue(string strMsg)
{
if (strMsg == "")
{
return HPR_ERROR;
}
char* chMq = g_MemPool.MemAlloc(strMsg.length());
if (chMq == NULL)
{
FIRE_ERROR("char* chActiveMq = MallocBuff(LARGE_LEN) fail");
return HPR_ERROR;
}
HPR_Strncpy(chMq, strMsg.c_str(), strMsg.length());
PushMq(chMq);
return HPR_OK;
}
HPR_INT32 CHttpClient::Init()
{
HPR_INT32 iRetVal = HPR_ERROR;
m_strMqIp="";
m_bExit=HPR_FALSE;
do
{
if (HPR_SemCreate(&m_iSendMqSem, ) != HPR_OK)
{
LOG_ERROR("LoopSendMq list semaphore create failed!");
#if defined(OS_WINDOWS)
m_iSendMqSem = HPR_INVALID_HANDLE;
#endif
break;
}
m_hLoopPublishMq = HPR_Thread_Create(DoPublishAlarmThread, this, );
if (m_hLoopPublishMq == HPR_INVALID_THREAD)
{
FIRE_ERROR("Create publish alarm thread failed!");
break;
}
iRetVal = HPR_OK;
} while ();
return iRetVal;
}
HPR_VOID CHttpClient::Fini()
{
m_bExit = HPR_TRUE;
if (m_hLoopPublishMq != HPR_INVALID_THREAD)
{
HPR_Thread_Wait(m_hLoopPublishMq);
m_hLoopPublishMq = HPR_INVALID_THREAD;
}
#if defined(OS_WINDOWS)
if (m_iSendMqSem != HPR_INVALID_HANDLE)
{
HPR_SemDestroy(&m_iSendMqSem);
m_iSendMqSem = HPR_INVALID_HANDLE;
}
#else
HPR_SemDestroy(&m_iSendMqSem);
#endif
ClearMqList();
} HPR_INT32 CHttpClient::Connect()
{
return HPR_OK;
} HPR_VOID CHttpClient::DoPublishAlarm()
{
int iStatus = ;
string strURL=CConfig::instance()->GetHttpURL(); std::string strData= "hello Rabbit";
string strResponse="";
string strContent="";
while (!m_bExit)
{
char* msg=PopMq();
if (msg!=NULL)
{
strContent=string(msg);
iStatus=Post(strURL,strContent,strResponse);
if(iStatus!=CURLE_OK)
{
FIRE_ERROR("send data %s faild\n",msg);
PushMq(msg);
continue;
}
g_MemPool.MemRstore(msg);
FIRE_INFO("send data sucess %s\n",msg);
}
}
} HPR_VOID CHttpClient::CheckMqStatus()
{ } HPR_VOID CHttpClient::PushMq(char* chMqInfo)
{
HPR_Guard lock(&m_iMutex);
FIRE_INFO("PushActiveMq %s",chMqInfo);
m_listRabbitMq.push_back(chMqInfo);
HPR_SemPost(&m_iSendMqSem);
} char* CHttpClient::PopMq()
{
char* chMqInfo = NULL; do
{
if (HPR_SemTimedWait(&m_iSendMqSem, ) != HPR_OK)
{
//FIRE_INFO("HPR_SemTimedWait(&m_iSendMqSem, 1000)");
break;
} HPR_Guard lock(&m_iMutex);
if (m_listRabbitMq.size() == )
{
break;
}
else
{
chMqInfo = m_listRabbitMq.front();
m_listRabbitMq.pop_front();
}
lock.Release();
} while (); return chMqInfo;
} HPR_VOID CHttpClient::ClearMqList()
{
{
HPR_Guard lock(&m_iMutex);
while(!m_listRabbitMq.empty())
{
char* chMqInfo = m_listRabbitMq.front();
m_listRabbitMq.pop_front();
g_MemPool.MemRstore(chMqInfo);
}
}
} HPR_INT32 CHttpClient::CheckServerStatus( HPR_LONG lTimeOut)
{
return HPR_OK;
}

带有协议头的restfull接口

int CImageTransfer::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;
}
/*if(m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}*/
struct curl_slist *headers = NULL; //增加HTTP header
headers = curl_slist_append(headers, "Accept:application/json");
headers = curl_slist_append(headers, "Content-Type:application/json");
headers = curl_slist_append(headers, "charset:utf-8");
CBase64 pBase;
string strCode="kqzhcg_hikvision :P3w6%kfm";
strCode= pBase.Encode(strCode.c_str(),strCode.size());
strCode="Authorization:"+strCode;
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
curl_easy_setopt(curl, CURLOPT_POST, );
if (strPost!="")
{
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strPost.size());
}
//curl_easy_setopt(curl, CURLOPT_, headerlist); 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, );
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, );
curl_easy_setopt(curl, CURLOPT_TIMEOUT, );
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res;
}

自己编了一个股票监控软件,有如下功能,有兴趣的朋友可以下载;

(1)   个股监测。监测个股实时变化,可以监测个股大单交易、急速拉升和下降、主力入场和出场、股票最高点和最低点提醒。检测到最高点、最低点、主力进场点、主力退场点、急速拉升点、急速下跌点,给出语音或者声音提醒,不用再时刻看着大盘了,给你更多自由的时间;

(2)   大盘监测。监测大盘的走势,采用上证、深证、创业三大指数的综合指数作为大盘走势。并实时监测大盘的最高点和最低点、中间的转折点。

(3)   股票推荐。还能根据历史数据长期或短期走势进行分析,对股市3千多个股票进行分析对比,选出涨势良好的股票,按照增长速度从大到小排序,推荐给你涨势良好的股票;

下载地址:

1.0.3版本(修复大盘指数崩溃缺陷)下载地址:

链接:https://pan.baidu.com/s/1BJcTp-kdniM7VE9K5Kd3vg 提取码:003h

更新链接:

https://www.cnblogs.com/bclshuai/p/10621613.html

基于libcurl的restfull接口 post posts get gets的更多相关文章

  1. atitit.基于http json api 接口设计 最佳实践 总结o7

    atitit.基于http  json  api 接口设计 最佳实践 总结o7 1. 需求:::服务器and android 端接口通讯 2 2. 接口开发的要点 2 2.1. 普通参数 meth,p ...

  2. 【转】基于laravel制作APP接口(API)

    这篇文章主要介绍了基于laravel制作APP接口(API)的相关资料,需要的朋友可以参考下 前期准备 前言,为什么做以及要做个啥本人姓小名白,不折不扣编程届小白一名,但是自从大一那会儿接触到编程这件 ...

  3. RobotFrameWork环境搭建(基于HTTP协议的接口自动化)

    1. 前言 接着上一篇<RobotFramework框架系统课程介绍>,本篇主要介绍一下在基于RobotFramework框架开展接口自动化前,前期的环境如何搭建,正所谓”工欲善其事,必先 ...

  4. 基于PCIe的高速接口设计

    基于PCIe的高速接口设计 由 judyzhong 于 星期四, 03/03/2016 - 13:49 发表 作者:李晓宁,姚远程,秦明伟 2016年微型机与应用第1期 摘要:PCIe总线是第三代I/ ...

  5. 5.7 Liquibase:与具体数据库独立的追踪、管理和应用数据库Scheme变化的工具。-mybatis-generator将数据库表反向生成对应的实体类及基于mybatis的mapper接口和xml映射文件(类似代码生成器)

    一. liquibase 使用说明 功能概述:通过xml文件规范化维护数据库表结构及初始化数据. 1.配置不同环境下的数据库信息 (1)创建不同环境的数据库. (2)在resource/liquiba ...

  6. ASP.NET Core WebApi基于Redis实现Token接口安全认证

    一.课程介绍 明人不说暗话,跟着阿笨一起玩WebApi!开发提供数据的WebApi服务,最重要的是数据的安全性.那么对于我们来说,如何确保数据的安全将会是需要思考的问题.在ASP.NET WebSer ...

  7. 基于Python的WEB接口开发与自动化测试 pdf(内含书签)

    基于Python的WEB接口开发与自动化测试 目录 目 录O V目 录章 Python 学习必知 ................................................... ...

  8. 基于zynq XC7Z100 FMC接口通用计算平台 XC7Z100

      一.板卡概述 本板卡基于Xilinx公司的FPGA XC7Z100 FFG 9000 芯片, 该平台为设计和验证应用程序提供了一个完整的开发平台.该平台使设计师能够更加简单进行高性能的原型设计,并 ...

  9. 基于小程序请求接口 wx.request 封装的类 axios 请求

    基于小程序请求接口 wx.request 封装的类 axios 请求 Introduction wx.request 的配置.axios 的调用方式 源码戳我 feature 支持 wx.reques ...

随机推荐

  1. O058、Snapshot Volume 操作

    参考https://www.cnblogs.com/CloudMan6/p/5657744.html   Snapshot 可以为 volume 创建快照,快照中保存了 volume当前的状态,以后可 ...

  2. [leetcode] 题解记录 11-20

    博客园markdown太烂, 题解详情https://github.com/TangliziGit/leetcode/blob/master/solution/11-20.md Leetcode So ...

  3. Sql Server 导出数据库表结构的SQL查询语句

    --导出数据库所有表 SELECT 表名 Then D.name Else '' End, 表说明 Then isnull(F.value,'') Else '' End, 字段序号 = A.colo ...

  4. Centos6.8 rabbitmq搭建且修改默认端口

    一.安装依赖环境 yum install build-essential openssl openssl-devel unixODBC unixODBC-devel make gcc gcc-c++ ...

  5. element table 表格 修改背景为透明并去除边框

    .el-table{ /* 表格字体颜色 */ color:white; /* 表格边框颜色 */ /* border: 0.5px solid #758a99; */ height: 500px; ...

  6. Hacklab WebIDE在线调试ESP32笔记

    目录 1.什么是Hacklab WebIDE 1.1 优势 1.2 趋势 2. 使用方法 2.1 功能介绍 2.2 编译第一个程序 2.3 搭建esp32的开发环境 2.4 建立开发板与云平台的连接 ...

  7. Easy UI combobox实现类似 Select2的效果,下拉带搜索框

    一直在开发一个新系统,其中用Easy UI作为前端框架,少不了用 combobox做为一个 下拉控件,它支持 可编辑 模糊本地数据过滤,也可支持 不可编辑 下拉 选择的功能: $('#ID' ).co ...

  8. 如何对Win10电脑文件夹选项进行设置?

    文件夹选项是Windows系统中非常重要的一个功能,在这里能对电脑内的文件及文件夹进行各种各样的设置以及操作.在Windows系统升级到Win10版本后,许多界面都发生了变化,文件夹选项也是如此,打开 ...

  9. GridView 二维排布

    与ListView一维排布相对 public class MainActivity extends Activity implements AdapterView.OnItemClickListene ...

  10. 离线(不联网)安装gcc4.8.5

    1 楔子 原来机器gcc版本是4.4.6,不支持cpp11,很麻烦需要升级,但是机器不能连外网,只能离线安装,十分麻烦: 2 gcc4.8.5离线安装,通过rpm包: 资源链接: https://pa ...