头文件

#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. 和 Python 2.x 说再见!项目移到python3

    如果你仍在使用 2.x,那么是时候将你的代码移植到 Python 3 了. 在技术的长河中,软件.工具.系统等版本的迭代本是常事,但由于使用习惯.版本的兼容性.易用性等因素,很多用户及开发者在使用或做 ...

  2. NSIS逻辑函数头文件介绍

    !include "LogicLib.nsh"使用 NSIS 的宏来提供各种逻辑基本语句,不需要预先添加函数. 基本语句 If|Unless..{ElseIf|ElseUnless ...

  3. pthread 笔记

    1.创建线程 res = pthread_create(&a_thread, NULL, thread_function1, NULL); if (res != 0) { perror(&qu ...

  4. 转载: Linux查看系统开机时间

    转自: https://www.cnblogs.com/kerrycode/p/3759395.html 查看Linux系统运行了多久时间,此时需要知道上次开机启动时间: 有时候由于断电或供电故障突然 ...

  5. 使用wget下载百度云资源

    目录 使用wget下载百度云资源 一.材料准备: 二.步骤 三.总结 使用wget下载百度云资源 一.材料准备: [BaiduPan explorer]谷歌插件,可以加载文件的真实下载地址 [Chro ...

  6. 【异常】hue:unknown database hue

    1 hue error日志报错,找不到hue数据库 2 解决办法 删除hue服务,重新添加,再次在添加database阶段验证密码,test通过,再继续. 还有造成这个事情的原因,是自己移动了mysq ...

  7. ubuntu版本信息查看

    1.cat /etc/issue 2.cat /etc/lsb-release 3.uname -a 4.cat /proc/version 5.lsb_release -a 显卡信息1.lspci ...

  8. deep_learning_Dropout

    吴恩达深度学习笔记(十一)—— dropout正则化 主要内容: 一.dropout正则化的思想 二.dropout算法流程 三.dropout的优缺点 一.dropout正则化的思想 在神经网络中, ...

  9. 超简单!教你如何修改源列表(sources.list)来提高软件访问速度

    因为Ubuntu官方的源地址不在国内,所以在国内的访问速度非常慢,比如:我们要下载或是更新软件那速度比蜗牛还慢.所以,我们需要改成国内的镜像服务器,这样,我们在下载或更新软件的时候就会很快了. 配置步 ...

  10. java-集合学习-底层实现

    集合分为两大类: Collection集合: 单个存储 Map集合: 按<键,值>对的形式存储,  <员工姓名,工资> Collection类关系图 Collection常见方 ...