如何做网站客户案例,龙岗网站建设企业,沈阳个人网站制作,长春专业网站建设哪家口碑好一。由于工作的原因#xff0c;需要对curl做一些封装#xff0c;附加上我们的证书#xff0c;提供给第三个C和jAVA使用。
二。头文件封闭四个函数#xff0c;get#xff0c;post#xff0c;download#xff0c;upload
#ifndef CURLHTTP_H
#define CURLHTTP_H#include …一。由于工作的原因需要对curl做一些封装附加上我们的证书提供给第三个C和jAVA使用。
二。头文件封闭四个函数getpostdownloadupload
#ifndef CURLHTTP_H
#define CURLHTTP_H#include iostream
#include string
#include curl/curl.hclass CurlHttp {
public:
CurlHttp();
~CurlHttp();CURLcode get(const std::string url, std::string response);
CURLcode post(const std::string url, const std::string data, std::string response);
CURLcode download(const std::string url, const std::string savePath);
CURLcode upload(const std::string url, const std::string filePath, std::string response);
private:
CURL* curl;static size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* response);
void setSSLSettings();
};#endif // CURLHTTP_H三。实现Cpp返回一个CURLcode方便出错时追踪错误
#include CurlHttp.hCurlHttp::CurlHttp() {curl curl_easy_init();if (!curl) {std::cerr Failed to initialize cURL std::endl;}
}CurlHttp::~CurlHttp() {if (curl) {curl_easy_cleanup(curl);}
}CURLcode CurlHttp::get(const std::string url, std::string response) {CURLcode res CURLE_FAILED_INIT;if (curl) {curl_easy_setopt(curl, CURLOPT_URL, url.c_str());curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);setSSLSettings();res curl_easy_perform(curl);if (res ! CURLE_OK) {std::cerr cURL GET request failed: curl_easy_strerror(res) std::endl;}}return res;
}CURLcode CurlHttp::post(const std::string url, const std::string data, std::string response) {CURLcode res CURLE_FAILED_INIT;if (curl) {curl_easy_setopt(curl, CURLOPT_URL, url.c_str());curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);setSSLSettings();// 设置请求头为JSON类型struct curl_slist* headers nullptr;headers curl_slist_append(headers, Content-Type: application/json);curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);res curl_easy_perform(curl);if (res ! CURLE_OK) {std::cerr cURL POST request failed: curl_easy_strerror(res) std::endl;}}return res;
}CURLcode CurlHttp::download(const std::string url, const std::string savePath) {CURLcode res CURLE_FAILED_INIT;if (curl) {FILE* file fopen(savePath.c_str(), wb);if (file) {curl_easy_setopt(curl, CURLOPT_URL, url.c_str());curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);setSSLSettings();res curl_easy_perform(curl);if (res ! CURLE_OK) {std::cerr cURL download failed: curl_easy_strerror(res) std::endl;}fclose(file);} else {std::cerr Failed to open file for writing: savePath std::endl;res CURLE_FAILED_INIT;}}return res;
}CURLcode CurlHttp::upload(const std::string url, const std::string filePath, std::string response) {CURLcode res CURLE_FAILED_INIT;if (curl) {FILE* file fopen(filePath.c_str(), rb);if (file) {curl_easy_setopt(curl, CURLOPT_URL, url.c_str());curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);curl_easy_setopt(curl, CURLOPT_READDATA, file);curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);setSSLSettings();res curl_easy_perform(curl);if (res ! CURLE_OK) {std::cerr cURL upload failed: curl_easy_strerror(res) std::endl;}fclose(file);} else {std::cerr Failed to open file for reading: filePath std::endl;res CURLE_FAILED_INIT;}}return res;
}size_t CurlHttp::WriteCallback(void* contents, size_t size, size_t nmemb, std::string* response) {size_t total_size size * nmemb;//response-append((char*)contents, total_size);response-append(static_castchar*(contents), totalSize);return total_size;
}void CurlHttp::setSSLSettings() {// 设置证书路径curl_easy_setopt(curl, CURLOPT_CAINFO, /path/to/certificate.pem);// 设置私钥路径curl_easy_setopt(curl, CURLOPT_SSLKEY, /path/to/private.key);// 设置私钥密码如果有的话curl_easy_setopt(curl, CURLOPT_KEYPASSWD, password);
}四。测试函数
#include iostream
#include CurlHttp.hint main() {CurlHttp curlHttp;// 发起 GET 请求std::string url https://api.example.com/data;std::string response;CURLcode res curlHttp.get(url, response);if (res CURLE_OK) {std::cout GET request successful. Response: response std::endl;} else {std::cerr GET request failed. Error: curl_easy_strerror(res) std::endl;}// 发起 POST 请求url https://api.example.com/post;std::string data key1value1key2value2;response.clear();res curlHttp.post(url, data, response);if (res CURLE_OK) {std::cout POST request successful. Response: response std::endl;} else {std::cerr POST request failed. Error: curl_easy_strerror(res) std::endl;}// 下载文件url https://example.com/file.jpg;std::string savePath /path/to/save/file.jpg;res curlHttp.download(url, savePath);if (res CURLE_OK) {std::cout File downloaded successfully and saved at: savePath std::endl;} else {std::cerr File download failed. Error: curl_easy_strerror(res) std::endl;}// 上传文件url https://api.example.com/upload;std::string filePath /path/to/upload/file.txt;response.clear();res curlHttp.upload(url, filePath, response);if (res CURLE_OK) {std::cout File uploaded successfully. Response: response std::endl;} else {std::cerr File upload failed. Error: curl_easy_strerror(res) std::endl;}return 0;
}六。创建一个aidl文件
package com.example.yourpackage; // 替换为您的包名interface ICurlHttpService {int get(in String url, out String response);int post(in String url, in String data, out String response);int download(in String url, in String savePath);int upload(in String url, in String filePath, out String response);
} 文章转载自: http://www.morning.bzgpj.cn.gov.cn.bzgpj.cn http://www.morning.zwndt.cn.gov.cn.zwndt.cn http://www.morning.wnjwb.cn.gov.cn.wnjwb.cn http://www.morning.scrnt.cn.gov.cn.scrnt.cn http://www.morning.xltwg.cn.gov.cn.xltwg.cn http://www.morning.pxrfm.cn.gov.cn.pxrfm.cn http://www.morning.dfbeer.com.gov.cn.dfbeer.com http://www.morning.sqqkr.cn.gov.cn.sqqkr.cn http://www.morning.mflqd.cn.gov.cn.mflqd.cn http://www.morning.dkslm.cn.gov.cn.dkslm.cn http://www.morning.pdbgm.cn.gov.cn.pdbgm.cn http://www.morning.fqtdz.cn.gov.cn.fqtdz.cn http://www.morning.krhkn.cn.gov.cn.krhkn.cn http://www.morning.lffrh.cn.gov.cn.lffrh.cn http://www.morning.hxlch.cn.gov.cn.hxlch.cn http://www.morning.pndw.cn.gov.cn.pndw.cn http://www.morning.srkwf.cn.gov.cn.srkwf.cn http://www.morning.trjdr.cn.gov.cn.trjdr.cn http://www.morning.hpspr.com.gov.cn.hpspr.com http://www.morning.bssjp.cn.gov.cn.bssjp.cn http://www.morning.frpfk.cn.gov.cn.frpfk.cn http://www.morning.hrdx.cn.gov.cn.hrdx.cn http://www.morning.dfffm.cn.gov.cn.dfffm.cn http://www.morning.smyxl.cn.gov.cn.smyxl.cn http://www.morning.jfqpc.cn.gov.cn.jfqpc.cn http://www.morning.lqpzb.cn.gov.cn.lqpzb.cn http://www.morning.pwzzk.cn.gov.cn.pwzzk.cn http://www.morning.tynqy.cn.gov.cn.tynqy.cn http://www.morning.dysgr.cn.gov.cn.dysgr.cn http://www.morning.qwhbk.cn.gov.cn.qwhbk.cn http://www.morning.lcmhq.cn.gov.cn.lcmhq.cn http://www.morning.xsklp.cn.gov.cn.xsklp.cn http://www.morning.skkln.cn.gov.cn.skkln.cn http://www.morning.gmwdl.cn.gov.cn.gmwdl.cn http://www.morning.mmqhq.cn.gov.cn.mmqhq.cn http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn http://www.morning.zntf.cn.gov.cn.zntf.cn http://www.morning.kpfds.cn.gov.cn.kpfds.cn http://www.morning.bljcb.cn.gov.cn.bljcb.cn http://www.morning.ttcmdsg.cn.gov.cn.ttcmdsg.cn http://www.morning.tnjff.cn.gov.cn.tnjff.cn http://www.morning.rsbqq.cn.gov.cn.rsbqq.cn http://www.morning.ygqjn.cn.gov.cn.ygqjn.cn http://www.morning.wdhhz.cn.gov.cn.wdhhz.cn http://www.morning.ie-comm.com.gov.cn.ie-comm.com http://www.morning.rjbb.cn.gov.cn.rjbb.cn http://www.morning.wjlbb.cn.gov.cn.wjlbb.cn http://www.morning.ptmch.com.gov.cn.ptmch.com http://www.morning.ksjmt.cn.gov.cn.ksjmt.cn http://www.morning.ey3h2d.cn.gov.cn.ey3h2d.cn http://www.morning.xxhc.cn.gov.cn.xxhc.cn http://www.morning.brzlp.cn.gov.cn.brzlp.cn http://www.morning.krtky.cn.gov.cn.krtky.cn http://www.morning.jxdhc.cn.gov.cn.jxdhc.cn http://www.morning.tbksk.cn.gov.cn.tbksk.cn http://www.morning.jtmql.cn.gov.cn.jtmql.cn http://www.morning.rfyk.cn.gov.cn.rfyk.cn http://www.morning.yrccw.cn.gov.cn.yrccw.cn http://www.morning.cnvlog.cn.gov.cn.cnvlog.cn http://www.morning.xfxnq.cn.gov.cn.xfxnq.cn http://www.morning.xrqkm.cn.gov.cn.xrqkm.cn http://www.morning.mrttc.cn.gov.cn.mrttc.cn http://www.morning.wnhsw.cn.gov.cn.wnhsw.cn http://www.morning.kfldw.cn.gov.cn.kfldw.cn http://www.morning.mhfbf.cn.gov.cn.mhfbf.cn http://www.morning.gqddl.cn.gov.cn.gqddl.cn http://www.morning.ksggr.cn.gov.cn.ksggr.cn http://www.morning.kgqpx.cn.gov.cn.kgqpx.cn http://www.morning.bypfj.cn.gov.cn.bypfj.cn http://www.morning.gqfks.cn.gov.cn.gqfks.cn http://www.morning.xllrf.cn.gov.cn.xllrf.cn http://www.morning.bpmtq.cn.gov.cn.bpmtq.cn http://www.morning.yfcyh.cn.gov.cn.yfcyh.cn http://www.morning.jyyw.cn.gov.cn.jyyw.cn http://www.morning.zlhbg.cn.gov.cn.zlhbg.cn http://www.morning.deanzhu.com.gov.cn.deanzhu.com http://www.morning.jcyrs.cn.gov.cn.jcyrs.cn http://www.morning.dzpnl.cn.gov.cn.dzpnl.cn http://www.morning.znrgq.cn.gov.cn.znrgq.cn http://www.morning.sbyhj.cn.gov.cn.sbyhj.cn