当前位置:   article > 正文

libcurl库的使用--使用https发送post数据 c++实现_c++ 使用curl post请求 需要绕开ssl 证书吗

c++ 使用curl post请求 需要绕开ssl 证书吗

使用curl库post数据,不加ssl证书验证的话,只需要加两行参数即可

Http.h

#ifndef _HTTP_H_
#define _HTTP_H_

#include <curl/curl.h>
#include <string>
#include <iostream>
//#include "CJsonObject.hpp"


using namespace std;
//using namespace neb;


struct MemoryStruct {
    char *memory;
    size_t size;
};

class Http{
public:
    Http();
    ~Http();
    static string post(string url,const string data);
//    static CJsonObject post(string url,const CJsonObject& cjson_obj);

    void set_server_ip(string ip);

private:
    static int get_full_data(void *buffer,size_t size, size_t nmemb, void *userp);
    static size_t data_callback(void *buffer,size_t size, size_t nmemb, void *userp);

private:
    string server_ip;
};

#endif

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

Http.cpp

#include <string.h>

#include "Http.h"
//#include "zlog.h"
//#include "Global.h"



Http::Http()
{

}

Http::~Http()
{

}

int Http::get_full_data(void *buffer,size_t size, size_t nmemb, void *userp)
{
    size_t realsize = size * nmemb;
    if ((0 != realsize) && ((char*)userp != NULL)){
        struct MemoryStruct *mem = (struct MemoryStruct *)userp;
        char *ptr = (char*)realloc(mem->memory, mem->size + realsize + 1);
        if(ptr == NULL) {
            /* out of memory! */
            return 0;
        }
        mem->memory = ptr;
        memcpy(&(mem->memory[mem->size]), buffer, realsize);
        mem->size += realsize;
        mem->memory[mem->size] = 0;
    }
}

size_t Http::data_callback(void *buffer,size_t size, size_t nmemb, void *userp)
{
    size_t realsize = size * nmemb;
    get_full_data(buffer,size, nmemb, userp);
    return realsize;
}

string Http::post(string url,const string data)
{
    string recv;
    struct MemoryStruct chunk;
    chunk.memory = (char*)malloc(sizeof(char*));
    chunk.size = 0;
    CURL * curl = curl_easy_init();
    CURLcode res;
//    zlog_info(global_zlog_t,"post:%s",data.c_str());
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_easy_setopt(curl,CURLOPT_URL,url.c_str());
    curl_easy_setopt(curl,CURLOPT_POST,1L);
    curl_easy_setopt(curl,CURLOPT_POSTFIELDS,data.c_str());
    curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,data.length());
    curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,data_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)(&chunk));
    res = curl_easy_perform(curl);

    if (res != CURLE_OK) {
        fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));
    }
    if (res == CURLE_OK) {
        recv = string(chunk.memory);
    }

    curl_easy_cleanup(curl);
    free(chunk.memory);
    return recv;
}

/*
CJsonObject Http::post(string url,const CJsonObject& cjson_obj)
{
    CJsonObject recv;
    struct MemoryStruct chunk;
    chunk.memory = (char*)malloc(sizeof(char*));
    chunk.size = 0;
    CURL * curl = curl_easy_init();
    CURLcode res;
        struct curl_slist* header = NULL;
        string datastr = cjson_obj.ToString();
    zlog_info(global_zlog_t,"post:%s",datastr.c_str());
    //cout << "url:" << url.c_str() << endl;
        //cout << "send data:\n" << datastr.c_str() << endl;
        header = curl_slist_append(header,"Content-Type:application/json");

//    curl_easy_setopt(curl,CURLOPT_VERBOSE,1);
        curl_easy_setopt(curl,CURLOPT_HTTPHEADER,header);
    curl_easy_setopt(curl,CURLOPT_URL,url.c_str());
    curl_easy_setopt(curl,CURLOPT_POST,1L);
    curl_easy_setopt(curl,CURLOPT_POSTFIELDS,datastr.c_str());
    curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,datastr.length());
    curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,data_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)(&chunk));
    res = curl_easy_perform(curl);

    if (res != CURLE_OK) {
        fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));
    }
    if (res == CURLE_OK) {
                //cout << "chunk.memory:\n" << chunk.memory << endl;
        CJsonObject recv_tmp(string(chunk.memory));
                recv = recv_tmp;
    }

    curl_easy_cleanup(curl);
    free(chunk.memory);
    return recv;
}
*/
void Http::set_server_ip(string ip)
{

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120

调用

QVariantMap param;
param["mod"] = "getsyscfg";
_log(MLogPath,param);
QString url = getHosturl();
string resultstr = Http::post(url.toStdString(),QJsonDocument::fromVariant(param).toJson().toStdString());
std::cout << "getsys:" << resultstr << std::endl;
QString result = QString::fromStdString(resultstr);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

实现https发送数据重点是下面两行的配置

    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
  • 1
  • 2

CURLOPT_SSL_VERFYPEER :

    设置这个选项,将会决定curl是否会验证证书的有效性,0 表示不会验证,1 表示会验证。默认值为1。
  • 1

CURLOPT_SSL_VERIFYHOST :

     当建立 TLS 和 SSL 连接的时候,服务器会发一个证书来表明它的身份。

    值为0时,不管证书的上的名称,也不验证证书是否正确。

    值为1时,检查证书中是否包含名称。

    值为2的时候,就表示要求服务器返回的证书中包含的服务器名称,必须和curl请求Url上的域名是一样的,否则连接会失败。(有可能弹出上述错误: “SSL peer certificate or SSH remote key was not OK”)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

参考:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/685444
推荐阅读
相关标签
  

闽ICP备14008679号