Форум АНТИЧАТ

Форум АНТИЧАТ (https://forum.antichat.xyz/index.php)
-   С/С++, C#, Delphi, .NET, Asm (https://forum.antichat.xyz/forumdisplay.php?f=24)
-   -   Пост-запрос libcurl C++ (https://forum.antichat.xyz/showthread.php?t=182294)

Скотти 26.02.2010 22:34

Пост-запрос libcurl C++
 
Есть код из примера:
PHP код:

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


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

#include <string>
 
... 
string email="test@mail.ru";
 
string pass="password";
  ...     
curl_easy_setopt(curlCURLOPT_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.

Цитата:

Сообщение от Spy2ex
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, &cookies);

Это если куки из файла загружать, а если просто нужно включить их автосохранение, то не обязательно использовать переменную cookies.
Код:

curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");


Время: 17:31