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

武汉做营销型网站建设网站搜什么关键词

武汉做营销型网站建设,网站搜什么关键词,动漫设计专业属于什么大类,网站备案证件在现代软件开发中,与外部API服务进行通信已成为常见需求。本文将展示如何使用C和libcurl库实现基本的HTTP请求,包括GET请求、POST请求(带JSON数据)以及包含文件上传的POST请求。 准备工作 首先,需要确保已安装libcur…

在现代软件开发中,与外部API服务进行通信已成为常见需求。本文将展示如何使用C++和libcurl库实现基本的HTTP请求,包括GET请求、POST请求(带JSON数据)以及包含文件上传的POST请求。

准备工作

首先,需要确保已安装libcurl库,并正确链接到项目中。如果还没有安装libcurl,可以在libcurl官网找到安装方法。使用CMake构建项目时,可以在CMakeLists.txt中指定库路径,确保CURL能够找到:

# set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/libcurl/share/curl")  # 替换为 CURL 安装的实际路径
find_package(CURL REQUIRED)
target_link_libraries(your_project_name PRIVATE CURL::libcurl)

基本结构

代码的核心是三个函数,分别用于GET请求、带JSON的POST请求和包含文件上传的POST请求。每个函数使用了libcurl的不同配置来处理具体需求。

代码结构和功能实现

1. req_reply:处理请求响应的函数

这个函数是libcurl的回调函数,用于处理从服务器返回的数据,将数据写入到指定的字符串流中。

size_t req_reply(void *ptr, size_t size, size_t nmemb, void *stream) {string *str = (string *) stream;(*str).append((char *) ptr, size * nmemb);return size * nmemb;
}
2. GET 请求

GET请求是最常用的请求类型,用于从服务器获取资源。curl_get_req函数实现了GET请求,通过curl_easy_setopt函数来设置URL和请求参数。

CURLcode curl_get_req(const std::string &url, std::string &response) {CURL *curl = curl_easy_init();CURLcode res;if (curl) {curl_easy_setopt(curl, CURLOPT_URL, url.c_str());curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);  // 忽略SSL证书验证curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &response);curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);res = curl_easy_perform(curl);curl_easy_cleanup(curl);}return res;
}
3. 发送带JSON数据的POST请求

POST请求通常用于将数据发送到服务器,尤其是RESTful API。curl_post_json函数可以发送包含JSON数据的POST请求,代码通过设置Content-Type头为application/json来指定数据类型。

CURLcode curl_post_json(const string &url, const string &jsonData, string &response) {CURL *curl = curl_easy_init();CURLcode res;if (curl) {struct curl_slist *headers = NULL;headers = curl_slist_append(headers, "Content-Type: application/json");curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);curl_easy_setopt(curl, CURLOPT_POST, 1);curl_easy_setopt(curl, CURLOPT_URL, url.c_str());curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonData.c_str());curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &response);res = curl_easy_perform(curl);curl_slist_free_all(headers);curl_easy_cleanup(curl);}return res;
}
4. 发送包含JSON和文件的POST请求

在某些情况下,我们可能需要同时发送JSON数据和文件。这里用curl_mime实现多部分表单上传。curl_mime允许将JSON数据和文件字段一起打包并发送给服务器。

CURLcode curl_post_with_file(const string &url, const string &jsonData, const string &filePath, string &response) {CURL *curl = curl_easy_init();CURLcode res;if (curl) {struct curl_slist *headers = NULL;headers = curl_slist_append(headers, "Content-Type: multipart/form-data");curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);// 创建 MIME 表单curl_mime *form = curl_mime_init(curl);// 添加 JSON 数据字段curl_mimepart *jsonField = curl_mime_addpart(form);curl_mime_name(jsonField, "json");curl_mime_data(jsonField, jsonData.c_str(), CURL_ZERO_TERMINATED);curl_mime_type(jsonField, "application/json");// 添加文件字段curl_mimepart *fileField = curl_mime_addpart(form);curl_mime_name(fileField, "file");curl_mime_filedata(fileField, filePath.c_str());curl_mime_type(fileField, "image/png");curl_easy_setopt(curl, CURLOPT_URL, url.c_str());curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &response);res = curl_easy_perform(curl);curl_mime_free(form);curl_slist_free_all(headers);curl_easy_cleanup(curl);}return res;
}

测试用例

最后,我们在main函数中展示了三个测试用例,分别是GET请求、带JSON数据的POST请求和带文件的POST请求。

