jni开发: 怎么用c++代码实现这个请求并获得响应内容 curl -k -s --data-binary s sbody httpssrv0151asrcom8007asrsn_active2
您可以使用以下代码片段来使用C++代码实现该请求并获取响应内容:
#include <iostream>
#include <curl/curl.h>
size_t writeCallback(void* contents, size_t size, size_t nmemb, std::string* response) {
size_t totalSize = size * nmemb;
response->append((char*)contents, totalSize);
return totalSize;
}
int main() {
CURL* curl;
CURLcode res;
// 初始化CURL库
curl_global_init(CURL_GLOBAL_ALL);
// 创建CURL句柄
curl = curl_easy_init();
if (curl) {
std::string url = "https://srv01.51asr.com:8007/asrsn_active2";
std::string body = "your_request_body";
// 设置请求URL
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
// 设置POST请求
curl_easy_setopt(curl, CURLOPT_POST, 1L);
// 设置请求体
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());
// 设置忽略SSL证书验证
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
// 设置写回调函数
std::string response;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
// 发送请求并获取响应
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
}
else {
std::cout << "Response: " << response << std::endl;
}
// 清理CURL句柄
curl_easy_cleanup(curl);
}
// 清理CURL库
curl_global_cleanup();
return 0;
}
请将"your_request_body"替换为您的实际请求体内容。此代码使用libcurl库来执行HTTP请求,并使用C++的回调函数来处理响应内容。请确保已安装libcurl库,并在编译时链接该库
原文地址: https://www.cveoy.top/t/topic/iHbt 著作权归作者所有。请勿转载和采集!