简体   繁体   English

嵌套的 libcurl 请求 c++

[英]Nested libcurl request c++

My question would be how would I do a nested curl request because my current code is this and does not work.我的问题是我将如何执行嵌套的 curl 请求,因为我当前的代码是这个并且不起作用。 I does not work as I can see the requests going to my server and there is only one which is a GET request.我不工作,因为我可以看到请求发送到我的服务器,并且只有一个是 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;

}

This prints Hello to stdout but does not run the curl POST request, so How would one do a nested curl request.这会将 Hello 打印到标准输出,但不会运行 curl POST 请求,因此如何执行嵌套的 curl 请求。

EDIT: I tried removing the if statement.编辑:我尝试删除 if 语句。 So my question is how does one do two libcurl requests所以我的问题是如何做两个 libcurl 请求

Your curl_easy_perform(hnd) returns 3 :您的curl_easy_perform(hnd)返回3

Via cURL :通过cURL

CURLE_URL_MALFORMAT (3) CURLE_URL_MALFORMAT (3)

The URL was not properly formatted. URL 的格式不正确。

You will have to pass a char * instead of a std::string (see here ):你将不得不传递一个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