#include "StdAfx.h"
#include "UrlHttp.h"
namespace _CURL_
{
/**
* buf: 从服务器返回的buffer
* unit: buufer的单位
* bufSize: buffer的大小
* data: 保存从服务器返回的内容
* 注意这个函数会被调用多次
*/
static size_t write_data(void *buf, size_t unit, size_t bufSize, std::string * data)
{
int size = unit * bufSize;
char * tmp = (char*)malloc(size + 1);
memcpy(tmp, buf, size);
tmp[size] = '\0';
data->append(tmp);
free(tmp);
return size;
}
//智能初始化curl库和释放curl库
class CurlIntelligence
{
public:
CurlIntelligence()
{
curl_global_init(CURL_GLOBAL_ALL);
}
~CurlIntelligence()
{
curl_global_cleanup();
}
};
}
_CURL_::CurlIntelligence g_curl;
CUrlHttp::CUrlHttp(void)
{
}
CUrlHttp::~CUrlHttp(void)
{
}
/*
* 函数:
* Request(请求函数)
* 参数:
* strRequestType:请求类型(get,post)
* strUrl:请求url地址
* strReport:回执信息
* strHeader:请求头
* strCookie:cookie信息
* strParam:请求参数(get的时候此参数填空)
* nTimeOut:超时设置默认是0秒 是无限等待
* 返回值:
* 0表示成功 非0表示错误代码
* ssdwujianhua 2017/06/06
*/
int CUrlHttp::Request(std::string strRequestType,
std::string strUrl,
std::string &strReport,
std::string strParam/* ="" */,
std::string strHeader/* ="" */,
std::string strCookie/* ="" */,
int nTimeOut/* =0 */)
{
CURL * curl;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
if ( strRequestType.compare("post")==0 || strRequestType.compare("POST") == 0 )
{
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strParam.c_str());
}
else
{
curl_easy_setopt(curl, CURLOPT_POST, 0);//get请求
}
std::string strReportHeader;//回执回来的头数据
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); //可以看到调试信息
//接受服务器的ssl证书而不管合不合法 (相当于命令行中的--insecure)
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
//curl_easy_setopt(curl,CURLOPT_HEADERFUNCTION,_CURL_::write_data);
//curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &strReportHeader);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _CURL_::write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strReport);
if ( nTimeOut > 0 )
{
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, nTimeOut);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, nTimeOut);
}
if (!strHeader.empty())
{
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, strHeader.c_str());
}
if (!strCookie.empty())
{
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, strCookie.c_str());
}
CURLcode code = curl_easy_perform(curl);
curl_easy_cleanup(curl);
//打印出来
// std::string strReportData;
// strReportData.append(strReportHeader);
// strReportData.append(strReport);
// TRACE("request:%s url:%s report:%s", strRequestType.c_str(), strUrl.c_str(), strReportData.c_str());
return code;
}
/*
* 函数:
* RequestSSL(表单提交)
* 参数:
* strUrl:请求url地址
* strReport:回执信息
* strHeader:请求头
* strCookie:cookie信息
* listParam:表单列表
* strCaPath:ca转成pem证书路径
* nTimeOut:超时设置默认是0秒 是无限等待:
* 返回值:
*
* ssdwujianhua 2017/07/21
*/
int CUrlHttp::RequestSSL(std::string strUrl,
std::string &strReport,
_POST_LIST_DATA_ listParam,
std::string strHeader/* ="" */,
std::string strCookie/* ="" */,
std::string strCaPath/* ="" */,
int nTimeOut/* =0 */)
{
CURLcode code;
CURL * curl;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
//err = curl_easy_setopt(hCurl, CURLOPT_HTTPHEADER, "dfsdf");
struct curl_httppost *post=NULL;
struct curl_httppost *last=NULL;
CURLFORMcode errf;
for ( int i=0; i<listParam.size(); i++ )
{
POST_LIST post_list = listParam.at(i);
if (post_list.eKeyType == e_key_iamge ) //图片类型 直接提交图片路径
{
errf = curl_formadd(&post, &last, CURLFORM_COPYNAME, post_list.strKey.c_str(),
CURLFORM_FILE,post_list.strVal.c_str(),CURLFORM_CONTENTTYPE, "image/jpeg",CURLFORM_END);
}
else
{
errf = curl_formadd(&post, &last, CURLFORM_COPYNAME, post_list.strKey.c_str(),
CURLFORM_COPYCONTENTS, post_list.strVal.c_str(),CURLFORM_END);
}
}
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _CURL_::write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strReport);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
//判断是否有证书
if(strCaPath.empty())
{
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, strCaPath.c_str());
}
if ( nTimeOut > 0 )
{
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, nTimeOut);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, nTimeOut);
}
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,1); //是否抓取跳转后的页面
/* Set the form info */
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post); //
curl_easy_setopt(curl, CURLOPT_HEADER, 0); //不读取返回头的数据
if (!strHeader.empty())
{
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, strHeader.c_str());
}
if (!strCookie.empty())
{
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, strCookie.c_str());
}
code = curl_easy_perform(curl); /* post away! */
//获取请求返回的值 如:200
//code = curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE, &RESPONSE_CODE);
/* free the post data again */
curl_formfree(post);
curl_easy_cleanup(curl);
return code;
}
std::string CUrlHttp::GBKToUTF8(const char* strGBK)
{
int len = MultiByteToWideChar(CP_ACP, 0, strGBK, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len+1];
memset(wstr, 0, len+1);
MultiByteToWideChar(CP_ACP, 0, strGBK, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len+1];
memset(str, 0, len+1);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
std::string strTemp = str;
if(wstr) delete[] wstr;
if(str) delete[] str;
return strTemp;
}
std::string CUrlHttp::UTF8ToGBK(const char* strUTF8)
{
int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8, -1, NULL, 0);
wchar_t* wszGBK = new wchar_t[len+1];
memset(wszGBK, 0, len*2+2);
MultiByteToWideChar(CP_UTF8, 0, strUTF8, -1, wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char* szGBK = new char[len+1];
memset(szGBK, 0, len+1);
WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL);
std::string strTemp(szGBK);
if(wszGBK) delete[] wszGBK;
if(szGBK) delete[] szGBK;
return strTemp;
}
CUrlHttp封装curl类
需积分: 50 98 浏览量
2017-07-21
09:44:10
上传
评论 1
收藏 174KB RAR 举报
wu110112
- 粉丝: 110
- 资源: 36
最新资源
- STM32F103C8T6入门教学ppt.pptx
- oogle OAuth Client Library for Java
- 用Python实现基于人脸识别的门禁管理系统(附源码+各功能截图+分类打包)
- 新生汇总.zip
- 基于Javaweb开发的学生就业管理系统,学生就业管理系统有三个角色:管理员、企业、学生(源码,论文,数据库,前后台)
- 基于JavaWeb的足球网平台设计(毕业设计源码)
- 基于matlab实现不同老化天数的玉米籽粒的THz光谱,用移动窗口相关系数法找出特征波段.rar
- 基于matlab实现单纯型算法和最速下降法的综合算法,能很快的搜索到最小值 .rar
- 基于matlab实现该算法可以用于新安江水文模型的参数优选,从而进行降雨径流过程模拟 .rar
- 基于matlab实现混沌单纯形法算子的布谷鸟搜索算法,算法引入混沌算子及单纯形法,提高了布谷鸟搜索算法的.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