繁体   English   中英

嵌套的 libcurl 请求 c++

[英]Nested libcurl request c++

我的问题是我将如何执行嵌套的 curl 请求,因为我当前的代码是这个并且不起作用。 我不工作,因为我可以看到请求发送到我的服务器,并且只有一个是 GET 请求。

#include <curl/curl.h>
#include <iostream>

int rad = rand() % 100000;
std::string agent_name = "https://127.0.0.1:443/agent/" + std::to_string(rad);


int main(int argc, char *argv[])
{
  CURLcode res;
  CURL *curl;

  curl = curl_easy_init();
  curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 102400L);
  curl_easy_setopt(curl, CURLOPT_URL, "https://127.0.0.1:443/");
  curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  curl_easy_setopt(curl, CURLOPT_FTP_SKIP_PASV_IP, 1L);
  curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);

  res = curl_easy_perform(curl);
  
  if (res == CURLE_OK) {
    long response_code;
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
    if (response_code == 200)
    {
      std::cout << "Hello";
      CURLcode ret;
      CURL *hnd;

      hnd = curl_easy_init();
      curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, 102400L);
      curl_easy_setopt(hnd, CURLOPT_URL, agent_name);
      curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
      curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
      curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
      curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
      curl_easy_setopt(hnd, CURLOPT_FTP_SKIP_PASV_IP, 1L);
      curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);

      ret = curl_easy_perform(hnd);

      curl_easy_cleanup(hnd);
      hnd = NULL;

    }
    else { }
  }
  curl_easy_cleanup(curl);
  curl = NULL;

}

这会将 Hello 打印到标准输出,但不会运行 curl POST 请求,因此如何执行嵌套的 curl 请求。

编辑:我尝试删除 if 语句。 所以我的问题是如何做两个 libcurl 请求

您的curl_easy_perform(hnd)返回3

通过cURL

CURLE_URL_MALFORMAT (3)

URL 的格式不正确。

你将不得不传递一个char *而不是std::string (见这里):

curl_easy_setopt(hnd, CURLOPT_URL, agent_name.c_str());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM