簡體   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