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

网站文章内容的选取上海谷歌seo公司

网站文章内容的选取,上海谷歌seo公司,做网站优化的话术,网站抓取qq一。由于工作的原因,需要对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/110287.html

相关文章:

  • 政府门户网站的重要性网络营销环境分析
  • 免费书画网站怎么做的百度手机app
  • 哪些网站是单页应用b2c有哪些电商平台
  • 注册广告公司名字快抖霸屏乐云seo
  • 建e网室内设计网 模型长春关键词优化排名
  • 2016年网站设计风格推广网站文案
  • 互动科技网站建设谷歌的推广是怎么样的推广
  • 秀洲区住房和城乡建设局网站百度极速版app下载安装挣钱
  • 一家做公司点评网站软文推广营销平台
  • 17网站一起做网店 睡衣批发网站建设方案书 模板
  • 电线电缆做销售哪个网站好培训学校怎么招生
  • 合肥长丰路网站建设石家庄最新消息今天
  • 网站策划的最终体现是什么成都seo优化外包公司
  • 专门做自助游攻略的网站是哪个山东一级造价师
  • 网站开发年收入在百度怎么发广告做宣传
  • 网站建设投标文件站长之家seo信息
  • 在线视频网站开发互联网站
  • 施工企业安全培训西安关键词优化软件
  • 网络科技公司有什么职位seo外链怎么发
  • 产地证哪个网站做快链友情链接平台
  • 设计云网站建设百度发作品入口在哪里
  • 动态网站制作基础建议免费推广广告链接
  • php做网站csdn百度搜索引擎优化方案
  • 新闻聚合网站怎么做外贸企业网站设计公司
  • 做期货都看那些网站湖北搜索引擎优化
  • 网站流量少怎么做网站增加外链的方法有哪些
  • 哪些网站可以上传自己做的视频网络平台推广方案
  • 重庆营销型网站开发价格宁波seo推广服务
  • 网站建设 网址导航2023年7 8月十大新闻
  • wordpress新建模板网站的seo是什么意思