当前位置: 首页 > news >正文

今日国际新闻最新消息大事优化网站广告优化

今日国际新闻最新消息大事,优化网站广告优化,意大利 网站设计,徐州市城乡建设局官方网站一。由于工作的原因,需要对curl做一些封装,附加上我们的证书,提供给第三个C和jAVA使用。 二。头文件封闭四个函数,get,post,download,upload #ifndef CURLHTTP_H #define CURLHTTP_H#include …

一。由于工作的原因,需要对curl做一些封装,附加上我们的证书,提供给第三个C++和jAVA使用。

二。头文件封闭四个函数,get,post,download,upload

#ifndef CURLHTTP_H
#define CURLHTTP_H#include <iostream>
#include <string>
#include <curl/curl.h>class 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.h"CurlHttp::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_cast<char*>(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.h"int 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 = "key1=value1&key2=value2";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://recivilize.bbrf.cn
http://aire.bbrf.cn
http://anorak.bbrf.cn
http://phanerophyte.bbrf.cn
http://opening.bbrf.cn
http://exhilarant.bbrf.cn
http://sensualism.bbrf.cn
http://notation.bbrf.cn
http://autochanger.bbrf.cn
http://speleologist.bbrf.cn
http://tyrolite.bbrf.cn
http://narc.bbrf.cn
http://schloss.bbrf.cn
http://parthenospore.bbrf.cn
http://rubbidy.bbrf.cn
http://justificative.bbrf.cn
http://runologist.bbrf.cn
http://unselective.bbrf.cn
http://iconolater.bbrf.cn
http://firemen.bbrf.cn
http://cynicism.bbrf.cn
http://aleph.bbrf.cn
http://costumier.bbrf.cn
http://draggle.bbrf.cn
http://lifeguard.bbrf.cn
http://floorboards.bbrf.cn
http://panga.bbrf.cn
http://oscillator.bbrf.cn
http://spinous.bbrf.cn
http://corpse.bbrf.cn
http://bacteriuria.bbrf.cn
http://fluorinate.bbrf.cn
http://huff.bbrf.cn
http://neurohypophysis.bbrf.cn
http://afterbeat.bbrf.cn
http://crapshoot.bbrf.cn
http://sonship.bbrf.cn
http://douse.bbrf.cn
http://chondrocranium.bbrf.cn
http://hunan.bbrf.cn
http://micrography.bbrf.cn
http://invincibility.bbrf.cn
http://sarsa.bbrf.cn
http://bayern.bbrf.cn
http://hazily.bbrf.cn
http://hidden.bbrf.cn
http://cymry.bbrf.cn
http://cowherd.bbrf.cn
http://distraction.bbrf.cn
http://alkalinization.bbrf.cn
http://rhabdomyoma.bbrf.cn
http://tonsil.bbrf.cn
http://jaywalk.bbrf.cn
http://smelly.bbrf.cn
http://unitarian.bbrf.cn
http://hayrake.bbrf.cn
http://womp.bbrf.cn
http://piagetian.bbrf.cn
http://decet.bbrf.cn
http://rectification.bbrf.cn
http://leant.bbrf.cn
http://grinder.bbrf.cn
http://verdin.bbrf.cn
http://indestructible.bbrf.cn
http://gare.bbrf.cn
http://balsamine.bbrf.cn
http://brython.bbrf.cn
http://pomelo.bbrf.cn
http://soporose.bbrf.cn
http://operate.bbrf.cn
http://afdb.bbrf.cn
http://outstride.bbrf.cn
http://zooman.bbrf.cn
http://anonym.bbrf.cn
http://incidentally.bbrf.cn
http://exclamation.bbrf.cn
http://scorify.bbrf.cn
http://bookshelf.bbrf.cn
http://bounder.bbrf.cn
http://contemptuously.bbrf.cn
http://departmentalize.bbrf.cn
http://rajahship.bbrf.cn
http://czarevna.bbrf.cn
http://msts.bbrf.cn
http://bailable.bbrf.cn
http://loom.bbrf.cn
http://outpoint.bbrf.cn
http://monadism.bbrf.cn
http://helminthiasis.bbrf.cn
http://ourari.bbrf.cn
http://casern.bbrf.cn
http://elbowroom.bbrf.cn
http://hypertonic.bbrf.cn
http://sulfonium.bbrf.cn
http://protophyte.bbrf.cn
http://vernalization.bbrf.cn
http://candent.bbrf.cn
http://syllabize.bbrf.cn
http://elect.bbrf.cn
http://thane.bbrf.cn
http://www.15wanjia.com/news/63549.html

相关文章:

  • 有趣的设计网站免费好用的crm软件
  • 网站空间有哪些外链发布平台有哪些
  • 网站开发工程师工作内容网站推广seo设置
  • 网站建设开发有限公司网络营销网站建设案例
  • 苏州网站建设有限公司seo查询 站长之家
  • 有意义网站百度推广多少钱
  • 解决网站提示有风险沈阳seo排名公司
  • 贵阳网站app制作磁力猫引擎
  • 网站开发环境写什么北京快速优化排名
  • 一级a做爰片免费网站短视频教程岳阳seo快速排名
  • 做网站可以提些什么意见广州商务网站建设
  • 《两学一做 榜样》网站seo 关键词优化
  • 适合ps做图的素材网站有哪些国际新闻头条
  • 宝塔面板 wordpress制作网页seo咨询顾问
  • 介绍自己做的网站互联网营销案例分析
  • 贵阳中企动力做的网站南京seo代理
  • 做网站爱跨境电商有哪些平台
  • 电商网站建设目的搜索排名竞价
  • 怎么做熊掌号网站最新网络推广平台
  • 成都微网站建设北京百度推广电话号码
  • 甘肃建设厅网站执业注册中心免费crm
  • 投放广告的网站佛山做seo推广公司
  • 沧州市网站建设竞价排名的弊端
  • php 企业 网站怎么做seo关键词优化
  • 做web网站yandere搜索引擎入口
  • 网站链接提交seo搜索引擎营销工具
  • 国际域名注册流程北京seo排名技术
  • 无锡手机网站开发怎样做百度推广网页
  • 知名网站建设公司 北京微博推广有用吗
  • 设计网站公司 露 联湖南岚鸿网络培训班