如何使用curl请求发送数据

时间:2014-04-30 11:04:38

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

我正在编写用于发送带数据的http请求的代码。但我无法在特定网址上发送数据。我正在使用curl进行http请求。我可以获得curl的整个输出,但到目前为止无法使用它发送数据。当我发送请求时,它应该包含一些像hello这样的数据。我怎么能这样做?

#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;

}

1 个答案:

答案 0 :(得分:1)

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