PDA

Просмотр полной версии : Пост-запрос libcurl C++


Скотти
26.02.2010, 22:34
Есть код из примера:

#include <stdio.h>
#include <curl/curl.h>
int main(void) {
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://postit.example.com/moo.cgi");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
return 0;
}


Как сделать запрос со своими данными?
Например:

#include <string>
...
string email="test@mail.ru";
string pass="password";
...
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "email="+email+"&pass="+pass); ...
Так не получается, в снифере пустой пост запрос.
И еще при указании :
string cookie;
curl_easy_setopt(curl, CURLOPT_COOKIE, cookie);
Куки не сохраняются в переменную. Как исправить?

Spy2ex
26.02.2010, 22:54
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
curl_easy_setopt(curl, CURLOPT_URL, "http://url");
curl_easy_setopt(curl, CURLOPT_HEADER,1);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "запрос");
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, &cookies);
curl_easy_perform(curl); // посылаем



static int writer(char *data, size_t size, size_t nmemb, string *buffer)
{
int result = 0;

if (buffer != NULL)
{
buffer->append(data, size * nmemb);
result = size * nmemb;
}

return result;
}

Врайтер.
передавай ее как ссылку.
&cookie

Скотти
26.02.2010, 23:25
Спасибо.
Пост запрос будет работать так:

static char *s="...";
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, s);

А со string не получается.

Spy2ex
26.02.2010, 23:40
string req ="запрос";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, req.c_str());

M_script_
27.02.2010, 10:26
И еще при указании :
string cookie;
curl_easy_setopt(curl, CURLOPT_COOKIE, cookie);
Куки не сохраняются в переменную.
Они и не должны сохраняться, наоборот они берутся из переменной при использовании CURLOPT_COOKIE.

curl_easy_setopt(curl, CURLOPT_COOKIEFILE, &cookies);
Это если куки из файла загружать, а если просто нужно включить их автосохранение, то не обязательно использовать переменную cookies.
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");