如何在c++;发送POST请求并接收它? 我用C++ LBCURL向网页发送一个POST请求,但我正在努力测试它。使用的代码是: #include <stdio.h> #include <curl/curl.h> #include <string> using namespace std; int main(void) { CURL *curl = curl_easy_init(); if(curl) { const char *data = "submit = 1"; curl_easy_setopt(curl, CURLOPT_URL, "http://10.5.10.200/website/WebFrontend/backend/posttest.php"); /* size of the POST data */ curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 10L); /* pass in a pointer to the data - libcurl will not copy */ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); curl_easy_perform(curl); } /* Perform the request, res will get the return code */ /* always cleanup */ return 0; } #包括 #包括 #包括 使用名称空间std; 内部主(空) { CURL*CURL=CURL_easy_init(); if(curl){ const char*data=“submit=1”; curl\u easy\u setopt(curl,CURLOPT\u URL,“http://10.5.10.200/website/WebFrontend/backend/posttest.php"); /*POST数据的大小*/ curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,10L); /*传入指向数据的指针-libcurl不会复制*/ curl_easy_setopt(curl,CURLOPT_POSTFIELDS,data); 旋度,易于执行(旋度); } /*执行请求时,res将获得返回代码*/ /*始终清理*/ 返回0; }

如何在c++;发送POST请求并接收它? 我用C++ LBCURL向网页发送一个POST请求,但我正在努力测试它。使用的代码是: #include <stdio.h> #include <curl/curl.h> #include <string> using namespace std; int main(void) { CURL *curl = curl_easy_init(); if(curl) { const char *data = "submit = 1"; curl_easy_setopt(curl, CURLOPT_URL, "http://10.5.10.200/website/WebFrontend/backend/posttest.php"); /* size of the POST data */ curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 10L); /* pass in a pointer to the data - libcurl will not copy */ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); curl_easy_perform(curl); } /* Perform the request, res will get the return code */ /* always cleanup */ return 0; } #包括 #包括 #包括 使用名称空间std; 内部主(空) { CURL*CURL=CURL_easy_init(); if(curl){ const char*data=“submit=1”; curl\u easy\u setopt(curl,CURLOPT\u URL,“http://10.5.10.200/website/WebFrontend/backend/posttest.php"); /*POST数据的大小*/ curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,10L); /*传入指向数据的指针-libcurl不会复制*/ curl_easy_setopt(curl,CURLOPT_POSTFIELDS,data); 旋度,易于执行(旋度); } /*执行请求时,res将获得返回代码*/ /*始终清理*/ 返回0; },c++,http,curl,libcurl,C++,Http,Curl,Libcurl,这是来自以下站点的示例代码: 结果真把我弄糊涂了。从终端我可以看到已经发送了POST请求,但从网页我无法检索任何数据。该网页是非常简单的php代码,可以打印出$\u帖子。 和 有人能帮我吗? 为什么我无法从网页获取POST请求,以及如何修复此问题? 谁能给我一个更好的方法来测试代码? 非常感谢你们 您必须实现一个回调函数,curl将在接收到的每一批数据时调用该函数 这里有一个很好的例子: 显然,可以用WriteCallback()函数中所需的任何数据类型和处理替换简单字符串 复制/粘贴Alg

这是来自以下站点的示例代码:

结果真把我弄糊涂了。从终端我可以看到已经发送了POST请求,但从网页我无法检索任何数据。该网页是非常简单的php代码,可以打印出$\u帖子。 和

有人能帮我吗? 为什么我无法从网页获取POST请求,以及如何修复此问题? 谁能给我一个更好的方法来测试代码?
非常感谢你们

您必须实现一个回调函数,curl将在接收到的每一批数据时调用该函数

这里有一个很好的例子:

显然,可以用WriteCallback()函数中所需的任何数据类型和处理替换简单字符串

复制/粘贴Alghnami的示例:

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


static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

int main(void)
{
  CURL *curl;
  CURLcode res;
  std::string readBuffer;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);

    std::cout << readBuffer << std::endl;
  }
  return 0;
}
#包括
#包括
#包括
静态大小写回(void*contents、size\t size、size\t nmemb、void*userp)
{
((std::string*)userp)->追加((char*)内容,大小*nmemb);
返回大小*nmemb;
}
内部主(空)
{
卷曲*卷曲;
卷曲编码;
字符串读取缓冲区;
curl=curl_easy_init();
if(curl){
curl\u easy\u setopt(curl,CURLOPT\u URL,“http://www.google.com");
curl\u easy\u setopt(curl,CURLOPT\u WRITEFUNCTION,WriteCallback);
curl\u easy\u setopt(curl、CURLOPT\u WRITEDATA和readBuffer);
res=旋度(curl)\u容易执行(curl);
旋度\轻松\清洁(旋度);

std::不能尝试添加卷曲\u easy\u setopt(卷曲,卷曲\u POST,1L)@JunheeShin谢谢,但它仍然不起作用…我不确定我是否正确理解了你的问题。有两个post请求1)在你的终端中,2)在你的浏览器中。两个调用都不同。都有不同的post数据。因此你看到不同的$\u post值。都是独立的请求。正如我所说的,我不理解你的困惑.您的意思是当您通过终端(C++可执行文件)发送数据时,您希望在浏览器中查看数据吗?将“提交=1”更改为“提交=1”@Ravi是的,我认为它们是相同的请求。感谢您的澄清!据我对文档的理解,回调函数是OP使用的另一种方法。我从昨天开始学习curl,您能评论一下为什么您认为这两个函数的用途相同吗?谢谢