对curl的封装_curl参数

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6


http.h

#ifndef _HTTP_H_
#define _HTTP_H_

#include <string>
#include <set>
#include <curl/curl.h>

namespace tools
{

class CCurlWrapper
{
public:
    CCurlWrapper ();
    ~CCurlWrapper ();

    int PostFile (const std::string& strUrl, const std::string& strRequest, std::string& strReply);
    int GetFile (const std::string& strUrl, std::string& strReply);
    void AddHttpHeader (const std::string& strHeader);

private:
    int Init ();
    static size_t WriteDataCallback (void *ptr, size_t size, size_t nCount, void *pData);

    CURL *m_pCurl;
    std::set<std::string> m_setHeaders;

};

}

#endif





http.cpp

#include "http.h"
#include <sstream>

using namespace std;
using namespace tools;

CCurlWrapper::CCurlWrapper () :
    m_pCurl(NULL)
{
    //curl_global_init(CURL_GLOBAL_ALL);
    Init();
}

CCurlWrapper::~CCurlWrapper ()
{
    curl_easy_cleanup(m_pCurl);
    m_pCurl = NULL;
}

int CCurlWrapper::Init ()
{
    if (m_pCurl)
    {
        curl_easy_cleanup(m_pCurl);
        m_pCurl = NULL;
    }

    m_pCurl = curl_easy_init();
    if (m_pCurl == NULL)
    {
        return -1;
    }

    // set connection timeout to 10's
    curl_easy_setopt(m_pCurl, CURLOPT_CONNECTTIMEOUT, 10);

    // set timeout to 30's
    curl_easy_setopt(m_pCurl, CURLOPT_TIMEOUT, 30);

    // accept identiy, deflate, and gzip encoding. (Accept-Encoding: )
    curl_easy_setopt(m_pCurl, CURLOPT_ENCODING, "gzip, deflate");

    // set user-agent to that of MSIE6
    curl_easy_setopt(m_pCurl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");

    // let curl to follow location (auto handle HTTP 301, 302)
    curl_easy_setopt(m_pCurl, CURLOPT_FOLLOWLOCATION, 1);

    m_setHeaders.clear();
    m_setHeaders.insert("Accept-Language: zh-cn");

    // output debug info (for debug only)
    //curl_easy_setopt(m_pCurl, CURLOPT_VERBOSE, true);
    curl_easy_setopt(m_pCurl, CURLOPT_VERBOSE, false);

    return 0;
}

void CCurlWrapper::AddHttpHeader (const string& strHeader)
{
    m_setHeaders.insert(strHeader);
}

int CCurlWrapper::PostFile (const string& strUrl, const string& strRequest, string& strReply)
{
    if (m_pCurl == NULL)
        return -1;

    int iRet;
    if (strUrl.empty())
    {
        return -2;
    }

    curl_easy_setopt(m_pCurl, CURLOPT_URL, strUrl.c_str());
    stringstream ssReply;
    curl_easy_setopt(m_pCurl, CURLOPT_WRITEDATA, &ssReply);
    curl_easy_setopt(m_pCurl, CURLOPT_WRITEFUNCTION, WriteDataCallback);

    // set post data
    if (strRequest.empty())
    {
        return -3;
    }
    curl_easy_setopt(m_pCurl, CURLOPT_POST, true);
    curl_easy_setopt(m_pCurl, CURLOPT_POSTFIELDS, strRequest.c_str());
    curl_easy_setopt(m_pCurl, CURLOPT_POSTFIELDSIZE, strRequest.length());

    // autosave cookie with the handle
    curl_easy_setopt(m_pCurl, CURLOPT_COOKIEFILE, "/dev/null");

    // set header
    struct curl_slist *headers = NULL;
    if (!m_setHeaders.empty())
    {
        for (set<string>::iterator it = m_setHeaders.begin(); it != m_setHeaders.end(); ++it)
            headers = curl_slist_append(headers, it->c_str());
    }
    curl_easy_setopt(m_pCurl, CURLOPT_HTTPHEADER, headers);

    curl_easy_setopt(m_pCurl, CURLOPT_SSL_VERIFYPEER, 0L);
    curl_easy_setopt(m_pCurl, CURLOPT_SSL_VERIFYHOST, 0L);

    //curl_easy_setopt(m_pCurl, CURLOPT_WRITEDATA, &ssReply);
    //curl_easy_setopt(m_pCurl, CURLOPT_WRITEFUNCTION, WriteDataCallback);

    // perform
    iRet = curl_easy_perform(m_pCurl);
    if (iRet == CURLE_OK)
    {
        strReply = ssReply.str();
    }

    if (headers)
        curl_slist_free_all(headers);

    return (iRet == CURLE_OK) ? 0 : iRet;
}


int CCurlWrapper::GetFile (const string& strUrl, string& strReply)
{
    if (m_pCurl == NULL)
        return -1;

    int iRet;
    if (strUrl.empty())
    {
        return -2;
    }

    curl_easy_setopt(m_pCurl, CURLOPT_URL, strUrl.c_str());
    stringstream ssReply;
    curl_easy_setopt(m_pCurl, CURLOPT_WRITEDATA, &ssReply);
    curl_easy_setopt(m_pCurl, CURLOPT_WRITEFUNCTION, WriteDataCallback);

    curl_easy_setopt(m_pCurl, CURLOPT_HTTPGET, true);

    // autosave cookie with the handle
    curl_easy_setopt(m_pCurl, CURLOPT_COOKIEFILE, "/dev/null");

    // set header
    struct curl_slist *headers = NULL;
    if (!m_setHeaders.empty())
    {
        for (set<string>::iterator it = m_setHeaders.begin(); it != m_setHeaders.end(); ++it)
            headers = curl_slist_append(headers, it->c_str());
    }
    curl_easy_setopt(m_pCurl, CURLOPT_HTTPHEADER, headers);

    curl_easy_setopt(m_pCurl, CURLOPT_SSL_VERIFYPEER, 0L);
    curl_easy_setopt(m_pCurl, CURLOPT_SSL_VERIFYHOST, 0L);

    //curl_easy_setopt(m_pCurl, CURLOPT_WRITEDATA, &ssReply);
    //curl_easy_setopt(m_pCurl, CURLOPT_WRITEFUNCTION, WriteDataCallback);

    // perform
    iRet = curl_easy_perform(m_pCurl);
    if (iRet == CURLE_OK)
    {
        strReply = ssReply.str();
    }

    if (headers)
        curl_slist_free_all(headers);

    return (iRet == CURLE_OK) ? 0 : iRet;
}

size_t CCurlWrapper::WriteDataCallback (void *ptr, size_t size, size_t nCount, void *pData)
{
    stringstream* pTmp = (stringstream*) pData;
    pTmp->write((char*) ptr, size * nCount);
    return size * nCount;
}




阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

“对curl的封装_curl参数” 的相关文章

4-9 二叉树的遍历 (25分)

本题要求给定二叉树的4种遍历。 函数接口定义:void InorderTraversal( BinTree BT ); void PreorderTraversal( BinTree BT ); void PostorderTraversal( BinTree BT ); void Levelor...

Python怎么读写JSON格式数据 - 开发技术

今天小编给大家分享一下Python怎么读写JSON格式数据的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。JSON格式数据简介JSON 全称“JavaSc...

双显示器

首先设置主显,根据情况试试,第一个不行就试第二个: sudo xrandr --output DVI1 --primary sudo xrandr --output VGA1 --primary 然后如果鼠标移动不正常: 系统->首选项->显示器...

怎么使用PHP修改本地地址 - 编程语言

这篇“怎么使用PHP修改本地地址”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“怎么使用PHP修改本地地址”文章吧。 首先,需...

php如何把特殊字符转义 - 编程语言

今天小编给大家分享一下php如何把特殊字符转义的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。 一、什么是特殊字符特殊字符是指...

VUE3+mqtt怎么封装解决多页面使用需重复连接等问题 - 开发技术

这篇文章主要介绍“VUE3+mqtt怎么封装解决多页面使用需重复连接等问题”,在日常操作中,相信很多人在VUE3+mqtt怎么封装解决多页面使用需重复连接等问题问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”VUE3+mqtt怎么封装解决...