libcurl POST, удалите лишние данные, добавленные Curl - PullRequest
0 голосов
/ 16 декабря 2010

Кто-нибудь знает, как удалить это из поста 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 голосов
/ 22 января 2012

Мне не удалось воспроизвести вашу проблему, 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
0 голосов
/ 17 декабря 2010

Curl будет автоматически добавляться: "method =" post "name =" form_reply "enctype =" multipart / form-data ">

каждый раз, когда вы делаете сообщение, а URL включает параметры или то, что он думаетпередаются параметры.

/ someurl / Topic / comment / replycreate / 27537 *? sp = 1 *

Итак, когда вы удаляете '?'знак вопроса и данные после знака вопроса, curl больше не добавляет какие-либо странные параметры сообщения ...

...