【问题标题】:How to send data with curl request如何使用 curl 请求发送数据
【发布时间】:2014-04-30 11:04:38
【问题描述】:

我正在编写用于发送带有数据的 http 请求的代码。但我不能在特定的 URL 发送数据。我正在使用 curl 进行 http 请求。我可以获得 curl 的全部输出,但到目前为止无法用它发送数据。当我发送请求时,它应该带有一些数据,比如你好。我该怎么做?

#include "stdafx.h"
#include <stdio.h>
#include "curl/curl.h"
#include "curl\easy.h"


int main(void)
{
  CURL *curl;
  CURLcode res;

  /* In windows, this will init the winsock stuff */ 
  curl_global_init(CURL_GLOBAL_ALL);

  curl = curl_easy_init();
  if(curl) {
    /* First set the URL that is about to receive our POST. This URL can
       just as well be a https:// URL if that is what should receive the
       data. */ 
  curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/Main/GiveResponse.jsp");
    /* Now specify the POST data */ 

 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "prod1");





    /* Perform the request, res will get the return code */ 
    res = curl_easy_perform(curl);
    /* Check for errors */ 
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
  curl_global_cleanup();

 system ("pause");
  return 0;

}

【问题讨论】:

  • 这个程序已经发送数据了。你需要更好地说明它有什么问题......

标签: c++ visual-c++ curl


【解决方案1】:

它的工作..检查你的本地主机连接或检查 GiveResponse.JSP 工作正常,没有任何错误。

【讨论】:

  • pod1 是 GiveResponse.jsp 中的一个参数,所以现在我必须像 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "prod1=MyName");它现在工作成功..谢谢。
猜你喜欢
  • 2018-03-30
  • 1970-01-01
  • 2018-06-29
  • 1970-01-01
  • 2018-07-07
  • 1970-01-01
  • 1970-01-01
  • 2020-04-05
  • 2017-11-14
相关资源
最近更新 更多