#include <iostream>
#include <string>
#include "curl/curl.h"using namespace std;// 请求的回复处理函数
size_t req_reply(void *ptr, size_t size, size_t nmemb, void *stream) {string *str = (string *) stream;(*str).append((char *) ptr, size * nmemb);return size * nmemb;
}// HTTP GET 请求
CURLcode curl_get_req(const std::string &url, std::string &response) {CURL *curl = curl_easy_init();CURLcode res;if (curl) {// 设置 URL 地址curl_easy_setopt(curl, CURLOPT_URL, url.c_str());// 设置忽略 SSL 证书验证curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);// 设置回复处理函数curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &response);curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);curl_easy_setopt(curl, CURLOPT_HEADER, 1);// 设置连接和响应超时时间curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);res = curl_easy_perform(curl);  // 执行 GET 请求curl_easy_cleanup(curl);  // 清理 curl 资源}return res;
}// 发送带有 JSON 数据的 HTTP POST 请求
CURLcode curl_post_json(const string &url, const string &jsonData, string &response) {CURL *curl = curl_easy_init();CURLcode res;if (curl) {struct curl_slist *headers = NULL;headers = curl_slist_append(headers, "Content-Type: application/json"); // 设置 JSON 类型头部//    headers = curl_slist_append(headers, ("appcode: " + appcode).c_str());//    headers = curl_slist_append(headers, ("User-Agent: " + agent).c_str());curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);// 设置 POST 请求参数curl_easy_setopt(curl, CURLOPT_POST, 1);curl_easy_setopt(curl, CURLOPT_URL, url.c_str());curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonData.c_str());curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &response);curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);curl_easy_setopt(curl, CURLOPT_HEADER, 1);curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);res = curl_easy_perform(curl);  // 执行 POST 请求curl_slist_free_all(headers); // 释放头部内存curl_easy_cleanup(curl);  // 清理 curl 资源}return res;
}// 发送包含 JSON 数据和文件的 POST 请求
CURLcode curl_post_with_file(const string &url, const string &jsonData, const string &filePath, string &response) {CURL *curl = curl_easy_init();CURLcode res;if (curl) {struct curl_slist *headers = NULL;headers = curl_slist_append(headers, "Content-Type: multipart/form-data"); // 设置多部分表单头部curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);// 创建 MIME 表单curl_mime *form = curl_mime_init(curl);// 添加 JSON 数据字段curl_mimepart *jsonField = curl_mime_addpart(form);curl_mime_name(jsonField, "json");curl_mime_data(jsonField, jsonData.c_str(), CURL_ZERO_TERMINATED);curl_mime_type(jsonField, "application/json");// 添加文件字段curl_mimepart *fileField = curl_mime_addpart(form);curl_mime_name(fileField, "file");curl_mime_filedata(fileField, filePath.c_str());curl_mime_type(fileField, "image/png");  // 设置文件类型为 image/png// 设置 CURL 参数curl_easy_setopt(curl, CURLOPT_URL, url.c_str());curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &response);curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);// 执行请求res = curl_easy_perform(curl);// 清理 MIME 表单和头部curl_mime_free(form);curl_slist_free_all(headers);curl_easy_cleanup(curl);}return res;
}int main() {curl_global_init(CURL_GLOBAL_ALL);// 测试 GET 请求string getUrlStr = "http://cn.bing.com/images/trending?form=Z9LH";string getResponseStr;auto res = curl_get_req(getUrlStr, getResponseStr);if (res != CURLE_OK)cerr << "GET 请求失败: " + string(curl_easy_strerror(res)) << endl;elsecout << getResponseStr << endl;// JSON POST 请求string postUrlStr = "https://api.example.com/endpoint";string jsonData = R"({"key1": "value1", "key2": "value2"})";string postResponseStr;res = curl_post_json(postUrlStr, jsonData, postResponseStr);if (res != CURLE_OK)cerr << "POST 请求失败: " + string(curl_easy_strerror(res)) << endl;elsecout << postResponseStr << endl;// JSON 和文件的 POST 请求string postfileStr = "https://api.example.com/upload";string filePath = "path/to/image.png";  // 指定上传的文件路径res = curl_post_with_file(postfileStr, jsonData, filePath, postResponseStr);if (res != CURLE_OK)cerr << "文件 POST 请求失败: " + string(curl_easy_strerror(res)) << endl;elsecout << "服务器响应:\n" << postResponseStr << endl;// 清空curl_global_cleanup();system("pause");return 0;
}

总结

使用libcurl实现HTTP请求是与外部API或服务器进行通信的强大工具。本文展示了如何发送GET请求、带JSON数据的POST请求以及包含文件的POST请求,涵盖了许多实际应用中的需求。通过合理配置libcurl选项,可以轻松实现高效且可靠的网络请求。


文章转载自:
http://wanjiaundermanned.stph.cn
http://wanjiangaio.stph.cn
http://wanjiastornello.stph.cn
http://wanjiaveritably.stph.cn
http://wanjiaofficialdom.stph.cn
http://wanjiahermitry.stph.cn
http://wanjiadomestic.stph.cn
http://wanjiaebullioscope.stph.cn
http://wanjiascintillogram.stph.cn
http://wanjiaambulant.stph.cn
http://wanjiacapsular.stph.cn
http://wanjiarennin.stph.cn
http://wanjialightly.stph.cn
http://wanjiasolely.stph.cn
http://wanjiaspraints.stph.cn
http://wanjiaesther.stph.cn
http://wanjiashoe.stph.cn
http://wanjiadewberry.stph.cn
http://wanjiagentlevoiced.stph.cn
http://wanjianonferrous.stph.cn
http://wanjiachrysarobin.stph.cn
http://wanjiacockleshell.stph.cn
http://wanjiacollieshangie.stph.cn
http://wanjialeadswinger.stph.cn
http://wanjiahilo.stph.cn
http://wanjialats.stph.cn
http://wanjiaadjudicative.stph.cn
http://wanjiaunbooked.stph.cn
http://wanjiaclung.stph.cn
http://wanjiakedger.stph.cn
http://wanjiaess.stph.cn
http://wanjiaschopenhauerian.stph.cn
http://wanjiaharijan.stph.cn
http://wanjiagastrointestinal.stph.cn
http://wanjiagracioso.stph.cn
http://wanjiafurl.stph.cn
http://wanjianes.stph.cn
http://wanjiaevisceration.stph.cn
http://wanjiabribe.stph.cn
http://wanjiaklompen.stph.cn
http://wanjialeisure.stph.cn
http://wanjiacelebrator.stph.cn
http://wanjianitrid.stph.cn
http://wanjiaosteosis.stph.cn
http://wanjiamimicker.stph.cn
http://wanjiamyokymia.stph.cn
http://wanjiatroubadour.stph.cn
http://wanjiascintillogram.stph.cn
http://wanjiapyrrhotite.stph.cn
http://wanjiaterminal.stph.cn
http://wanjiaholpen.stph.cn
http://wanjiainalienability.stph.cn
http://wanjiaquietly.stph.cn
http://wanjiatellurous.stph.cn
http://wanjiafichu.stph.cn
http://wanjiamara.stph.cn
http://wanjiaindia.stph.cn
http://wanjiafireboard.stph.cn
http://wanjiaduel.stph.cn
http://wanjiatuboid.stph.cn
http://wanjiacelebrity.stph.cn
http://wanjiacyanogenetic.stph.cn
http://wanjiaunjealous.stph.cn
http://wanjiaiyar.stph.cn
http://wanjiacloud.stph.cn
http://wanjiatympana.stph.cn
http://wanjiamutule.stph.cn
http://wanjiafirsthand.stph.cn
http://wanjiamonodactyl.stph.cn
http://wanjiaappealing.stph.cn
http://wanjiaunsell.stph.cn
http://wanjiastricture.stph.cn
http://wanjiaarcifinious.stph.cn
http://wanjiawoo.stph.cn
http://wanjiaimpish.stph.cn
http://wanjiakeepsake.stph.cn
http://wanjiaregorge.stph.cn
http://wanjiadripolator.stph.cn
http://wanjialobeliaceous.stph.cn
http://wanjiaunweary.stph.cn
http://www.15wanjia.com/news/113245.html

相关文章:

  • 中国展览公司前十名seo是什么服
  • 成都网站建设网络推广的重要性与好处
  • 如何做高端网站建设百度seo新算法
  • 泗洪做网站营销管理制度范本
  • 做汽车价格的网站建设企业网站优化报告
  • 网站开发设计书籍怎样推广小程序平台
  • 顺的网站建设要多少钱百度搜索优化建议
  • 自己做的电商网站要多少钱青岛疫情最新情况
  • 南宁网站推广费用安卓手机优化
  • 长沙设计网站效果seo优化方案项目策划书
  • 邯郸移动网站建设报价快速网站排名优化
  • wordpress显示flash logo上海seo推广平台
  • 乐清官方网站营销手段和营销方式
  • 聊城做网站的地方亚马逊关键词快速优化
  • 公司网站建设价格线上推广策划方案
  • 怎样做农村电商网站免费人脉推广
  • 廊坊网站的优化页面优化的方法
  • 宣城公司做网站二十条疫情优化措施
  • 网站导航是什么意思网络营销方式有哪几种
  • 源码可以做网站吗一套完整的运营方案
  • 独立网站的好处建网站的软件有哪些
  • dedecms 调用 两个网站厦门seo网站排名优化
  • 政府网站建设原因百度友情链接
  • 做免费网站怎么做上海正规seo公司
  • 石家庄网站服务最新seo黑帽技术工具软件
  • 宁波网站制作公司百度页面推广
  • 网站域名需icp备案模板建站哪里有
  • 马云将来淘汰的十个行业网站建设百度爱采购平台官网
  • 十堰的网站建设天津百度推广
  • 设计上海官网seo中国是什么