赞
踩
使用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
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) { }
调用
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);
实现https发送数据重点是下面两行的配置
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
CURLOPT_SSL_VERFYPEER :
设置这个选项,将会决定curl是否会验证证书的有效性,0 表示不会验证,1 表示会验证。默认值为1。
CURLOPT_SSL_VERIFYHOST :
当建立 TLS 和 SSL 连接的时候,服务器会发一个证书来表明它的身份。
值为0时,不管证书的上的名称,也不验证证书是否正确。
值为1时,检查证书中是否包含名称。
值为2的时候,就表示要求服务器返回的证书中包含的服务器名称,必须和curl请求Url上的域名是一样的,否则连接会失败。(有可能弹出上述错误: “SSL peer certificate or SSH remote key was not OK”)
参考:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。