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

ckplayer 视频网站网络舆情分析报告

ckplayer 视频网站,网络舆情分析报告,开发手机端网站,贵阳响应式网站开发一。由于工作的原因,需要对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://www.tj-hxxt.cn/news/16482.html

相关文章:

  • 合肥关键词排名工具seo还能赚钱吗
  • 官方网站开发制作公司百度竞价代运营托管
  • 网站模板与网站定制版的区别百度快速seo优化
  • 怎么看一个网站做的好不好樱花bt引擎
  • 宣传单页制作appseo霸屏软件
  • 漯河网站推广多少钱ue5培训机构哪家强
  • 搜索引擎网站推广可以自己做吗西安网络推广优化培训
  • 宿州网站建设多少钱关键词优化排名详细步骤
  • php做网站有哪些好处百度营销推广
  • 重庆求建网站seo软件代理
  • 网站建设制作设计优化seo网站推广
  • 哈尔滨做网站的免费html网站模板
  • 给实体店老板做的网站seo关键词排名网络公司
  • 网站建设 齐鲁软件园sem是什么设备
  • 做动效网站小红书怎么推广引流
  • 个人网站设计毕业论文知乎漳州seo网站快速排名
  • 政府网站建设目标株洲最新今日头条
  • 做化学合成的网站有哪些seo搜索优化公司
  • 做购物网站有什么要求吗哪个浏览器看黄页最快夸克浏览器
  • php和mysql做租车网站网站优化费用报价明细
  • wordpress文章采集属于seo网站优化
  • 如何做网站旅游产品分析百度发布
  • 投资 公司 网站模板百度大数据平台
  • 档案网站建设愿景手机优化软件排名
  • 分析对手网站的优化方法网络推广平台软件app
  • 山东手机响应式网站建设设计自媒体是什么
  • 江苏市场监督管理局苏州seo关键词优化外包
  • 二手房网签合同在哪个网站做山东进一步优化
  • 1号网站建设 高端网站建设百度seo工具
  • 蓝色风格企业网站东莞网站推广技巧