如何使用curl请求发送数据

How to send data with curl request

本文关键字:数据 请求 何使用 curl      更新时间:2023-10-16

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

#include "stdafx.h"
#include <stdio.h>
#include "curl/curl.h"
#include "curleasy.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: %sn",
              curl_easy_strerror(res));
    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
  curl_global_cleanup();
 system ("pause");
  return 0;
}

它正在工作。。检查您的localhost连接或检查GiveResponse.JSP工作正常,没有任何错误。