libcurl POST,删除Curl抛出的额外数据

时间:2010-12-16 02:54:38

标签: http post libcurl

任何人都知道如何从Curl帖子中删除这个...?

" method="post" name="form_reply" enctype="multipart/form-data"> 

它正在消息的POST行加上。即...

POST /someurl/Topic/comment/replycreate/27537?sp=1**" method="post" name="form_reply" enctype="multipart/form-data">** HTTP/1.1 

提前致谢...!

好的,更多信息...... 顺便说一句:这是C,而不是PHP


CURL *curl;
CURLcode result;
char errorBuffer[CURL_ERROR_SIZE];
int status=FALSE;
BufHandle *Bufhandle = CreateBufHandle(BUFSIZE);
memset(&errorBuffer,0x0,sizeof(errorBuffer));
curl = curl_easy_init();
if(curl)
{
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, GetPtrToSlist(S));
  curl_easy_setopt(curl, CURLOPT_HEADER, 0);
  curl_easy_setopt(curl, CURLOPT_USERAGENT,  GetParameterByName(S, "useragent"));
  curl_easy_setopt(curl, CURLOPT_COOKIEFILE, GetParameterByName(S, "cookiefile"));
  curl_easy_setopt(curl, CURLOPT_COOKIEJAR,  GetParameterByName(S, "cookiefile"));
  curl_easy_setopt(curl, CURLOPT_URL, url);
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &errorBuffer);
  curl_easy_setopt(curl, CURLOPT_ENCODING , "gzip");
  curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30);
  curl_easy_setopt(curl, CURLOPT_PROXY, "");
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
  //curl_easy_setopt(curl, CURLOPT_RETURNTRANSFER, 1);
  curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  curl_easy_setopt(curl, CURLOPT_POST, 1);
  //curl_easy_setopt(curl, CURLOPT_USERPWD, buf);
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)Bufhandle);
  result = curl_easy_perform(curl);
  curl_slist_free_all(GetPtrToSlist(S));
  curl_easy_cleanup(curl);
  if(result == CURLE_OK)
  {
    status = TRUE;
  }
  else
  {
    printf("Error: [%d] - %s\n",result, errorBuffer);
    status = FALSE;
  }
}

2 个答案:

答案 0 :(得分:0)

卷曲会自动附加: “method =”post“name =”form_reply“enctype =”multipart / form-data“>

任何时候你做一个帖子,网址包含参数,或者它认为传递参数的内容。

/ someurl /主题/评论/ replycreate / 27537 * 的ηsp= 1 *

所以当你删除'?'问号,问号后面的数据,curl不再附加任何奇怪的帖子参数......

答案 1 :(得分:0)

我无法重现您的问题,URI完全按照订单出现。我正在使用libcurl 7.22和以下代码来构建请求:

BOOST_AUTO_TEST_CASE(case_http_session_libcurl)
{
  test_site::site test_site;
  std::string url = "http://localhost:";
  url += boost::lexical_cast<std::string>(test_site.start());
  url += "/post?some=1&data=2";
  char error[CURL_ERROR_SIZE];

  CURL *curl;
  int status=FALSE;
  curl = curl_easy_init();
  if(curl)
  {
    curl_slist* slist = NULL;
    slist = curl_slist_append(slist, "Expect:");
    slist = curl_slist_append(slist, "Some-Rubbish: here");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
    curl_easy_setopt(curl, CURLOPT_HEADER, 0);
    curl_easy_setopt(curl, CURLOPT_USERAGENT,  "testing goes here");
    curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
    curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "");
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error[0]);
    curl_easy_setopt(curl, CURLOPT_ENCODING , "gzip");
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30);
    curl_easy_setopt(curl, CURLOPT_PROXY, "");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "data=being;posted=1");
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_on_recv);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, 0);
    CURLcode result = curl_easy_perform(curl);
    curl_slist_free_all(slist);
    curl_easy_cleanup(curl);
    if(result == CURLE_OK)
    {
      status = TRUE;
    }
    else
    {
      printf("Error: [%d] - %s\n",result, error);
      status = FALSE;
    }
  }
}

并且HTTP服务器收到的请求是:

POST /post?some=1&data=2 HTTP/1.1
User-Agent: testing goes here
Host: localhost:16385
Accept: */*
Accept-Encoding: gzip
Some-Rubbish: here
Content-Length: 19
Content-Type: application/x-www-form-urlencoded

data=being;posted=1