libcurl 请求无结果

Libcurl request no result

我在 C++ 中使用 libcurl 扩展。我向 elasticsearch 服务器发送请求,但结果为“null”。 我在 PHP 中重建了 curl 请求并且它起作用了。 我如何从网络服务器获取结果?

CURL *curl;
CURLcode res;

std::stringstream ss_url; 
ss_url << "http://IP/newgame/player/2/_update";

char *post_url = const_cast<char*>(ss_url.str().c_str());


curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, post_url);
curl_easy_setopt(curl, CURLOPT_PORT, 9200);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl,CURLOPT_POSTFIELDS, '{ "doc": { "x": 555555, "y": 287273 }}');
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);

编辑:

我已尝试记录卷曲答案。但是文件是空的。

FILE *curllog = fopen("/home/elasticsearch_curl.log", "w+");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, curllog);

通过将其替换为实际字符串来检查 post_url 转换是否存在问题。在 curl_set_opt 中 CURLOPT_URL

curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com");

可能是问题所在。它对我有用。 将其包含在 curl_easy_perform 之后以进行调试和检查。

if(res != CURLE_OK) { fprintf(stderr, "curl_easy_perform()failed:%s\n",curl_easy_strerror(res));}

一个有效的 GET 示例: https://gist.github.com/k3ut0i/737b6d19713ef5aa0a74